题意:给出$n$个广播站,每个广播站有坐标$x_i$,广播半径$r_i$,广播频率$f_i$,求满足$i\lt j,\min(r_i,r_j)\geq|x_i-x_j|,|f_i-f_j|\leq k$的$(i,j)$对数
因为$f_i$很小,$k$更小,所以不妨考虑枚举$f$,那么频率的限制可以转化只考虑满足$f_j\in[f-k,f+k]$的$j$
现在考虑统计$x_i,r_i$的约束,将$f_i\in[f-k,f-1]\cup[f+1,f+k]$的$i$按$x_i-r_i$排序,再对$f_i=f$的$i$按$x_i$从小到大查询有多少满足约束的,用树状数组和以$x_i+r_i$为关键字的小根堆动态维护当前能广播到$i$的站(按排序顺序插入并删除小根堆中不满足约束的),直接在树状数组上查询$[x_i-r_i,x_i+r_i]$即可,频率相同的最后再统计即可
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
struct sta{
int x,r;
sta(int a=0,int b=0){x=a;r=b;}
};
bool operator<(sta a,sta b){return a.x+a.r>b.x+b.r;}
bool cmpx(sta a,sta b){return a.x<b.x;}
bool cmpl(sta a,sta b){return a.x-a.r<b.x-b.r;}
vector<sta>f[10010],now,nei;
vector<sta>::iterator v1,v2;
map<int,int>pos;
map<int,int>::iterator it;
priority_queue<sta>q;
int k,N,maxf,T[100010];
int lowbit(int x){return x&-x;}
int query(int x){
int s=0;
while(x){
s+=T[x];
x-=lowbit(x);
}
return s;
}
int query(int l,int r){
it=pos.upper_bound(r);
if(it==pos.begin())return 0;
it--;
r=query(it->second);
l=pos.lower_bound(l)->second;
if(l>1)r-=query(l-1);
return r;
}
void modify(int x,int v){
while(x<=N){
T[x]+=v;
x+=lowbit(x);
}
}
ll calc(int x){
int i;
ll ans;
now=f[x];
nei.clear();
for(i=max(x-k,1);i<=min(x+k,maxf);i++){
if(i!=x){
for(v1=f[i].begin();v1!=f[i].end();v1++)nei.push_back(*v1);
}
}
sort(now.begin(),now.end(),cmpx);
sort(nei.begin(),nei.end(),cmpl);
v2=nei.begin();
ans=0;
for(v1=now.begin();v1!=now.end();v1++){
while(v2!=nei.end()&&v2->x-v2->r<=v1->x){
q.push(*v2);
modify(pos[v2->x],1);
v2++;
}
while(!q.empty()&&q.top().x+q.top().rx){
modify(pos[q.top().x],-1);
q.pop();
}
ans+=query(v1->x-v1->r,v1->x+v1->r);
}
while(!q.empty()){
modify(pos[q.top().x],-1);
q.pop();
}
for(v1=now.begin();v1!=now.end();v1++){
while(!q.empty()&&q.top().x+q.top().rx){
modify(pos[q.top().x],-2);
q.pop();
}
ans+=query(v1->x-v1->r,v1->x+v1->r);
modify(pos[v1->x],2);
q.push(*v1);
}
while(!q.empty()){
modify(pos[q.top().x],-2);
q.pop();
}
return ans;
}
int main(){
int n,i,a,b,c;
ll ans;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++){
scanf("%d%d%d",&a,&b,&c);
pos[a]=1;
f[c].push_back(sta(a,b));
maxf=max(maxf,c);
}
for(N=1,it=pos.begin();it!=pos.end();it++,N++)it->second=N;
N--;
ans=0;
for(i=1;i<=maxf;i++)ans+=calc(i);
printf("%I64d",ans>>1);
}