模拟赛题

给定一个坐标系,以及n个点,定义两个的之间的距离 s s s

s ( i , j ) = m a x ( x [ i ] − x [ j ] , y [ i ] − y [ j ] ) s(i,j)=max(x[i]-x[j],y[i]-y[j]) s(i,j)=max(x[i]x[j],y[i]y[j])

求距离在 l l l以内的点对数量

可以二维数点cdq解决,也可以稍微用点trick的树状数组:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 500010;

inline void read(int &x) {
	x = 0; int f = 1; char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	x *= f;
}

struct Node {int l, r;}a[N];
ll c[N];
int n, L, tot; 

#define lowbit(i) (i&-i)
inline void add(int x, ll v) {
	for(int i = x; i <= 6 * L; i += lowbit(i)) c[i] += v;
}
inline ll query(int x) {
	ll ans = 0;
	for(int i = x; i; i -= lowbit(i)) ans += c[i];
	return ans;
}

bool operator < (Node a, Node b) {return a.l < b.l;}

int main() {
	read(n); read(L);
	for(int i = 1; i <= n; ++i) {
		int x, y; read(x); read(y);
		a[i].l = x; a[i].r = y;
		a[i].l += 2 * L + 1; a[i].r += 2 * L + 1;
	}
	sort(a + 1, a + n + 1);
	int cur = 1;
	ll ans = 1ll * n * (n - 1) / 2;
	for(int i = 1; i <= n; ++i) {
		while(cur <= i && a[i].l - a[cur].l >= L) add(a[cur++].r, -1);
		ans -= query(min(6 * L, a[i].r + L - 1)) - query(max(1, a[i].r - L));
		add(a[i].r, 1);
	}
	printf("%lld\n", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值