Radio stations CodeForces - 762E (cdq分治)

大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$

 

按$r$降序排, 去掉$min$, 然后就是个裸的三维数点问题, 可以用$CDQ$分治解决.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



#ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif

int n, k, tot;
struct _ {int x,r,f;} a[N];
struct __ {
	int type,x,y;
	bool operator < (const __ & rhs) const {
		if (x!=rhs.x) return x<rhs.x;
		if (y!=rhs.y) return y<rhs.y;
		return type<rhs.type;
	}
} e[N];
ll ans;
int b[N], c[N], L, R;
void add(int x, int v) {
	for (; x<=*b; x+=x&-x) c[x]+=v;
}
int qry(int x) {
	int r = 0;
	for (; x; x^=x&-x) r+=c[x];
	return r;
}
void qry(int x1, int y1, int x2, int y2) {
	e[++tot] = {1,x2,y2};
	e[++tot] = {1,x1-1,y1-1};
	e[++tot] = {2,x1-1,y2};
	e[++tot] = {2,x2,y1-1};
	b[++*b]=y2,b[++*b]=y1-1;
}
void ins(int x, int y) {
	e[++tot] = {0,x,y};
}
void merge(int l, int r) {
	if (l==r) return;
	merge(l,mid),merge(mid+1,r);
	int now = l;
	REP(i,mid+1,r) {
		while (now<=mid&&e[now].x<=e[i].x) {
			if (e[now].type==0) add(e[now].y,1);
			++now;
		}
		if (e[i].type==1) ans+=qry(e[i].y);
		else if (e[i].type==2) ans-=qry(e[i].y);
	}
	while (now!=l) {
		if (e[--now].type==0) add(e[now].y,-1);
	}
	inplace_merge(e+l,e+mid+1,e+r+1);
}

int main() {
	scanf("%d%d", &n, &k);
	REP(i,1,n) scanf("%d%d%d",&a[i].x,&a[i].r,&a[i].f);
	sort(a+1,a+1+n,[](_ a,_ b){return a.r>b.r;});
	REP(i,1,n) {
		qry(a[i].f-k,a[i].x-a[i].r,a[i].f+k,a[i].x+a[i].r);
		ins(a[i].f,a[i].x);
	}
	sort(b+1,b+1+*b),*b=unique(b+1,b+1+*b)-b-1;
	REP(i,1,tot) e[i].y=lower_bound(b+1,b+1+*b,e[i].y)-b;
	merge(1,tot);
	printf("%lld\n", ans);
}

 

posted @ 2019-05-07 20:42  uid001  阅读(268)  评论(0编辑  收藏  举报