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+=cntx+cnty-2 * cntx,y+cnty+x+cnty-x ans+=cntx+cnty−2∗cntx,y+cnty+x+cnty−x
  • c n t x + c n y y − 2 ∗ c n t x , y cntx+cnyy-2 * cntx,y cntx+cnyy−2∗cntx,y是统计坐标轴方向的,-2倍是需要把本身这个点给减去容斥是减一倍,这里还需要把自己给挖掉
  • c n t y + x + c n t y − x cnty+x+cnty-x cnty+x+cnty−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;
}
相关推荐
jyOverQ9 分钟前
LangGraph 流式执行详解:stream、astream 与 stream_mode 怎么选?
python·langchain
cfm_291414 分钟前
Spring AI Tool 调用架构全解
java·人工智能·spring
sun༒25 分钟前
Spring @Scheduled 定时任务详解:Cron表达式、执行顺序、优先级与并行调度
java·后端·spring
闲猫26 分钟前
LangGraph / Capabilities / Stores
python·agent·langgraph
pjj1985437 分钟前
机器学习-NumPy2
人工智能·python·机器学习
典典分享指南38 分钟前
VS Code Git 工作树:解锁多分支并行开发新体验
算法·决策树·逻辑回归·启发式算法
OPEN-F1 小时前
Python进阶教程:单元测试与代码质量
开发语言·python·单元测试
_Narcissus_1 小时前
枚举和模拟算法笔记
c语言·数据结构·c++·笔记·算法·模拟·枚举
Logintern091 小时前
装饰器和洋葱模式的区分
开发语言·python·架构
天疆说1 小时前
策略的进化:从随心所欲到稳健前行
算法