G. The Morning Star

Problem - G - Codeforces

思路:想了挺长时间的,一直没想到一个简便的方法在瞎搞。我们发现对于某个点来说,其他的点如果能够跟他匹配,那么一定在这8个方向上,而同时这8个方向其实对应这4条直线,假设点为(x1,y1),那么直线为x=x1,y=y1,y=x+y1-x1,y=-x+x1+y1,那么在求能够跟当前点匹配的点时,只需要看一下在这四条直线上的点的数量,假设为a,b,c,d,那么产生的贡献就是a-1+b-1+c-1+d-1,同时我们还需要考虑会不会重复,就是一个点会不会跟一个点匹配两次,因为保证了没有重复点,所以前两条直线是不会重复的,那么后两条直线也是不会重复的,只有当x1=0,y1=0时后两个直线重复,但是在算贡献是一定不会同时添加

cpp 复制代码
// Problem: G. The Morning Star
// Contest: Codeforces - Codeforces Round 886 (Div. 4)
// URL: https://codeforces.com/contest/1850/problem/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms

#include<bits/stdc++.h>
#include<sstream>
#include<cassert>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
PII w[N];

void solve() {
	n=read();
	
	for(int i=1;i<=n;i++) w[i].fi=read(),w[i].se=read();
	
	ll res=0;
	map<int,int> a,b,c,d;
	for(int i=1;i<=n;i++) {
		a[w[i].fi]++;
		b[w[i].se]++;
		c[w[i].fi+w[i].se]++;
		d[w[i].se-w[i].fi]++;
	}
	
	for(int i=1;i<=n;i++) {
		a[w[i].fi]--;
		b[w[i].se]--;
		c[w[i].fi+w[i].se]--;
		d[w[i].se-w[i].fi]--;
		
		res+=a[w[i].fi]+b[w[i].se]+c[w[i].fi+w[i].se]+d[w[i].se-w[i].fi];
	}
	
	printf("%lld\n",res*2);
}   

int main() {
    // init();
    // stin();
	// ios::sync_with_stdio(false); 

    scanf("%d",&T);
    // T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          
相关推荐
郭涤生3 分钟前
AWB算法基础理解
人工智能·算法·计算机视觉
hetao17338375 分钟前
2026-01-21~22 hetao1733837 的刷题笔记
c++·笔记·算法
Hcoco_me7 分钟前
大模型面试题91:合并访存是什么?原理是什么?
人工智能·深度学习·算法·机器学习·vllm
2501_9011478310 分钟前
零钱兑换——动态规划与高性能优化学习笔记
学习·算法·面试·职场和发展·性能优化·动态规划·求职招聘
充值修改昵称2 小时前
数据结构基础:B树磁盘IO优化的数据结构艺术
数据结构·b树·python·算法
程序员-King.8 小时前
day158—回溯—全排列(LeetCode-46)
算法·leetcode·深度优先·回溯·递归
月挽清风9 小时前
代码随想录第七天:
数据结构·c++·算法
小O的算法实验室9 小时前
2026年AEI SCI1区TOP,基于改进 IRRT*-D* 算法的森林火灾救援场景下直升机轨迹规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
小郭团队10 小时前
2_1_七段式SVPWM (经典算法)算法理论与 MATLAB 实现详解
嵌入式硬件·算法·硬件架构·arm·dsp开发
充值修改昵称10 小时前
数据结构基础:从二叉树到多叉树数据结构进阶
数据结构·python·算法