G. The Morning Star

  • 思路:用map记录x,y,以及y-x、y+x
  • 从前往后统计一遍答案即可
  • 公式\(ans+=cnt[x]+cnt[y]-2 * cnt[x,y]+cnt[y+x]+cnt[y-x]\)
  • \(cnt[x]+cny[y]-2 * cnt[x,y]\)是统计坐标轴方向的,-2倍是需要把本身这个点给减去容斥是减一倍,这里还需要把自己给挖掉
  • \(cnt[y+x]+cnt[y-x]\)是统计对角线方向的。这也是一种常用的表示对角线的技巧
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define rep(i, a, b) for(int i = (a); i <= (b); ++i)
  4. #define fep(i, a, b) for(int i = (a); i >= (b); --i)
  5. #define _for(i, a, b) for(int i=(a); i<(b); ++i)
  6. #define pii pair<int, int>
  7. #define pdd pair<double,double>
  8. #define ll long long
  9. #define db double
  10. #define endl '\n'
  11. #define x first
  12. #define y second
  13. #define pb push_back
  14. #define vi vector<int>
  15. /*
  16. * 思路:用map记录x,y,以及y-x、y+x
  17. * 从前往后统计一遍答案即可
  18. */
  19. using namespace std;
  20. const int maxn = 2e5 + 10;
  21. pii a[maxn];
  22. void solve() {
  23. int n;
  24. cin>>n;
  25. map<int,int>cntx,cnty,cntd,cnts;
  26. map<pii,int>cnt;
  27. int ans=0;
  28. rep(i,1,n){
  29. cin>>a[i].x>>a[i].y;
  30. }
  31. sort(a+1,a+1+n);
  32. rep(i,1,n){
  33. int x=a[i].x,y=a[i].y;
  34. ans+=cntx[x];
  35. ans+=cnty[y];
  36. //容斥
  37. ans-=2*cnt[{x,y}];
  38. ans+=cntd[y-x];
  39. ans+=cnts[y+x];
  40. cntx[x]+=1;
  41. cnty[y]+=1;
  42. cntd[y-x]+=1;
  43. cnts[y+x]+=1;
  44. cnt[{x,y}]+=1;
  45. }
  46. cout<<ans*2<<endl;
  47. }
  48. signed main() {
  49. ios::sync_with_stdio(false);
  50. cin.tie(0);
  51. cout.tie(0);
  52. // freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
  53. int _;
  54. cin >> _;
  55. while (_--)
  56. solve();
  57. return 0;
  58. }

13 Codeforces Round 886 (Div. 4)G. The Morning Star(简单容斥)的更多相关文章

  1. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  2. Codeforces Round #547 (Div. 3) G 贪心

    https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...

  3. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  4. Codeforces Round #481 (Div. 3) G. Petya's Exams

    http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...

  5. Codeforces Round #346 (Div. 2) G. Fence Divercity dp

    G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...

  6. Codeforces Round #677 (Div. 3) G. Reducing Delivery Cost(dijkstra算法)

    题目链接:https://codeforces.com/contest/1433/problem/G 题解 跑 \(n\) 遍 \(dijkstra\) 得到任意两点间的距离,然后枚举哪一条边权为 \ ...

  7. Codeforces Round #386 (Div. 2)G. New Roads [构造][树]

    题目链接:G. New Roads 题意:给出n个结点,t层深度,每层有a[i]个结点,总共有k个叶子结点,构造一棵树. 分析: 考虑一颗树,如果满足每层深度上有a[i]结点,最多能有多少叶子结点 那 ...

  8. Codeforces Round #744 (Div. 3) G题题解

    淦,最后一道题没写出来,...还是我太菜了,不过这个题确实比较有趣. G. Minimal Coverage 简化题意:就是你处在坐标轴的0点上,给你一个序列\(a_i\),每次你可以选择向左走\(a ...

  9. Codeforces Round #592 (Div. 2)G(模拟)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...

  10. Codeforces Round #697 (Div. 3) G. Strange Beauty (DP,数学)

    题意:给你一组数,问你最少删去多少数,使得剩下的数,每个数都能整除数组中其它某个数或被数组中其它某个数整除. 题解:我们直接枚举所有因子,\(dp[i]\)表示\(i\)在数组中所含的最大因子数(当我 ...

随机推荐

  1. C/C++ 获取自身IP与域名片段

    判断大端序小端序: 通常情况下,数值在内存中存储的方式有两种,一种是大尾字节序,另一种是小尾,比如0x01020304这样一个数值,如果用大尾方式存储,其存储方式为01 02 03 04而用小尾方式存 ...

  2. MySQL创建, 修改,删除用户密码

    MySQL创建, 修改,删除用户密码 创建用新户名密码 创建用新户名密码: create user 'test1'@'localhost' identified by 'test1'; 修改用户名密码 ...

  3. Intel 14代酷睿提前上架加拿大:涨价最多7%

    Intel将在10月17日正式发布14代酷睿,说白了就是13代酷睿升级版,代号就能说明一切--Raptor Lake Refresh. 首批发布的只是高端的K/KF系列,一共六款,分别是8+16 24 ...

  4. windows10 安装运行docker

    windows10 安装使用docker part01.windows设置 启用windows 虚拟化 任务管理器-性能-CPU-虚拟化启用 启用Hyper-v 控制面板(Win+R -> 输入 ...

  5. 【奶奶看了都会】Meta开源大模型LLama2部署使用教程,附模型对话效果

    1.写在前面 就在7月19日,MetaAI开源了LLama2大模型,Meta 首席科学家.图灵奖获得者 Yann LeCun在推特上表示Meta 此举可能将改变大模型行业的竞争格局.一夜之间,大模型格 ...

  6. 【framework】WMS启动流程

    1 前言 ​ WMS 是 WindowManagerService 的简称. (1)WMS 主要职责 窗口管理:负责启动.添加.删除窗口,管理窗口大小.层级,核心成员有:WindowContainer ...

  7. Java中的包装类(wrapper class)

    1.介绍 顾名思义,包装类是封装Java原始类型的对象.每个Java原始类型都有一个对应的包装类: 基本类型 包装类 boolean Boolean byte Byte short Short cha ...

  8. Shiro实战2-Springboot集成Shiro实战

    说明 最近打算打造一个简易的权限管理系统,打算用shiro做认证和授权.本篇作为springboot集成shiro的入门实战案例记录下来,希望也可以帮到大家,代码整理自网络. 技术栈 springbo ...

  9. spring boot使用拦截器修改请求URL

    假如我要将请求路径中/foobar都去掉? 1.定义拦截器 package com.laoxu.test.helloweb; import org.springframework.stereotype ...

  10. junit多个测试方法共享变量

    本文介绍利用类的静态属性实现junit多个测试方法间共享同一变量值. package com.laoxu.gamedog; import org.junit.Test; /** * @author x ...