13 Codeforces Round 886 (Div. 4)G. The Morning Star(简单容斥)

G. The Morning Star

  • 思路:用map记录x,y,以及y-x、y+x
  • 从前往后统计一遍答案即可
  • 公式 a n s + = c n t [ x ] + c n t [ y ] − 2 ∗ c n t [ x , y ] + c n t [ y + x ] + c n t [ y − x ] ans+=cnt[x]+cnt[y]-2 * cnt[x,y]+cnt[y+x]+cnt[y-x] ans+=cnt[x]+cnt[y]−2∗cnt[x,y]+cnt[y+x]+cnt[y−x]
  • c n t [ x ] + c n y [ y ] − 2 ∗ c n t [ x , y ] cnt[x]+cny[y]-2 * cnt[x,y] cnt[x]+cny[y]−2∗cnt[x,y]是统计坐标轴方向的,-2倍是需要把本身这个点给减去容斥是减一倍,这里还需要把自己给挖掉
  • c n t [ y + x ] + c n t [ y − x ] cnt[y+x]+cnt[y-x] cnt[y+x]+cnt[y−x]是统计对角线方向的。这也是一种常用的表示对角线的技巧
cpp 复制代码
#include <bits/stdc++.h>

#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define vi vector<int>



/*
 * 思路:用map记录x,y,以及y-x、y+x
 * 从前往后统计一遍答案即可
 */
using namespace std;
const int maxn = 2e5 + 10;
pii a[maxn];

void solve() {
    int n;
    cin>>n;
    map<int,int>cntx,cnty,cntd,cnts;
    map<pii,int>cnt;
    int ans=0;
    rep(i,1,n){
        cin>>a[i].x>>a[i].y;
    }
    sort(a+1,a+1+n);
    rep(i,1,n){
        int x=a[i].x,y=a[i].y;
        ans+=cntx[x];
        ans+=cnty[y];
        //容斥
        ans-=2*cnt[{x,y}];
        ans+=cntd[y-x];
        ans+=cnts[y+x];
        cntx[x]+=1;
        cnty[y]+=1;
        cntd[y-x]+=1;
        cnts[y+x]+=1;
        cnt[{x,y}]+=1;
    }
    cout<<ans*2<<endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//	freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
    int _;
    cin >> _;
    while (_--)
        solve();
    return 0;
}
相关推荐
飞翔的佩奇3 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance3 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博4 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
lxmyzzs5 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
Coovally AI模型快速验证6 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
pusue_the_sun6 小时前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
萧鼎6 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
RaymondZhao346 小时前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
zhangfeng11337 小时前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类
yujkss7 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python