大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数.
这个题挺好的, 关键是要求互相看到这个条件, 直接求的话是个四维数点问题, 但是可以发现按照$r$排序后, $r$小的能看到的一定能互相看到, 所以就是一个简单的三维数点了.
#include <iostream> #include <iostream> #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 ' ' #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, b[N]; struct _ { int type,x,y; void pr() { printf("tp=%d,x=%d,y=%d ",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]; struct __ { int x,r,q; } a[N]; ll ans; int c[N], tim[N], clk; void add(int x) { for (; x<=*b; x+=x&-x) tim[x]==clk?++c[x]:c[x]=1,tim[x]=clk; } int qry(int x) { int r = 0; for (; x; x^=x&-x) tim[x]==clk?r+=c[x]:0; return r; } void merge(int l, int r) { if (l==r) return; merge(l,mid),merge(mid+1,r); int now = l; ++clk; REP(i,mid+1,r) { while (now<=mid&&e[now].x<=e[i].x) { if (e[now].type==0) add(e[now].y); ++now; } if (e[i].type==1) ans+=qry(e[i].y); else if (e[i].type==2) ans-=qry(e[i].y); } inplace_merge(e+l,e+mid+1,e+r+1); } int id(int x) { return lower_bound(b+1,b+1+*b,x)-b; } void add(int x, int y) { y = id(y); e[++tot] = {0,x,y}; } void qry(int x1, int y1, int x2, int y2) { y1 = id(y1), y2 = id(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}; } int main() { scanf("%d%d", &n, &k); REP(i,1,n) { scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q); b[++*b]=a[i].q,b[++*b]=a[i].q+k,b[++*b]=a[i].q-k-1; } sort(a+1,a+1+n,[](__ a,__ b){return a.r>b.r;}); sort(b+1,b+1+*b),*b=unique(b+1,b+1+*b)-b-1; REP(i,1,n) { qry(a[i].x-a[i].r,a[i].q-k,a[i].x+a[i].r,a[i].q+k); add(a[i].x,a[i].q); } merge(1,tot); printf("%lld ", ans); }