1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=1e5+10; int n,k,cnt,b[N<<2],g[N],tot,rt[N],Mx;LL Ans; struct node{int x,r,q;}a[N]; class SegmentTree{ private: int id; struct Node{int l,r;LL S;}T[N*40]; #define mid (l+r>>1) #define PT int& x,CI l=1,CI r=cnt #define LT T[x].l,l,mid #define RT T[x].r,mid+1,r #define PU(x) (T[x].S=T[T[x].l].S+T[T[x].r].S) public: I void U(CI p,CI v,PT){ if(!x) x=++id; if(l==r) return void(T[x].S+=v); p<=mid?U(p,v,LT):U(p,v,RT),PU(x); } I LL Q(CI L,CI R,PT){ if(!x) return 0; if(L<=l&&r<=R) return T[x].S; LL S=0;return L<=mid&&(S+=Q(L,R,LT)),R>mid&&(S+=Q(L,R,RT)),S; } }S; int main(){ RI i,j,p;for(read(n,k),i=1;i<=n;i++) read(a[i].x,a[i].r,a[i].q),b[++cnt]=a[i].x-a[i].r,b[++cnt]=a[i].x+a[i].r,b[++cnt]=a[i].x, g[++tot]=a[i].q,Mx=max(Mx,a[i].q);sort(b+1,b+cnt+1),cnt=unique(b+1,b+cnt+1)-b-1,sort(g+1,g+tot+1),tot=unique(g+1,g+tot+1)-g-1; #define LWx(x) (lower_bound(b+1,b+cnt+1,x)-b) #define LWy(x) (lower_bound(g+1,g+tot+1,x)-g) for(sort(a+1,a+n+1,[&](Cn node& x,Cn node& y){return x.r>y.r;}),i=1;i<=n;S.U(LWx(a[i].x),1,rt[LWy(a[i].q)]),i++) for(j=max(0,a[i].q-k);j<=min(Mx,a[i].q+k);j++) if(p=LWy(j),g[p]==j) Ans+=S.Q(LWx(a[i].x-a[i].r),LWx(a[i].x+a[i].r),rt[p]); return writeln(Ans),0; }
|