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;
}
相关推荐
爱上语文12 分钟前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
荆州克莱14 分钟前
springcloud整合nacos、sentinal、springcloud-gateway,springboot security、oauth2总结
spring boot·spring·spring cloud·css3·技术
waterHBO1 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
limingade2 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate4 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
jiao000015 小时前
数据结构——队列
c语言·数据结构·算法
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法
FreakStudio6 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy