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;
}
相关推荐
y1su14 分钟前
Leetcode 字符串模板
java·数据结构·算法·leetcode·职场和发展·动态规划
residual_fan44 分钟前
船用柴油机动态机动工况故障诊断:控制感知仿射网络(CAAN)的技术实现
人工智能·算法·数据挖掘·数据分析
爱上纯净的蓝天1 小时前
不用云做 RAG:一个本地笔记问答工具的取舍与坑
python·开源·大模型·rag·本地部署
其实防守也摸鱼1 小时前
Cylab在线靶场——CTF中的通用技能
linux·数据库·学习·算法·靶场·ctf·学习网络安全
cspttty1 小时前
2027风险管理岗准备路线:数学与应用数学如何补齐金融、SQL与Python
开发语言·python·金融
用户8356290780511 小时前
如何使用 Python 格式化 Excel 单元格中的长文本
后端·python
我命由我123451 小时前
Python 开发 - Python 的 pathlib
java·开发语言·后端·python·学习·java-ee·学习方法
蓝悦无人机2 小时前
《Planning algorithms》读书笔记——第8章 反馈运动规划
算法·读书笔记·反馈运动规划
RaaS1002 小时前
Agent Skills 进了 Linux Foundation:2.8 亿次安装,但装 skill 还没有安全规范
人工智能·算法·架构
严谨的麻辣烫2 小时前
Python HTTP 连接池怎么用?从连接复用到请求异常排查
开发语言·python·http·requests·网络排查