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;
}
相关推荐
C++ 老炮儿的技术栈2 分钟前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
jc_hook12 分钟前
Python 接入DeepSeek
python·大模型·deepseek
chicpopoo31 分钟前
Python打卡DAY25
开发语言·python
yychen_java38 分钟前
R-tree详解
java·算法·r-tree
MarkHard1231 小时前
Leetcode (力扣)做题记录 hot100(62,64,287,108)
算法·leetcode·职场和发展
crazyme_61 小时前
深入掌握 Python 切片操作:解锁数据处理的高效密码
开发语言·python
一只鱼^_2 小时前
牛客练习赛138(首篇万字题解???)
数据结构·c++·算法·贪心算法·动态规划·广度优先·图搜索算法
一只码代码的章鱼2 小时前
Spring的 @Validate注解详细分析
前端·spring boot·算法
邹诗钰-电子信息工程2 小时前
嵌入式自学第二十一天(5.14)
java·开发语言·算法
有梦想的攻城狮2 小时前
spring中的@MapperScan注解详解
java·后端·spring·mapperscan