#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <math.h>
#include <iomanip>
using namespace std;

#define FOR0(i,n) for( i = 0 ; i < n ; i++ )
#define FOR1(i,n) for( i = 1 ; i <= n ; i++ )
#define sys_p system( "pause" )
#define End return 0
#define pb push_back
#define mp make_pair

ifstream Cin("input.txt");
ofstream Cout( "output.txt" );

typedef int ll ;

set < ll > clr ;
vector < ll > e[100001] ;
ll c[100001] ;

void dfs( ll t, ll p )
{
        ll i, sz = (ll)e[t].size(), next ;
        for( i = 0 ; i < sz ; i++ )
        {
             next = e[t][i] ;
             if( next != p )
             {
                 if( c[t] == c[next] )
                   dfs( next, t ) ;
                 else
                   clr.insert( c[next] ) ;
             }
        }
}

main()
{
      ll n, m, i, j, a, b, mx = -1, mi, cur ;
      cin >> n >> m ;
      FOR1( i, n )
        cin >> c[i] ;
      FOR1( i, m )
      {
        cin >> a >> b ;
        e[a].push_back( b ) ;
        e[b].push_back( a ) ;
      }
      FOR1( i, n )
      {
            clr.clear() ;
            dfs(i, -1) ;
            if( clr.size() > mx || clr.size() == mx && c[i] < mi )
              mx = clr.size(), mi = c[i] ;
      }
      cout << mi ;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <math.h>
#include <iomanip>
using namespace std;

#define FOR0(i,n) for( i = 0 ; i < n ; i++ )
#define FOR1(i,n) for( i = 1 ; i <= n ; i++ )
#define sys_p system( "pause" )
#define End return 0
#define pb push_back
#define mp make_pair

ifstream Cin("input.txt");
ofstream Cout( "output.txt" );

typedef long long ll ;

main()
{
      ll n, m, i, a, b, c[10001], mx = -1, mi ;
      cin >> n >> m ;
      set < ll > zz[10001] ;
      FOR1( i, n )
        cin >> c[i] ;
      FOR1( i, m )
      {
        cin >> a >> b ;
        if( c[a] != c[b] ) zz[ c[a] ].insert( c[b] ) ;
        if( c[a] != c[b] ) zz[ c[b] ].insert( c[a] ) ;

        if( (ll)zz[ c[a] ].size() > mx || (ll)zz[ c[a] ].size() == mx && c[a] < mi )
          mx = (ll)zz[ c[a] ].size(), mi = c[a] ;

        if( (ll)zz[ c[b] ].size() > mx || (ll)zz[ c[b] ].size() == mx && c[b] < mi )
          mx = (ll)zz[ c[b] ].size(), mi = c[b] ;
      }
      cout << mi ;
}


#include <iostream>
#include <set>
#define DN 100005
using namespace std;

int n,m,cul[DN],rez,af=(1<<30);
set<int> gr[DN];

int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; ++i) {
        cin>>cul[i];
        af=min(af,cul[i]);
    }
    for(int i=1; i<=m; ++i) {
        int a,b;
        cin>>a>>b;
        if(cul[a]==cul[b]) continue;
        gr[cul[a]].insert(cul[b]);
        gr[cul[b]].insert(cul[a]);
    }
    for(int i=1; i<=n; ++i)
        if(gr[cul[i]].size()>gr[af].size() || (gr[cul[i]].size()==gr[af].size() && af>cul[i])) af=cul[i];
    cout<<af;
    return 0;
}


#include <iostream>
#include <set>
#define DN 100005
using namespace std;

int n,m,cul[DN],rez,af;
set<int> gr[DN];

int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; ++i) {
        cin>>cul[i];
        af=min(af,cul[i]);
    }
    for(int i=1; i<=m; ++i) {
        int a,b;
        cin>>a>>b;
        gr[cul[a]].insert(cul[b]);
        gr[cul[b]].insert(cul[a]);
    }
    for(int i=1; i<=n; ++i)
        if(gr[cul[i]].size()>gr[af].size() || (gr[cul[i]].size()==gr[af].size() && af>cul[i])) af=cul[i];
    cout<<af;
    return 0;
}


#include <iostream>
#include <vector>
#define DN 100005
#include <bitset>
using namespace std;

typedef vector<int>::iterator it;

int n,m,cul[DN],rez,af;
bitset<DN> fol,f2;
vector<int> gr[DN];

int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; ++i) cin>>cul[i];
    for(int i=1; i<=m; ++i) {
        int a,b;
        cin>>a>>b;
        gr[cul[a]].push_back(cul[b]);
        gr[cul[b]].push_back(cul[a]);
    }
    for(int i=1; i<=n; ++i) if(!f2[cul[i]]){
        int nod=cul[i],cnt=0;
        f2[nod]=1;
        //cout<<nod<<' ';
        for(it j=gr[nod].begin(); j!=gr[nod].end(); ++j) if(*j!=nod && !fol[*j]) {
            ++cnt;
            fol[*j]=1;
        }
        fol&=0;
        if(rez==cnt) af=min(af,nod);
        if(rez<cnt) {
            rez=max(rez,cnt);
            af=nod;
        }

    }
    cout<<af;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
const int MAX_N = 100100;
int color[MAX_N];
typedef vector<int>::iterator iter;
vector<int> g[MAX_N];
set<int> card[MAX_N];
int main() {
    int N, M;
    cin >> N >> M;
    for (int i = 1; i <= N; ++i) {
        cin >> color[i];
    }
    for (int i = 1; i <= M; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int max = 0, maxColor = 0;
    for (int i = 1; i <= N; ++i) {
        for (iter j = g[i].begin(); j != g[i].end(); ++j) {
            if (color[*j] != color[i]) {
                card[color[i]].insert(color[*j]);
            }
        }
        if (card[color[i]].size() > max || (card[color[i]].size() == max && color[i] < maxColor)) {
            max = card[color[i]].size();
            maxColor = color[i];
        }
    }
    cout << maxColor;

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

#define LL long long
#define INI(x) memset(x,0,sizeof(x))
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))

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

const int MAXN=2e5+7;
long long a[MAXN];
long long f,x,n;
long long ans=0;

int main()
{
	read(n);
	for(int i=0;i<n;i++)read(a[i]);
	read(x);
	read(f);
	for(int i=0;i<n;i++)
	{
		ans+=f*(a[i]/(x+f));
		long long left=a[i]%(x+f);
		if(left>x)ans+=f;
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

#define LL long long
#define INI(x) memset(x,0,sizeof(x))
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))

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

const int MAXN=2e5+7;
int a[MAXN];
long long f,x,n;
long long ans=0;

int main()
{
	read(n);
	for(int i=0;i<n;i++)read(a[i]);
	read(x);
	read(f);
	for(int i=0;i<n;i++)ans+=(a[i]+x+f-1)/(x+f);
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

#define LL long long
#define INI(x) memset(x,0,sizeof(x))
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))

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

const int MAXN=2e5+7;
int a[MAXN];
int f,x,n;
int ans=0;

int main()
{
	read(n);
	for(int i=0;i<n;i++)read(a[i]);
	read(x);
	read(f);
	for(int i=0;i<n;i++)ans+=(a[i]+x+f-1)/(x+f);
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

#define LL long long
#define INI(x) memset(x,0,sizeof(x))
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))

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

const int MAXN=2e5+7;
long long a[MAXN];
long long f,x,n;
long long ans=0;

int main()
{
	read(n);
	for(int i=0;i<n;i++)read(a[i]);
	read(x);
	read(f);
	for(int i=0;i<n;i++)ans+=(a[i]+f-1)/(x+f);
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

#define LL long long
#define INI(x) memset(x,0,sizeof(x))
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))

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

const int MAXN=2e5+7;
long long a[MAXN];
long long f,x,n;
long long ans=0;

int main()
{
	read(n);
	for(int i=0;i<n;i++)read(a[i]);
	read(x);
	read(f);
	for(int i=0;i<n;i++)ans+=(a[i]+x+f-1)/(x+f);
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define RG register
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

inline int read()
{
	int data=0, w=1;
	char ch=getchar();
	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
	if(ch=='-') w=-1, ch=getchar();
	while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
	return data*w;
}

const int maxn(200010);
long long n, a[maxn], x, f, ans;

int main()
{
	n = read();
	for(RG int i = 1; i <= n; i++) a[i] = read();
	x = read(); f = read();
	for(RG int i = 1; i <= n; i++)
		if(a[i] > x)
		{
			a[i] = x; a[++n] = a[i] - x - f;
			if(a[n] < 0) a[i] += a[n], a[n] = 0;
			ans += f;
		}
	printf("%I64d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
#define RG register
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

inline int read()
{
	int data=0, w=1;
	char ch=getchar();
	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
	if(ch=='-') w=-1, ch=getchar();
	while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
	return data*w;
}
/////////////////////////////
const int maxn(200010);
long long n, a[maxn], x, f, ans;

int main()
{
	n = read();
	for(RG int i = 1; i <= n; i++) a[i] = read();
	x = read(); f = read();
	for(RG int i = 1; i <= n; i++)
		{
			a[i] -= x;
			if(a[i] > 0)
			{
				if(!(a[i] % (x + f)))
					ans += (a[i] / (x + f)) * f;
				else ans += (1 + a[i] / (x + f)) * f;
			}
		}
	printf("%I64d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
#define RG register
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

inline int read()
{
	int data=0, w=1;
	char ch=getchar();
	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
	if(ch=='-') w=-1, ch=getchar();
	while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
	return data*w;
}

const int maxn(200010);
long long n, a[maxn], x, f, ans;

int main()
{
	n = read();
	for(RG int i = 1; i <= n; i++) a[i] = read();
	x = read(); f = read();
	for(RG int i = 1; i <= n; i++)
		{
			a[i] -= x;
			if(a[i] > 0)
			{
				if(!(a[i] % (x + f)))
					ans += (a[i] / (x + f)) * f;
				else ans += (1 + a[i] / (x + f)) * f;
			}
		}
	printf("%I64d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res,cur,flag;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else continue;
		if(b[i]<=cnt+k)
		{
			flag--;
			continue;
		}
		cur=b[i]%(cnt+k);
		b[i]-=cur;
		if(b[i]<=(cnt+k)*2)res+=b[i]/(cnt+k);
		else
		{
			res+=b[i]/(cnt+k)-1;
			flag++;
		}
	}
	cout<<(res+abs(flag))*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,sum;
ll a[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)sum+=a[i]-k;
	}
	if(sum%k==0)
	{
		cout<<sum/k*cnt<<endl;
		return 0;
	}
	else
	{
		cout<<(sum/k+1)*cnt<<endl;
		return 0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,sum,res;
ll a[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)sum+=a[i]-k;
		else res+=k-a[i];
	}
	sum=sum-res;
	if(sum%k==0)
	{
		cout<<sum/k*cnt<<endl;
		return 0;
	}
	else
	{
		cout<<(sum/k+1)*cnt<<endl;
		return 0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else b[i]=0;
		//else res+=k-a[i];
	}
	for(int i=1;i<=n;i++)
	{
		if(b[i]==0)continue;
		while(b[i]>k)
		{
			res++;
			b[i]=b[i]-cnt-k;
		}
	}
	cout<<res*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else b[i]=0;
		/*while(b[i]>k)
		{
			res++;
			b[i]=b[i]-cnt-k;
		}*/
		if(b[i]==0)continue;
		b[i]=b[i]-k;
		res+=b[i]/(cnt+k)+1;
		//cout<<res<<endl;
		//else res+=k-a[i];
	}
	cout<<res*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res,cur,flag;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else continue;
		if(b[i]<=cnt+k)
		{
			if(flag>0)flag--;
			else res++;
			continue;
		}
		cur=b[i]%(cnt+k);
		b[i]-=cur;
		if(b[i]<=(cnt+k)*2)res+=b[i]/(cnt+k);
		else
		{
			res+=b[i]/(cnt+k)-1;
			flag++;
		}
	}
	cout<<(res+flag)*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res,cur,flag;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else continue;
		if(b[i]<=cnt+k)
		{
			flag--;
			continue;
		}
		cur=b[i]%(cnt+k);
		b[i]-=cur;
		if(b[i]<=cnt+k+cnt)res+=b[i]/(cnt+k);
		else
		{
			res+=b[i]/(cnt+k)-1;
			flag++;
		}
	}
	cout<<(res+abs(flag))*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res,cur;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else b[i]=0;
		/*while(b[i]>k)
		{
			res++;
			b[i]=b[i]-cnt-k;
		}*/
		if(b[i]==0)continue;
		//b[i]=b[i]-k;
		if(b[i]<cnt+k)b[i]+=cnt+k;
		cur=b[i]%(cnt+k);
		b[i]-=cur;
		res+=b[i]/(cnt+k);
		//cout<<res<<endl;
		//else res+=k-a[i];
	}
	cout<<res*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res,cur;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else continue;
		if(b[i]<=cnt+k)
		{
			res+=1;
			continue;
		}
		cur=b[i]%(cnt+k);
		b[i]-=cur;
		res+=b[i]/(cnt+k);
	}
	cout<<res*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res,cur;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else continue;
		if(b[i]<cnt+k)
		{
			res+=1;
			continue;
		}
		cur=b[i]%(cnt+k);
		b[i]-=cur;
		res+=b[i]/(cnt+k);
	}
	cout<<res*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res,cur,flag;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else continue;
			res += ceil(((double)(a[i]-k))/(k+cnt));
			//cout<<res<<endl;
	}
	cout<<(res)*cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=200010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,k,cnt,res;
ll a[maxn],b[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>k>>cnt;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>k)b[i]=a[i];
		else b[i]=0;
		while(b[i]>k)
		{
			res++;
			b[i]=b[i]-cnt-k;
		}
		//else res+=k-a[i];
	}
	cout<<res*cnt<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
// #define int long long
#define idx(v,x) (lower_bound(v.begin(), v.end(),(x))-v.begin())
map<int,vector<int> > m;
inline int get(int l, int r, vector<int> &v)
{
    int ans = upper_bound(v.begin(), v.end(), r) - upper_bound(v.begin(), v.end(), l-1);
    return ans;
}
struct s{
    int x,r,q;
    const bool operator < (const s &r) const {
        return r.r < this->r;
    }
};
vector<vector<int> > bit;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> order_set;
vector<order_set> ost;
int T=1;
void ost_insert(order_set &x, int val)
{
    x.insert({val,T++});
}
int upper_bound(order_set &x, int val)
{
    return x.order_of_key({val+1,0});
}
int32_t main() {
    ios::sync_with_stdio(false);cin.tie(0);
    int n,k;
    cin>>n>>k;
    vector<s> v(n);
    vector<int> c;
    c.reserve(2*(k+1)*n);
    for(int i=0;i<n;i++)
    {
        cin>>v[i].x>>v[i].r>>v[i].q;
        for(int j=v[i].q-k;j<=v[i].q+k;j++)
        {
            c.emplace_back(j);
        }
    }
    sort(c.begin(), c.end());
    c.erase(unique(c.begin(),c.end()),c.end());
    ost.resize(c.size());
    sort(v.begin(), v.end());
    long long ans = 0;
    for(int i=0;i<n;i++)
    {
        int loc;
        for(int j=v[i].q-k;j<=v[i].q+k;j++)
        {
            loc = idx(c,j);
            ans += upper_bound(ost[loc],v[i].x+v[i].r)-upper_bound(ost[loc],v[i].x-v[i].r-1);
        }
        loc = idx(c,v[i].q);
        ost_insert(ost[loc],v[i].x);
    }
    cout<<ans;
    return 0;
}


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

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x) << '\n', 0;
#define all(_obj)                     (_obj).begin(), (_obj).end()
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define has(_obj, _val)               ((_obj).find(_val) != (_obj).end())
#define sz(_obj)                      static_cast<int>((_obj).size())
#define debug(_x)                     std::cerr << (#_x) << " = " << (_x) << '\n';
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = std::next(_array, _N);
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
#include <ext/pb_ds/assoc_container.hpp>

template <typename TType>
using ordered_set = __gnu_pbds::tree<
    TType,
    __gnu_pbds::null_type,
    std::less<TType>,
    __gnu_pbds::rb_tree_tag,
    __gnu_pbds::tree_order_statistics_node_update
>;

struct TEvent {
    int X;
    int Q;
    int R;
    int Id;
    char Type;

    TEvent(int x, int q, int r, int id, char t)
        : X(x)
        , Q(q)
        , R(r)
        , Id(id)
        , Type(t)
    {
    }

    bool operator<(const TEvent& other) {
        if (this->Id == other.Id) {
            return this->Type < other.Type;
        }
        return this->Id < other.Id;
    }
};

map<int, ordered_set<pii>> tr;
vector<TEvent> events;
map<int, int> index;

inline int GetVisible(int l, int r, int lx, int rx) {
    int res = 0;
    for (int q = l; q <= r; ++q) {
        if (has(tr, q)) {
            res += tr[q].order_of_key({rx + 1, 0}) - tr[q].order_of_key({lx, 0});
        }
    }
    return res - 1;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin ("input.txt",  std::ios::in);
    std::ofstream cout("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int n, k;
    cin >> n >> k;
    loop(n) {
        int x, r, q;
        cin >> x >> r >> q;
        events.emplace_back(x, q, r, x - r, 0);
        events.emplace_back(x, q, r, x, 1);
        events.emplace_back(x, q, r, x + r, 2);
    }
    sort(all(events));
    ll ans = 0;
    for (auto&& e : events) {
        switch (e.Type) {
            case 0:
                tr[e.Q].insert({e.X, index[e.X]});
                ++index[e.X];
                break;
            case 1:
                ans += GetVisible(e.Q - k, e.Q + k, e.X - e.R, e.X + e.R);
                break;
            case 2:
                --index[e.X];
                tr[e.Q].erase({e.X, index[e.X]});
                break;
        }
    }
    cout << ans / 2 << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


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

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x) << '\n', 0;
#define all(_obj)                     (_obj).begin(), (_obj).end()
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define has(_obj, _val)               ((_obj).find(_val) != (_obj).end())
#define sz(_obj)                      static_cast<int>((_obj).size())
#define debug(_x)                     std::cerr << (#_x) << " = " << (_x) << '\n';
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = std::next(_array, _N);
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
#include <ext/pb_ds/assoc_container.hpp>

template <typename TType>
using ordered_set = __gnu_pbds::tree<
    TType,
    __gnu_pbds::null_type,
    std::less<TType>,
    __gnu_pbds::rb_tree_tag,
    __gnu_pbds::tree_order_statistics_node_update
>;

struct TEvent {
    int X;
    int Q;
    int R;
    int Id;
    char Type;

    TEvent(int x, int q, int r, int id, char t)
        : X(x)
        , Q(q)
        , R(r)
        , Id(id)
        , Type(t)
    {
    }

    bool operator<(const TEvent& other) {
        if (this->Id == other.Id) {
            return this->Type < other.Type;
        }
        return this->Id < other.Id;
    }
};

map<int, ordered_set<int>> tr;
vector<TEvent> events;

inline int GetVisible(int l, int r, int lx, int rx) {
    int res = 0;
    for (int q = l; q <= r; ++q) {
        if (has(tr, q)) {
            res += tr[q].order_of_key(rx + 1) - tr[q].order_of_key(lx);
        }
    }
    return res - 1;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin ("input.txt",  std::ios::in);
    std::ofstream cout("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int n, k;
    cin >> n >> k;
    loop(n) {
        int x, r, q;
        cin >> x >> r >> q;
        events.emplace_back(x, q, r, x - r, 0);
        events.emplace_back(x, q, r, x, 1);
        events.emplace_back(x, q, r, x + r, 2);
    }
    sort(all(events));
    ll ans = 0;
    for (auto&& e : events) {
        switch (e.Type) {
            case 0:
                tr[e.Q].insert(e.X);
                break;
            case 1:
                ans += GetVisible(e.Q - k, e.Q + k, e.X - e.R, e.X + e.R);
                break;
            case 2:
                tr[e.Q].erase(e.X);
                break;
        }
    }
    cout << ans / 2 << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


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

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x) << '\n', 0;
#define all(_obj)                     (_obj).begin(), (_obj).end()
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define has(_obj, _val)               ((_obj).find(_val) != (_obj).end())
#define sz(_obj)                      static_cast<int>((_obj).size())
#define debug(_x)                     std::cerr << (#_x) << " = " << (_x) << '\n';
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = std::next(_array, _N);
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
#include <ext/pb_ds/assoc_container.hpp>

template <typename TType>
using ordered_set = __gnu_pbds::tree<
    TType,
    __gnu_pbds::null_type,
    std::less<TType>,
    __gnu_pbds::rb_tree_tag,
    __gnu_pbds::tree_order_statistics_node_update
>;

struct TEvent {
    int X;
    int Q;
    int R;
    int Id;
    char Type;

    TEvent(int x, int q, int r, int id, char t)
        : X(x)
        , Q(q)
        , R(r)
        , Id(id)
        , Type(t)
    {
    }

    bool operator<(const TEvent& other) {
        if (this->Id == other.Id) {
            return this->Type < other.Type;
        }
        return this->Id < other.Id;
    }
};

map<int, ordered_set<int>> tr;
vector<TEvent> events;

inline int GetVisible(int l, int r, int lx, int rx) {
    int res = 0;
    for (int q = l; q <= r; ++q) {
        if (has(tr, q)) {
            res += tr[q].order_of_key(rx + 1) - tr[q].order_of_key(lx);
        }
    }
    return res - 1;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin ("input.txt",  std::ios::in);
    std::ofstream cout("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int n, k;
    cin >> n >> k;
    loop(n) {
        int x, r, q;
        cin >> x >> r >> q;
        events.emplace_back(x, q, r, x, 0);
        events.emplace_back(x, q, r, x - r, 1);
        events.emplace_back(x, q, r, x + r, 2);
    }
    sort(all(events));
    ll ans = 0;
    for (auto&& e : events) {
        switch (e.Type) {
            case 0:
                ans += GetVisible(e.Q - k, e.Q + k, e.X - e.R, e.X + e.R);
                break;
            case 1:
                tr[e.Q].insert(e.X);
                break;
            case 2:
                tr[e.Q].erase(e.X);
                break;
        }
    }
    cout << ans / 2 << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


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

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x) << '\n', 0;
#define all(_obj)                     (_obj).begin(), (_obj).end()
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define has(_obj, _val)               ((_obj).find(_val) != (_obj).end())
#define sz(_obj)                      static_cast<int>((_obj).size())
#define debug(_x)                     std::cerr << (#_x) << " = " << (_x) << '\n';
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = std::next(_array, _N);
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
#include <ext/pb_ds/assoc_container.hpp>

template <typename TType>
using ordered_set = __gnu_pbds::tree<
    TType,
    __gnu_pbds::null_type,
    std::less<TType>,
    __gnu_pbds::rb_tree_tag,
    __gnu_pbds::tree_order_statistics_node_update
>;

struct TEvent {
    int X;
    int Q;
    int Id;
    char Type;

    TEvent(int x, int q, int id, char t)
        : X(x)
        , Q(q)
        , Id(id)
        , Type(t)
    {
    }

    bool operator<(const TEvent& other) {
        if (this->Id == other.Id) {
            return this->Type < other.Type;
        }
        return this->Id < other.Id;
    }
};

map<int, ordered_set<int>> tr;
vector<TEvent> events;

inline int GetVisible(int l, int r, int x) {
    int res = 0;
    for (int q = l; q <= r; ++q) {
        res += tr[q].order_of_key(x);
    }
    return res;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin ("input.txt",  std::ios::in);
    std::ofstream cout("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int n, k;
    cin >> n >> k;
    loop(n) {
        int x, r, q;
        cin >> x >> r >> q;
        events.emplace_back(x, q, x - r, 0);
        events.emplace_back(x, q, x, 1);
        events.emplace_back(x, q, x + r, 2);
    }
    sort(all(events));
    ll ans = 0;
    for (auto&& e : events) {
        switch (e.Type) {
            case 0:
                tr[e.Q].insert(e.X);
                break;
            case 1:
                ans += GetVisible(e.Q - k, e.Q + k, e.X);
                break;
            case 2:
                tr[e.Q].erase(e.X);
                break;
        }
    }
    cout << ans << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


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

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x) << '\n', 0;
#define all(_obj)                     (_obj).begin(), (_obj).end()
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define has(_obj, _val)               ((_obj).find(_val) != (_obj).end())
#define sz(_obj)                      static_cast<int>((_obj).size())
#define debug(_x)                     std::cerr << (#_x) << " = " << (_x) << '\n';
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = std::next(_array, _N);
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
#include <ext/pb_ds/assoc_container.hpp>

template <typename TType>
using ordered_set = __gnu_pbds::tree<
    TType,
    __gnu_pbds::null_type,
    std::less<TType>,
    __gnu_pbds::rb_tree_tag,
    __gnu_pbds::tree_order_statistics_node_update
>;

struct TEvent {
    int X;
    int Q;
    int R;
    int Id;
    char Type;

    TEvent(int x, int q, int r, int id, char t)
        : X(x)
        , Q(q)
        , R(r)
        , Id(id)
        , Type(t)
    {
    }

    bool operator<(const TEvent& other) {
        if (this->Id == other.Id) {
            return this->Type < other.Type;
        }
        return this->Id < other.Id;
    }
};

map<int, ordered_set<int>> tr;
vector<TEvent> events;

inline int GetVisible(int l, int r, int lx, int rx) {
    int res = 0;
    for (int q = l; q <= r; ++q) {
        if (has(tr, q)) {
            res += tr[q].order_of_key(rx + 1) - tr[q].order_of_key(lx);
        }
    }
    return res - 1;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin ("input.txt",  std::ios::in);
    std::ofstream cout("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int n, k;
    cin >> n >> k;
    loop(n) {
        int x, r, q;
        cin >> x >> r >> q;
        events.emplace_back(x, q, r, x - r, 0);
        events.emplace_back(x, q, r, x, 1);
        events.emplace_back(x, q, r, x + r, 2);
    }
    sort(all(events));
    ll ans = 0;
    for (auto&& e : events) {
        switch (e.Type) {
            case 0:
                ans += GetVisible(e.Q - k, e.Q + k, e.X - e.R, e.X + e.R);
                break;
            case 1:
                tr[e.Q].insert(e.X);
                break;
            case 2:
                tr[e.Q].erase(e.X);
                break;
        }
    }
    cout << ans / 2 << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


#include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
inline int _min(int x, int y) {return x < y ? x : y;}
inline int _max(int x, int y) {return x > y ? x : y;}
inline int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct tnode {
	int lc, rc, c;
} t[6000010]; int cnt, rt[610000];
struct node {
	int x, l, r, ll, rr, c;
} a[610000], q[610000]; int root[610000];
int tim, xx[610000], rr[610000], Q[610000], vis[610000];
int b[610000], pa[610000], hh[610000], pp[610000];
LL ans = 0;

bool cmp(node a, node b) {
	if(a.x == b.x) return a.l < b.l;
	return a.x < b.x;
}

bool cmp1(node a, node b) {
	if(a.x == b.x) return a.c > b.c;
	return a.x < b.x;
}

void Link(int &u, int l, int r, int p, int c) {
	if(!u) u = ++cnt;
	t[u].c++;
	if(l == r) return ;
	int mid = (l + r) / 2;
	if(p <= mid) Link(t[u].lc, l, mid, p, c);
	else Link(t[u].rc, mid + 1, r, p, c);
}

int query(int u1, int l, int r, int ll, int rr) {
	if(l == ll && r == rr) return t[u1].c;
	int mid = (l + r) / 2;
	if(rr <= mid) return query(t[u1].lc, l, mid, ll, rr);
	else if(ll > mid) return query(t[u1].rc, mid + 1, r, ll, rr);
	else return query(t[u1].lc, l, mid, ll, mid) + query(t[u1].rc, mid + 1, r, mid + 1, rr);
}

inline void solve(int l, int r) {
	tim++;
	for(register int i = l; i <= r; ++i) {
		if(vis[a[i].c] != tim) vis[a[i].c] = tim, root[a[i].c] = 0;
		root[a[i].c]++;
	}
	int re=0;
	for(register int i=l;i<=r;++i)
		for(int j=a[i].ll;j<=a[i].rr;j++) if(pa[j]&&vis[j]==tim) re+=root[j];
	ans=ans-(re-(r-l+1))/2-(r-l+1);
}

int main() {
	int n, k; scanf("%d%d", &n, &k);
	for(register int i = 1; i <= n; ++i) {
		xx[i] = read(), rr[i] = read(), Q[i] = read();
		b[i * 3 - 2] = _max(0, xx[i] - rr[i]);
		b[i * 3 - 1] = xx[i];
		b[i * 3] = _min(xx[i] + rr[i], 1e9);
	} sort(b + 1, b + 3 * n + 1);
	for(register int i = 1; i <= n; ++i) {
		a[i].x = lower_bound(b + 1, b + 3 * n + 1, xx[i]) - b;
		a[i].l = lower_bound(b + 1, b + 3 * n + 1, _max(0, xx[i] - rr[i])) - b;
		a[i].r = lower_bound(b + 1, b + 3 * n + 1, _min(xx[i] + rr[i], 1e9)) - b;
	}
	for(register int i = 1; i <= n; ++i) {
		b[i * 3 - 2] = _max(0, Q[i] - k);
		b[i * 3 - 1] = Q[i];
		b[i * 3] = _min(Q[i] + k, 1e9);
	} sort(b + 1, b + 3 * n + 1);
	for(register int i = 1; i <= n; ++i) {
		a[i].c = lower_bound(b + 1, b + 3 * n + 1, Q[i]) - b;
		pa[a[i].c] = 1;
		a[i].ll = lower_bound(b + 1, b + 3 * n + 1, _max(0, Q[i] - k)) - b;
		a[i].rr = lower_bound(b + 1, b + 3 * n + 1, _min(Q[i] + k, 1e9)) - b;
	}
	int cnt = 0;
	for(register int i = 1; i <= n; ++i) {
		q[++cnt].x = a[i].l - 1; q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 1;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 2;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].r, q[cnt].r = a[i].c, q[cnt].c = 3;
	} sort(q + 1, q + cnt + 1, cmp1);
	int tp1 = 0, tp2 = 0;
	ans = 0; int last = 0;
	for(register int i = 1; i <= cnt; ++i) {
		if(q[i].c == 3) Link(rt[q[i].r], 0, 3 * n, q[i].l, 1);
		else {
			int tt = 0;
			for(register int j = q[i].l; j <= q[i].r; ++j) {
				if(!pa[j]) continue;
				tt += query(rt[j], 0, 3 * n, q[i].ll, 3 * n);
			} if(q[i].c == 1) ans -= tt;
			else ans += tt;
		}
	}
	int l=1;
	for(register int i = 1; i <= n; ++i) {
		if(i==1 || a[i].x == a[i-1].x) continue;
		else solve(l,i-1),l=i;
	}
	solve(l, n); printf("%lld\n", ans);
	return 0;
}


#include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
inline int _min(int x, int y) {return x < y ? x : y;}
inline int _max(int x, int y) {return x > y ? x : y;}
inline int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct tnode {
	int lc, rc, c;
} t[30 * 310000]; int cnt, rt[610000];
struct node {
	int x, l, r, ll, rr, c;
} a[610000], q[610000]; int root[610000];
int tim, xx[610000], rr[610000], Q[610000], vis[610000];
int b[610000], pa[610000], hh[610000], pp[610000];
LL ans = 0;

bool cmp(node a, node b) {
	if(a.x == b.x) return a.l < b.l;
	return a.x < b.x;
}

bool cmp1(node a, node b) {
	if(a.x == b.x) return a.c > b.c;
	return a.x < b.x;
}

void Link(int &u, int l, int r, int p, int c) {
	if(!u) u = ++cnt;
	t[u].c++;
	if(l == r) return ;
	int mid = (l + r) / 2;
	if(p <= mid) Link(t[u].lc, l, mid, p, c);
	else Link(t[u].rc, mid + 1, r, p, c);
}

int query(int u1, int l, int r, int ll, int rr) {
	if(l == ll && r == rr) return t[u1].c;
	int mid = (l + r) / 2;
	if(rr <= mid) return query(t[u1].lc, l, mid, ll, rr);
	else if(ll > mid) return query(t[u1].rc, mid + 1, r, ll, rr);
	else return query(t[u1].lc, l, mid, ll, mid) + query(t[u1].rc, mid + 1, r, mid + 1, rr);
}

inline void solve(int l, int r) {
	tim++;
	for(register int i = l; i <= r; ++i) {
		if(vis[a[i].c] != tim) vis[a[i].c] = tim, root[a[i].c] = 0;
		root[a[i].c]++;
	}
	int re=0;
	for(register int i=l;i<=r;++i)
		for(int j=a[i].ll;j<=a[i].rr;j++) if(pa[j]&&vis[j]==tim) re+=root[j];
	ans=ans-(re-(r-l+1))/2-(r-l+1);
}

int main() {
	int n, k; scanf("%d%d", &n, &k);
	for(register int i = 1; i <= n; ++i) {
		xx[i] = read(), rr[i] = read(), Q[i] = read();
		b[i * 3 - 2] = _max(0, xx[i] - rr[i]);
		b[i * 3 - 1] = xx[i];
		b[i * 3] = _min(xx[i] + rr[i], 1e9);
	} sort(b + 1, b + 3 * n + 1);
	for(register int i = 1; i <= n; ++i) {
		a[i].x = lower_bound(b + 1, b + 3 * n + 1, xx[i]) - b;
		a[i].l = lower_bound(b + 1, b + 3 * n + 1, _max(0, xx[i] - rr[i])) - b;
		a[i].r = lower_bound(b + 1, b + 3 * n + 1, _min(xx[i] + rr[i], 1e9)) - b;
	}
	for(register int i = 1; i <= n; ++i) {
		b[i * 3 - 2] = _max(0, Q[i] - k);
		b[i * 3 - 1] = Q[i];
		b[i * 3] = _min(Q[i] + k, 1e9);
	} sort(b + 1, b + 3 * n + 1);
	for(register int i = 1; i <= n; ++i) {
		a[i].c = lower_bound(b + 1, b + 3 * n + 1, Q[i]) - b;
		pa[a[i].c] = 1;
		a[i].ll = lower_bound(b + 1, b + 3 * n + 1, _max(0, Q[i] - k)) - b;
		a[i].rr = lower_bound(b + 1, b + 3 * n + 1, _min(Q[i] + k, 1e9)) - b;
	} sort(a + 1, a + n + 1, cmp);
	int cnt = 0;
	for(register int i = 1; i <= n; ++i) {
		q[++cnt].x = a[i].l - 1; q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 1;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 2;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].r, q[cnt].r = a[i].c, q[cnt].c = 3;
	} sort(q + 1, q + cnt + 1, cmp1);
	int tp1 = 0, tp2 = 0;
	ans = 0; int last = 0;
	for(register int i = 1; i <= cnt; ++i) {
		if(q[i].c == 3) Link(rt[q[i].r], 0, 3 * n, q[i].l, 1);
		else {
			int tt = 0;
			for(register int j = q[i].l; j <= q[i].r; ++j) {
				if(!pa[j]) continue;
				tt += query(rt[j], 0, 3 * n, q[i].ll, 3 * n);
			} if(q[i].c == 1) ans -= tt;
			else ans += tt;
		}
	}
	int l=1;
	for(register int i = 1; i <= n; ++i) {
		if(i==1 || a[i].x == a[i-1].x) continue;
		else solve(l,i-1),l=i;
	}
	solve(l, n); printf("%lld\n", ans);
	return 0;
}


#include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
inline int _min(int x, int y) {return x < y ? x : y;}
inline int _max(int x, int y) {return x > y ? x : y;}
inline int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct tnode {
	int lc, rc, c;
} t[30 * 310000]; int cnt, rt[610000];
struct node {
	int x, l, r, ll, rr, c;
} a[610000], q[610000]; int root[610000];
int tim, xx[610000], rr[610000], Q[610000], vis[610000];
int b[610000], pa[610000], hh[610000], pp[610000];
LL ans = 0;

bool cmp(node a, node b) {
	if(a.x == b.x) return a.l < b.l;
	return a.x < b.x;
}

bool cmp1(node a, node b) {
	if(a.x == b.x) return a.c > b.c;
	return a.x < b.x;
}

void Link(int &u, int l, int r, int p, int c) {
	if(!u) u = ++cnt;
	t[u].c++;
	if(l == r) return ;
	int mid = (l + r) / 2;
	if(p <= mid) Link(t[u].lc, l, mid, p, c);
	else Link(t[u].rc, mid + 1, r, p, c);
}

int query(int u1, int l, int r, int ll, int rr) {
	if(l == ll && r == rr) return t[u1].c;
	int mid = (l + r) / 2;
	if(rr <= mid) return query(t[u1].lc, l, mid, ll, rr);
	else if(ll > mid) return query(t[u1].rc, mid + 1, r, ll, rr);
	else return query(t[u1].lc, l, mid, ll, mid) + query(t[u1].rc, mid + 1, r, mid + 1, rr);
}

inline void solve(int l, int r) {
	tim++;
	for(register int i = l; i <= r; ++i) {
		if(vis[a[i].c] != tim) vis[a[i].c] = tim, root[a[i].c] = 0;
		root[a[i].c]++;
	}
	int re=0;
	for(register int i=l;i<=r;++i)
		for(int j=a[i].ll;j<=a[i].rr;j++) if(pa[j]&&vis[j]==tim) re+=root[j];
	ans=ans-(re-(r-l+1))/2-(r-l+1);
}

int main() {
	int n, k; scanf("%d%d", &n, &k);
	for(register int i = 1; i <= n; ++i) {
		xx[i] = read(), rr[i] = read(), Q[i] = read();
		b[i * 3 - 2] = _max(0, xx[i] - rr[i]);
		b[i * 3 - 1] = xx[i];
		b[i * 3] = _min(xx[i] + rr[i], 1e9);
	} sort(b + 1, b + 3 * n + 1);
	int ln=3*n+1;
	ln=unique(b+1,b+1+ln)-(b+1);
//	for(int i=1;i<=ln;i++)printf("%d ",b[i]);
	for(register int i = 1; i <= n; ++i) {
		a[i].x = lower_bound(b + 1, b + ln, xx[i]) - b;
		a[i].l = lower_bound(b + 1, b + ln, _max(0, xx[i] - rr[i])) - b;
		a[i].r = lower_bound(b + 1, b + ln, _min(xx[i] + rr[i], 1e9)) - b;
	}
	for(register int i = 1; i <= n; ++i) {
		b[i * 3 - 2] = _max(0, Q[i] - k);
		b[i * 3 - 1] = Q[i];
		b[i * 3] = _min(Q[i] + k, 1e9);
	} sort(b + 1, b + 3 * n + 1);
	int op = 3 * n + 1;
	op = unique(b+1,b+1+op)-(b+1);
	for(register int i = 1; i <= n; ++i) {
		a[i].c = lower_bound(b + 1, b + op +1, Q[i]) - b;
		pa[a[i].c] = 1;
		a[i].ll = lower_bound(b + 1, b + op +1, _max(0, Q[i] - k)) - b;
		a[i].rr = lower_bound(b + 1, b + op +1, _min(Q[i] + k, 1e9)) - b;
	} sort(a + 1, a + n + 1, cmp);
	int cnt = 0;
	for(register int i = 1; i <= n; ++i) {
		q[++cnt].x = a[i].l - 1; q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 1;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 2;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].r, q[cnt].r = a[i].c, q[cnt].c = 3;
	} sort(q + 1, q + cnt + 1, cmp1);
	int tp1 = 0, tp2 = 0;
	ans = 0; int last = 0;
	for(register int i = 1; i <= cnt; ++i) {
		if(q[i].c == 3) Link(rt[q[i].r], 1, ln, q[i].l, 1);
		else {
			int tt = 0;
			for(register int j = q[i].l; j <= q[i].r; ++j) {
				if(!pa[j]) continue;
				tt += query(rt[j], 1, ln, q[i].ll, ln);
			} if(q[i].c == 1) ans -= tt;
			else ans += tt;
		}
	}
	int l=1;
	for(register int i = 1; i <= n; ++i) {
		if(i==1 || a[i].x == a[i-1].x) continue;
		else solve(l,i-1),l = i;
	}
	solve(l, n); printf("%lld\n", ans);
	return 0;
}


#include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
inline int _min(int x, int y) {return x < y ? x : y;}
inline int _max(int x, int y) {return x > y ? x : y;}
inline int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct tnode {
	int lc, rc, c;
} t[30 * 310000]; int cnt, rt[610000];
struct node {
	int x, l, r, ll, rr, c;
} a[610000], q[610000]; int root[610000];
int tim, xx[610000], rr[610000], Q[610000], vis[610000];
int b[610000], pa[610000], hh[610000], pp[610000];
LL ans = 0;

bool cmp(node a, node b) {
	if(a.x == b.x) return a.l < b.l;
	return a.x < b.x;
}

bool cmp1(node a, node b) {
	if(a.x == b.x) return a.c > b.c;
	return a.x < b.x;
}

void Link(int &u, int l, int r, int p, int c) {
	if(!u) u = ++cnt;
	t[u].c++;
	if(l == r) return ;
	int mid = (l + r) / 2;
	if(p <= mid) Link(t[u].lc, l, mid, p, c);
	else Link(t[u].rc, mid + 1, r, p, c);
}

int query(int u1, int l, int r, int ll, int rr) {
	if(l == ll && r == rr) return t[u1].c;
	int mid = (l + r) / 2;
	if(rr <= mid) return query(t[u1].lc, l, mid, ll, rr);
	else if(ll > mid) return query(t[u1].rc, mid + 1, r, ll, rr);
	else return query(t[u1].lc, l, mid, ll, mid) + query(t[u1].rc, mid + 1, r, mid + 1, rr);
}

inline void solve(int l, int r) {
	tim++;
	for(register int i = l; i <= r; ++i) {
		if(vis[a[i].c] != tim) vis[a[i].c] = tim, root[a[i].c] = 0;
		root[a[i].c]++;
	}
	int re=0;
	for(register int i=l;i<=r;++i)
		for(int j=a[i].ll;j<=a[i].rr;j++) if(pa[j]&&vis[j]==tim) re+=root[j];
	ans=ans-(re-(r-l+1))/2-(r-l+1);
}

int main() {
	int n, k; scanf("%d%d", &n, &k);
	for(register int i = 1; i <= n; ++i) {
		xx[i] = read(), rr[i] = read(), Q[i] = read();
		b[i * 3 - 2] = _max(0, xx[i] - rr[i]);
		b[i * 3 - 1] = xx[i];
		b[i * 3] = _min(xx[i] + rr[i], 1e9);
	} sort(b + 1, b + 3 * n + 1);
	int ln=3*n+1;
	ln=unique(b+1,b+1+ln)-(b+1);
//	for(int i=1;i<=ln;i++)printf("%d ",b[i]);
	for(register int i = 1; i <= n; ++i) {
		a[i].x = lower_bound(b + 1, b + ln, xx[i]) - b;
		a[i].l = lower_bound(b + 1, b + ln, _max(0, xx[i] - rr[i])) - b;
		a[i].r = lower_bound(b + 1, b + ln, _min(xx[i] + rr[i], 1e9)) - b;
	}
	for(register int i = 1; i <= n; ++i) {
		b[i * 3 - 2] = _max(0, Q[i] - k);
		b[i * 3 - 1] = Q[i];
		b[i * 3] = _min(Q[i] + k, 1e9);
	} sort(b + 1, b + 3 * n + 1);
	for(register int i = 1; i <= n; ++i) {
		a[i].c = lower_bound(b + 1, b + 3 * n + 1, Q[i]) - b;
		pa[a[i].c] = 1;
		a[i].ll = lower_bound(b + 1, b + 3 * n + 1, _max(0, Q[i] - k)) - b;
		a[i].rr = lower_bound(b + 1, b + 3 * n + 1, _min(Q[i] + k, 1e9)) - b;
	} sort(a + 1, a + n + 1, cmp);
	int cnt = 0;
	for(register int i = 1; i <= n; ++i) {
		q[++cnt].x = a[i].l - 1; q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 1;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].ll; q[cnt].r = a[i].rr, q[cnt].ll = a[i].x, q[cnt].c = 2;
		q[++cnt].x = a[i].x, q[cnt].l = a[i].r, q[cnt].r = a[i].c, q[cnt].c = 3;
	} sort(q + 1, q + cnt + 1, cmp1);
	int tp1 = 0, tp2 = 0;
	ans = 0; int last = 0;
	for(register int i = 1; i <= cnt; ++i) {
		if(q[i].c == 3) Link(rt[q[i].r], 0, ln, q[i].l, 1);
		else {
			int tt = 0;
			for(register int j = q[i].l; j <= q[i].r; ++j) {
				if(!pa[j]) continue;
				tt += query(rt[j], 0, ln, q[i].ll, ln);
			} if(q[i].c == 1) ans -= tt;
			else ans += tt;
		}
	}
	int l=1;
	for(register int i = 1; i <= n; ++i) {
		if(i==1 || a[i].x == a[i-1].x) continue;
		else solve(l,i-1),l = i;
	}
	solve(l, n); printf("%lld\n", ans);
	return 0;
}


#include<iostream>
#include<map>
#include<vector>
#include<cmath>
//#include<bits/stdc++.h>
#include<algorithm>
#include<assert.h>
#include<queue>
#include<stdio.h>
#include<set>
#define ll long long
#define mod 1000000007
using namespace std;
vector< pair< int, pair< int, int > > > ip;
map< int, int > comp;
map< int, int > :: iterator it;

int n, k;
ll ans;

vector< pair< int, int > > add[100001];
vector< pair< int, int > > sub[100001];

map< int, int > my[4 * 2 * 100010];

int query(int node, int l, int r, int s, int e, int qa){
    if(s > r || l > e) return 0;
    if(l >= s && r <= e){
        it = my[node].lower_bound(qa - k);
        int cnt = 0;
        for(; it != my[node].end(); ++it){
            if(it->first > qa + k) break;
            cnt += it->second;
        }
        return cnt;
    }

    return query(2 * node, l, (l + r)/2, s, e, qa) + query(2 * node + 1,  (l + r)/2 + 1, r, s, e, qa);
}

void update(int node, int l, int r, int idx, int val){
    if(idx > r || l > idx) return;
    my[node][val]++;
    if(l != r){
        update(2 * node, l,( l + r)/2 , idx, val);
        update(2 * node + 1, (l + r)/2 + 1, r, idx, val);
    }
}

int main(){
    cin.sync_with_stdio(false);
    cin >> n >> k;
    for(int i = 1 ; i <= n ; ++i){
        int x, r, q;
        cin >> x >> r >> q;
        ip.push_back({x, {r, q}});
        comp[x];
        comp[x - r];
    }

    sort(ip.begin(), ip.end());

    int rnk = 0;
    for(it = comp.begin(); it != comp.end(); ++it){
        comp[it->first] = ++rnk;
    }

    for(int i = 1 ; i <= n ; ++i){
        int b = i;
        int e = n;
        int lim = ip[i - 1].first + ip[i - 1].second.first;
        int idx = i;
        while(b <= e){
            int mi = (b + e)/2;
            if(lim >= ip[mi - 1].first){
                idx = mi;
                b = mi + 1;
            }
            else
                e = mi - 1;
        }
        add[i].push_back({comp[ip[i - 1].first], ip[i - 1].second.second});
        sub[idx].push_back({comp[ip[i - 1].first], ip[i - 1].second.second});
    }

    for(int i = n; i >= 1 ; --i){

        for(int j = 0 ; j < sub[i].size() ; ++j){
            int qu = sub[i][j].first;
            int qa = sub[i][j].second;
            ans -= query(1, 1, rnk, 1, qu, qa);
        }
        for(int j = 0 ; j < add[i].size() ; ++j){
            int qu = add[i][j].first;
            int qa = add[i][j].second;
            ans += query(1, 1, rnk, 1, qu, qa);
        }

        update(1, 1, rnk, comp[ip[i - 1].first - ip[i - 1].second.first],  ip[i - 1].second.second);
    }

    cout << ans;

}


#include<bits/stdc++.h>

#define ll long long
#define pii pair<int,int >
#define pll pair<long long ,long long >
#define pbb pair<bool,bool>
#define plll pair<long long,pair<long long ,long long > >
#define sefi second.first
#define sese second.second
#define fi first
#define se second
#define ld long double
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz(x) x.size()
#define cont continue
//#define int long long

const long long  md = 1e9 + 7;
const int Inf = 1e9;
const ll Inf64 = 1e18;
const ll MaxN = 2e5 + 10;
const ll MaxM = 11;
const ld eps = 1e-15;
const ll dx[4] = { 0,1,0,-1 };
const ll dy[4] = { 1,0,-1,0 };
const ll ddx[4] = { 1,1,-1,-1 };
const ll ddy[4] = { 1,-1,1,-1 };
const ld Pi = acos(-1);

using namespace std;

ll gcd(ll a, ll b) {
	while (a) {
		b %= a;
		swap(a, b);
	}
	return b;
}
ll gcdex(ll a, ll mod = md) {
	ll g = mod, r = a, x = 0, y = 1;
	while (r != 0) {
		ll q = g / r;
		g %= r;
		swap(g, r);
		x -= q * y;
		swap(x, y);
	}
	return x < 0 ? x + mod : x;
}

ll binpow(ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1) {
			res *= a;
			res %= md;
		}
		a *= a;
		a %= md;
		n >>= 1;
	}
	return res % md;
}
// /
/// ////
// ///////
struct el {
	ll x, r, q;
};
vector<ll > pos;
el Rb[MaxN];
map<ll, ll > Tree[4 * MaxN];
ll calc(map<ll, ll > & Mp, ll lq, ll rq) {
	auto it = Mp.lower_bound(lq);
	ll res = 0;
	while (it != Mp.end() && it->first <= rq) {
		res += it->second;
		it++;
	}
	return res;
}
ll getSum(ll l, ll r, ll x, ll y,ll lq, ll rq, ll id) {
	if (x <= l && r <= y) {
		return calc(Tree[id], lq, rq);
	}

	ll mid = (l + r) >> 1;
	ll res = 0;
	if (x <= mid) res += getSum( l, mid, x, y, lq, rq,2 * id +1);
	if (mid < y) res += getSum( mid + 1, r, x, y, lq, rq,2 * id +2);
	return res;
}
void insert(ll l, ll r, ll x, ll a, ll id) {
	Tree[id][a] ++;
	if (l == r) {
		return;
	}
	else {
		ll mid = (l + r) / 2;
		if (mid >= x) {
			insert(l, mid, x, a, 2 * id + 1);
		}
		else {
			insert(mid + 1, r, x, a, 2 * id + 2);
		}
	}
}
ll N, K;
ll getL(ll x) {
	ll l = 0, r= pos.size() - 1;
	int res = -1;
	while (l <= r) {
		ll mid = (l + r)/ 2;
		if (pos[mid] >= x) {
			res = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	//assert(res != -1);
	return res;
}

ll getR(ll x) {
	ll l = 0, r = pos.size() - 1;
	ll res = -1;
	while (l <= r) {
		ll mid = (l + r) /2;
		if (pos[mid] <= x) {
			res = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	//assert(res != -1);
	return res;
}
signed main() {
	ios_base::sync_with_stdio(0);
	//ifstream cin("penalty.in");
	//ofstream cout("penalty.out");
	cin.tie(0);
	cout.tie(0);
	cerr.tie(0);
	// //
	cout << fixed, cout.precision(10);
	// //

	cin >> N >> K;
	pos.assign(N, 0);
	for (int i = 0;i < N;i++) {
		cin >> Rb[i].x >> Rb[i].r >> Rb[i].q;
		pos[i] = Rb[i].x;
	}
	sort(all(pos));
	ll m = unique(all(pos)) - pos.begin();
	pos.resize(m);
	sort(Rb, Rb + N, [=](el a, el b) {
		return a.r > b.r;
	});
	ll Ans = 0;
	for (int i = 0;i < N;i++) {
		ll l = getL(Rb[i].x - Rb[i].r);//upper_bound(all(pos), Rb[i].x - Rb[i].r) - pos.begin();
		ll r = getR(Rb[i].x + Rb[i].r); // lower_bound(all(pos), Rb[i].x + Rb[i].r) - pos.begin();
		l = max(l, 0LL);
		r = min(r,(ll)sz(pos) - 1);

		Ans += getSum(0,pos.size() -1 ,l,r, Rb[i].q - K, Rb[i].q + K, 0);
		ll x = getR(Rb[i].x); // upper_bound(all(pos), Rb[i].x) - pos.begin();
		insert(0, sz(pos) - 1, x, Rb[i].q, 0);
	}
	cout << Ans;

	/// ///////////////
	cerr << '\n' << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << '\n';

	return 0;
}
/*

*/


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int k) { sum[k] = sum[lch[k]] + sum[rch[k]]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    int Max = 0;
    for (int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q),
            Max = max(Max, a[i].x + a[i].r);
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, Max);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        int l = max(0, a[i].x - a[i].r);
        int r = a[i].x + a[i].r;
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            ans += query(root[Hashq[j]], 0, Max, l, r);
        }
        int old = root[Hashq[a[i].q]];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, Max, a[i].x, 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq, Hashx;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int rt) { sum[rt] = sum[lson] + sum[rson]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> X;
    for (int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q), X.push_back(a[i].x);
    my_sort_unique(X);
    int N = X.size();
    for (int i = 0; i < n; i++) {
        int pos = lower_bound(X.begin(), X.end(), a[i].x) - X.begin();
        Hashx[a[i].x] = pos;
    }
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, N - 1);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            int l =
                lower_bound(X.begin(), X.end(), a[i].x - a[i].r) - X.begin();
            int r = upper_bound(X.begin(), X.end(), a[i].x + a[i].r) -
                    X.begin() - 1;
            ans += query(root[Hashq[j]], 0, N - 1, l, r);
        }
        int old = root[Hashq[a[i].q]];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, N - 1, Hashx[a[i].x], 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int k) { sum[k] = sum[lch[k]] + sum[rch[k]]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    int Max = 0;
    for (int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q),
            Max = max(Max, a[i].x + a[i].r);
    Max = min((int)(1e9 + 1), Max);
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, Max);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        int l = max(0, a[i].x - a[i].r);
        int r = min(a[i].x + a[i].r, Max);
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            ans += query(root[Hashq[j]], 0, Max, l, r);
        }
        int old = root[Hashq[a[i].q]];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, Max, a[i].x, 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq, Hashx;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int k) { sum[k] = sum[lch[k]] + sum[rch[k]]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> X;
    for (int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q), X.push_back(a[i].x);
    my_sort_unique(X);
    int N = X.size();
    for (int i = 0; i < n; i++) {
        int pos = lower_bound(X.begin(), X.end(), a[i].x) - X.begin();
        Hashx[a[i].x] = pos;
    }
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, N - 1);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            int l =
                lower_bound(X.begin(), X.end(), a[i].x - a[i].r) - X.begin();
            int r = upper_bound(X.begin(), X.end(), a[i].x + a[i].r) -
                    X.begin() - 1;
            ans += query(root[Hashq[j]], 0, N - 1, l, r);
        }
        int old = root[Hashq[a[i].q]];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, N - 1, Hashx[a[i].x], 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq, Hashx;

int root[maxm];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void update(int &k, int l, int r, int p, int x) {
    if (!k) k = ++dfn;
    sum[k] += x;
    if (l == r) return;
    int mid = l + r >> 1;
    if (p <= mid) update(lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[k], mid + 1, r, p, x);
}
ll query(int k, int l, int r, int L, int R) {
    if (!k) return 0;
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int Max = 1e9 + 1;
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q);
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 1;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        int l = max(0, a[i].x - a[i].r);
        int r = min(a[i].x + a[i].r, Max);
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            ans += query(root[Hashq[j]], 0, Max, l, r);
        }
        if (Hashq[a[i].q] == 0) Hashq[a[i].q] = tot++;
        update(root[Hashq[a[i].q]], 0, Max, a[i].x, 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = maxn * 40;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int k) { sum[k] = sum[lch[k]] + sum[rch[k]]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    int Max = 1e9 + 1;
    for (int i = 0; i < n; i++) scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q);
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, Max);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        int l = max(0, a[i].x - a[i].r);
        int r = min(a[i].x + a[i].r, Max);
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            ans += query(root[Hashq[j]], 0, Max, l, r);
        }
        int old = root[Hashq[a[i].q]];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, Max, a[i].x, 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq, rHashq, Hashx, rHashx;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int rt) { sum[rt] = sum[lson] + sum[rson]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> X;
    for (int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q), X.push_back(a[i].x);
    my_sort_unique(X);
    int N = X.size();
    for (int i = 0; i < n; i++) {
        int pos = lower_bound(X.begin(), X.end(), a[i].x) - X.begin();
        Hashx[a[i].x] = pos;
    }
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, N - 1);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            int l =
                lower_bound(X.begin(), X.end(), a[i].x - a[i].r) - X.begin();
            int r = upper_bound(X.begin(), X.end(), a[i].x + a[i].r) -
                    X.begin() - 1;
            ans += query(root[Hashq[j]], 0, N - 1, l, r);
        }
        int old = Hashq[a[i].q];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, N - 1, Hashx[a[i].x], 1);
    }
    printf("%lld\n", ans);
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define lowbit(x) x&-x
#define clr(a) memset(a,0,sizeof(a))
#define _INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define IOS ios::sync_with_stdio(false)
#define fuck(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>pii;
typedef pair<ll, ll>pll;
const int MX = 1e5 + 7;
const int mod = 1e9 + 7;
const int N = 1e9;
const int inf = 0x3f3f3f3f;

int n, k, tot;
int root[MX * 40], ls[MX * 40], rs[MX * 40];
ll sum[MX * 40];
struct node {
	int x, r, q;

	bool operator<(const node &A)const {
		return r > A.r;
	}
} a[MX];
vector<int>h;
int get_id(int x) {
	return lower_bound(h.begin(), h.end(), x) - h.begin() + 1;
}
void update(int p, int l, int r, int &rt) {
	if (!rt) rt = ++tot;
	sum[rt]++;
	if (l == r) return;
	int m = (l + r) >> 1;
	if (p <= m) update(p, l, m, ls[rt]);
	else update(p, m + 1, r, rs[rt]);
}
ll query(int L, int R, int l, int r, int rt) {
	if (!rt) return 0;
	if (L <= l && r <= R) return sum[rt];
	int m = (l + r) >> 1;
	ll res = 0;
	if (L <= m) res += query(L, R, l, m, ls[rt]);
	if (R > m) res += query(L, R, m + 1, r, rs[rt]);
	return res;
}

int main() {
	//FIN;
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++) {
		scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q);
		h.pb(a[i].q); h.pb(a[i].q - k); h.pb(a[i].q + k);
	}
	sort(a + 1, a + n + 1);
	sort(h.begin(), h.end());
	h.erase(unique(h.begin(), h.end()), h.end());
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		int st = a[i].q - k, ed = a[i].q + k;
		st = get_id(st); ed = get_id(ed);
		for (int j = st; j <= ed; j++) {
			if (!root[j]) continue;
			int L = max(0, a[i].x - a[i].r), R = min(N, a[i].x + a[i].r);
			ans += query(L, R, 0, N, root[j]);
		}
		a[i].q = get_id(a[i].q);
		update(a[i].x, 0, N, root[a[i].q]);
	}
	printf("%lld\n",ans);
	//cout << ans << endl;

}

---------------------

本文来自 破晓③ 的CSDN 博客 ，全文地址请点击：https://blog.csdn.net/lee_w_j__/article/details/82823266?utm_source=copy


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq, Hashx;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int k) { sum[k] = sum[lch[k]] + sum[rch[k]]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> X;
    for (int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q), X.push_back(a[i].x);
    my_sort_unique(X);
    int N = X.size();
    for (int i = 0; i < n; i++) {
        int pos = lower_bound(X.begin(), X.end(), a[i].x) - X.begin();
        Hashx[a[i].x] = pos;
    }
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, N - 1);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        int l = lower_bound(X.begin(), X.end(), a[i].x - a[i].r) - X.begin();
        int r =
            upper_bound(X.begin(), X.end(), a[i].x + a[i].r) - X.begin() - 1;
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            ans += query(root[Hashq[j]], 0, N - 1, l, r);
        }
        int old = root[Hashq[a[i].q]];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, N - 1, Hashx[a[i].x], 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = (maxn << 2) + 2e6;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq;

int root[maxn];
ll sum[maxm];
int lch[maxm], rch[maxm];
int dfn;
void build(int &k, int l, int r) {
    k = ++dfn;
    sum[k] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    build(lch[k], l, mid);
    build(rch[k], mid + 1, r);
}
inline void pushup(int k) { sum[k] = sum[lch[k]] + sum[rch[k]]; }
inline void newnode(int old, int k) {
    sum[k] = sum[old];
    lch[k] = lch[old];
    rch[k] = rch[old];
}
void update(int old, int &k, int l, int r, int p, int x) {
    k = ++dfn;
    newnode(old, k);
    if (l == r) {
        sum[k] += x;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) update(lch[old], lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[old], rch[k], mid + 1, r, p, x);
    pushup(k);
}
ll query(int k, int l, int r, int L, int R) {
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int n, m;
    scanf("%d%d", &n, &m);
    int Max = 0;
    for (int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q),
            Max = max(Max, a[i].x + a[i].r);
    Max = min((int)(1e9 + 1), Max);
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 0;
    ll ans = 0;
    build(root[tot++], 0, Max);
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        int l = max(0, a[i].x - a[i].r);
        int r = a[i].x + a[i].r;
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            ans += query(root[Hashq[j]], 0, Max, l, r);
        }
        int old = root[Hashq[a[i].q]];
        Hashq[a[i].q] = tot++;
        update(old, root[Hashq[a[i].q]], 0, Max, a[i].x, 1);
    }
    printf("%lld\n", ans);
}


// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int maxm = maxn * 40;
struct Node {
    int x, r, q;
    bool operator<(const Node &A) const { return r > A.r; }
};
Node a[maxn];
map<int, int> Hashq;
ll sum[maxm];
int lch[maxm], rch[maxm], root[maxn];
int dfn;
void update(int &k, int l, int r, int p, int x) {
    if (!k) k = ++dfn;
    sum[k] += x;
    if (l == r) return;
    int mid = l + r >> 1;
    if (p <= mid) update(lch[k], l, mid, p, x);
    if (mid + 1 <= p) update(rch[k], mid + 1, r, p, x);
}
ll query(int k, int l, int r, int L, int R) {
    if (!k) return 0;
    if (L <= l && R >= r) return sum[k];
    int mid = l + r >> 1;
    ll ret = 0;
    if (L <= mid) ret += query(lch[k], l, mid, L, R);
    if (mid + 1 <= R) ret += query(rch[k], mid + 1, r, L, R);
    return ret;
}
int main() {
    //	/*
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    //	*/
    std::ios::sync_with_stdio(false);
    int Max = 1e9 + 1;
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q);
    sort(a, a + n);  // r
    dfn = 0;
    int tot = 1;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int base = max(0, a[i].q - m);
        int top = a[i].q + m;
        int l = max(0, a[i].x - a[i].r);
        int r = min(a[i].x + a[i].r, Max);
        for (int j = base; j <= top; j++) {
            if (Hashq.find(j) == Hashq.end()) continue;
            ans += query(root[Hashq[j]], 0, Max, l, r);
        }
        if (Hashq[a[i].q] == 0) Hashq[a[i].q] = tot++;
        update(root[Hashq[a[i].q]], 0, Max, a[i].x, 1);
    }
    printf("%lld\n", ans);
}


#include <bits/stdc++.h>
#define  pb push_back
#define  pf push_front
#define  S second
#define  F first
#define ra ragham
#define ll long long
#define ld long double
//#define ll int
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;
typedef tree<pair<int,int>,null_type,less<pair<int,int> >,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef pair<ll,ll> pii ;
typedef pair<long double,ll> pldi ;
typedef pair<long double,pii> pldii ;
typedef pair<int,char> cii ;
typedef pair<ll,pii> piii ;
//setprecision(8)
//ios_base::sync_with_stdio(false);
//ifstream fin ("in.txt");
//ofstream fout ("out.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout;
//string mabda1  = "abcdefghijklmnopqrstuvwxyz";
//string mabda2  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int maxn = 1e5+100 ;
piii a[maxn] ;
ll n , k ;
ll ans ;
ll q = 1e10;
map<int,order_set> m;

int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(ll y = 0 ; y < n ; y++)
    {
        cin>>a[y].S.F>>a[y].F>>a[y].S.S;
    }
    sort(a,a+n);
    for(ll y = n-1 ; y >= 0 ; y--)
    {
        for(ll i = a[y].S.S-k ; i <= a[y].S.S+k ; i++)
        {
            ans+= m[i].order_of_key(pii(a[y].S.F+a[y].F,q))-m[i].order_of_key(pii(a[y].S.F-a[y].F,q));
        }
        m[a[y].S.S].insert(pii(a[y].S.F,y));
    }

    cout<<ans;

}


#include <bits/stdc++.h>
#define  pb push_back
#define  pf push_front
#define  S second
#define  F first
#define ra ragham
#define ll long long
#define ld long double
//#define ll int
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;
typedef tree<pair<int,int>,null_type,less<pair<int,int> >,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef pair<ll,ll> pii ;
typedef pair<long double,ll> pldi ;
typedef pair<long double,pii> pldii ;
typedef pair<int,char> cii ;
typedef pair<ll,pii> piii ;
//setprecision(8)
//ios_base::sync_with_stdio(false);
//ifstream fin ("in.txt");
//ofstream fout ("out.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout;
//string mabda1  = "abcdefghijklmnopqrstuvwxyz";
//string mabda2  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int maxn = 1e5+100 ;
piii a[maxn] ;
ll n , k ;
ll ans ;
ll q = 1e10;
map<int,order_set> m;

int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(ll y = 0 ; y < n ; y++)
    {
        cin>>a[y].S.F>>a[y].F>>a[y].S.S;
    }
    sort(a,a+n);
    for(ll y = n-1 ; y >= 0 ; y--)
    {
        for(ll i = a[y].S.S-k ; i <= a[y].S.S+k ; i++)
        {
            ans+= m[i].order_of_key(pii(a[y].S.F+a[y].F,0))-m[i].order_of_key(pii(a[y].S.F-a[y].F,0));
        }
        m[a[y].S.S].insert(pii(a[y].S.F,y));
    }

    cout<<ans;

}


#include <bits/stdc++.h>
#define  pb push_back
#define  pf push_front
#define  S second
#define  F first
#define ra ragham
#define ll long long
#define ld long double
//#define ll int
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;
typedef tree<pair<int,int>,null_type,less<pair<int,int> >,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef pair<ll,ll> pii ;
typedef pair<long double,ll> pldi ;
typedef pair<long double,pii> pldii ;
typedef pair<int,char> cii ;
typedef pair<ll,pii> piii ;
//setprecision(8)
//ios_base::sync_with_stdio(false);
//ifstream fin ("in.txt");
//ofstream fout ("out.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout;
//string mabda1  = "abcdefghijklmnopqrstuvwxyz";
//string mabda2  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int maxn = 1e5+100 ;
piii a[maxn] ;
ll n , k ;
ll ans ;
ll q = 1e10;
map<int,order_set> m;

int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(ll y = 0 ; y < n ; y++)
    {
        cin>>a[y].S.F>>a[y].F>>a[y].S.S;
    }
    sort(a,a+n);
    for(ll y = n-1 ; y >= 0 ; y--)
    {
        for(ll i = a[y].S.S-k ; i <= a[y].S.S+k ; i++)
        {
            if (m.count(i))
                ans+= m[i].order_of_key(pii(a[y].S.F+a[y].F+1,-1))-m[i].order_of_key(pii(a[y].S.F-a[y].F,-1));
        }
        m[a[y].S.S].insert(pii(a[y].S.F,y));
    }

    cout<<ans;

}


#include <bits/stdc++.h>
#define  pb push_back
#define  pf push_front
#define  S second
#define  F first
#define ra ragham
#define ll long long
#define ld long double
//#define ll int
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;
typedef tree<pair<int,int>,null_type,less<pair<int,int> >,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef pair<ll,ll> pii ;
typedef pair<long double,ll> pldi ;
typedef pair<long double,pii> pldii ;
typedef pair<int,char> cii ;
typedef pair<ll,pii> piii ;
//setprecision(8)
//ios_base::sync_with_stdio(false);
//ifstream fin ("in.txt");
//ofstream fout ("out.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout;
//string mabda1  = "abcdefghijklmnopqrstuvwxyz";
//string mabda2  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int maxn = 1e5+100 ;
piii a[maxn] ;
ll n , k ;
ll ans ;
ll q = 1e10;
map<int,order_set> m;

int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(ll y = 0 ; y < n ; y++)
    {
        cin>>a[y].S.F>>a[y].F>>a[y].S.S;
    }
    sort(a,a+n);
    for(ll y = n-1 ; y >= 0 ; y--)
    {
        for(ll i = a[y].S.S-k ; i <= a[y].S.S+k ; i++)
        {
            ans+= m[i].order_of_key(pii(a[y].S.F+a[y].F+1,-1))-m[i].order_of_key(pii(a[y].S.F-a[y].F,-1));
        }
        m[a[y].S.S].insert(pii(a[y].S.F,y));
    }

    cout<<ans;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int,int>
#define INF 100000000000000000
#define EPS 1e-9
#define all(v) v.begin(),v.end()
#define sq(x) ((x)*(x))

const int MOD = 1000*1000*1000 + 7;
const int NMAX = 1000*100 + 11;

typedef tree<pii,null_type,less <pii>,rb_tree_tag,tree_order_statistics_node_update>
ordered_set;

int n,k,x,r,q;

unordered_map<int,ordered_set> IQ;
vector<pair<pii,pii> > pts;

int32_t main(){ fio;
    cin>>n>>k;

    for(int i=0;i<n;i++)
    {
        cin>>x>>r>>q;
        IQ[q].insert({x,i+1});

        pts.pb(mp(mp(r,x),mp(q,i)));
    }

    sort(pts.begin(),pts.end());
    int ans = 0;

    int x,r,q,i;

    for(auto pt:pts)
    {
        r = pt.first.first;
        x = pt.first.second;
        q = pt.second.first;
        i = pt.second.second;

        IQ[q].erase({x,i+1});

        int b = IQ[q].order_of_key({x+r+1,-1e9-10});
        int a = IQ[q].order_of_key({x-r,-1e9-10});
        ans += (b-a);

        for(int j=1;j<=k;j++)
        {
            if(IQ.find(q-j) != IQ.end())
            {
                int b = IQ[q-j].order_of_key({x+r+1,-1e9-10});
                int a = IQ[q-j].order_of_key({x-r,-1e9-10});
                ans += (b-a);
            }

            if(IQ.find(q+j) != IQ.end())
            {
                b = IQ[q+j].order_of_key({x+r+1,-1e9-10});
                a = IQ[q+j].order_of_key({x-r,-1e9-10});
                ans += (b-a);
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int,int>
#define INF 100000000000000000
#define EPS 1e-9
#define all(v) v.begin(),v.end()
#define sq(x) ((x)*(x))

const int MOD = 1000*1000*1000 + 7;
const int NMAX = 1000*100 + 11;

typedef tree<pii,null_type,less <pii>,rb_tree_tag,tree_order_statistics_node_update>
ordered_set;

int n,k,x[NMAX],r[NMAX],q[NMAX];

unordered_map<int,ordered_set> IQ;
vector<pair<pii,pii> > pts;

int32_t main(){ fio;
    cin>>n>>k;

    for(int i=0;i<n;i++)
    {
        cin>>x[i]>>r[i]>>q[i];
        IQ[q[i]].insert({x[i],i+1});

        pts.pb(mp(mp(r[i],x[i]),mp(q[i],i)));
    }

    sort(pts.begin(),pts.end());
    int ans = 0;

    for(int loop=0;loop<n;loop++)
    {
        int i = pts[loop].second.second;

        IQ[q[i]].erase({x[i],i+1});
        //cout<<i<<" ";
        //IQ[q[i]].insert({-1,-1});

        int b = IQ[q[i]].order_of_key({x[i]+r[i]+1,-1e9-10});
        int a = IQ[q[i]].order_of_key({x[i]-r[i],-1e9-10});
        ans += (b-a);

        //cout<<i<<" ";
        //cout<<i<<" "<<ans<<"\n";

        for(int j=1;j<=k;j++)
        {
            //IQ[q[i]-j].insert({-1,-1});

            int b = IQ[q[i]-j].order_of_key({x[i]+r[i]+1,-1e9-10});
            int a = IQ[q[i]-j].order_of_key({x[i]-r[i],-1e9-10});
            ans += (b-a);

            //cout<<i<<" "<<ans<<"\n";
            //IQ[q[i]+j].insert({-1,-1});

            b = IQ[q[i]+j].order_of_key({x[i]+r[i]+1,-1e9-10});
            a = IQ[q[i]+j].order_of_key({x[i]-r[i],-1e9-10});
            ans += (b-a);

            //if(i==0) cout<<j<<" "<<ans<<"\n";
        }

        //cout<<i<<" "<<ans<<"\n";
    }
    cout<<ans<<"\n";
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int,int>
#define INF 100000000000000000
#define EPS 1e-9
#define all(v) v.begin(),v.end()
#define sq(x) ((x)*(x))

const int MOD = 1000*1000*1000 + 7;
const int NMAX = 1000*100 + 11;

typedef tree<pii,null_type,less <pii>,rb_tree_tag,tree_order_statistics_node_update>
ordered_set;

int n,k,x,r,q;

unordered_map<int,ordered_set> IQ;
vector<pair<pii,pii> > pts;

int32_t main(){ fio;
    cin>>n>>k;

    for(int i=0;i<n;i++)
    {
        cin>>x>>r>>q;
        IQ[q].insert({x,i+1});

        pts.pb(mp(mp(r,x),mp(q,i)));
    }

    sort(pts.begin(),pts.end());
    int ans = 0;

    int x,r,q,i;

    for(auto pt:pts)
    {
        r = pt.first.first;
        x = pt.first.second;
        q = pt.second.first;
        i = pt.second.second;

        IQ[q].erase({x,i+1});
        //cout<<i<<" ";
        //IQ[q[i]].insert({-1,-1});

        int b = IQ[q].order_of_key({x+r+1,-1e9-10});
        int a = IQ[q].order_of_key({x-r,-1e9-10});
        ans += (b-a);

        //cout<<i<<" ";
        //cout<<i<<" "<<ans<<"\n";

        for(int j=1;j<=k;j++)
        {
            //IQ[q[i]-j].insert({-1,-1});
            if(IQ[q-j].size())
            {
                int b = IQ[q-j].order_of_key({x+r+1,-1e9-10});
                int a = IQ[q-j].order_of_key({x-r,-1e9-10});
                ans += (b-a);
            }

            //cout<<i<<" "<<ans<<"\n";
            //IQ[q[i]+j].insert({-1,-1});

            if(IQ[q+j].size())
            {
                b = IQ[q+j].order_of_key({x+r+1,-1e9-10});
                a = IQ[q+j].order_of_key({x-r,-1e9-10});
                ans += (b-a);
            }

            //if(i==0) cout<<j<<" "<<ans<<"\n";
        }

        //cout<<i<<" "<<ans<<"\n";
    }
    cout<<ans<<"\n";
    return 0;
}