#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});
        int a = IQ[q[i]].order_of_key({x[i]-r[i],1e9});
        ans += (b-a);

       // if(i==0) cout<<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});
            int a = IQ[q[i]-j].order_of_key({x[i]-r[i],1e9});
            ans += (b-a);

            //IQ[q[i]+j].insert({-1,-1});

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

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

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


#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>

using namespace std;
bool comp(const tuple<int,int,int> &lhs, const tuple<int,int,int> &rhs )
{
	return get<2>(lhs) < get<2>(rhs);
}

int main()
{
	int n, k, x, r, s;
	cin >> n >> k;
	tuple<int, int, int> robots[n];
	int seg_size = static_cast<int>(sqrt(n));
	for (int i = 0; i<n; i++) {
		cin >> x >> r >> s;
		robots[i] = {x, r, s};
	}
	int ranges[seg_size+1];
	sort(robots, robots+n);
	for(int i=0; i<n; i+= seg_size) {
		ranges[i/seg_size] = get<0>(robots[i]);
		if(i+seg_size <n)
			sort(robots+i, robots+i+seg_size, comp);
		else
			sort(robots+i, robots+n, comp);
	}
	long long ans=0;
	for(int i=0; i<n; i++) {
		int j=0, index;
		auto lowerfake= robots[i], upperfake = robots[i];
		get<2>(lowerfake)-=k;
		get<2>(upperfake)+=k;
		for(index=0; j+seg_size<n; j+=seg_size, index++) {
			if (abs(ranges[index+1]-get<0>(robots[i]))<=get<1>(robots[i])) {
				auto left = lower_bound(robots+j, robots+j+seg_size, lowerfake, comp);
				auto right = lower_bound(robots+j, robots+j+seg_size, upperfake, comp);
				ans += right-left;
			}
			else if (abs(ranges[index]-get<0>(robots[i]))<=get<1>(robots[i])) {
				for (int temp = 0; temp<seg_size; temp++) {
					if (abs(get<0>(robots[temp+j])-get<0>(robots[i]))<=get<1>(robots[i])) {
						if (abs(get<2>(robots[temp+j])-get<2>(robots[i]))<=k) {
							ans++;
						}
					}
				}
			}
		}
		if (j>=n)
			continue;
		for (; j<n; j++) {
			if(abs(get<0>(robots[j]) -get<0>(robots[i])) <= get<1>(robots[i])) {
				if(abs(get<2>(robots[j]) -get<2>(robots[i])) <= k) {
					ans++;
				}
			}
		}
		//cout << get<0>(robots[i]) << ' ' << ans << endl;
	}
	cout << ans-n;
}


#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});
            if(IQ[q[i]-j].size())
            {
                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});

            if(IQ[q[i]+j].size())
            {
                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>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st2;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st2.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st2.begin();it!=st2.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	multiset<int> st;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1)st.insert(f[i][j].first.second);
			else if(f[i][j].second == 0)st.erase(st.lower_bound(f[i][j].first.second));
		}
		// cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			// cout << i << ' ' << j << endl;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1)st.insert(f[j][x].first.second);
				else if(f[j][x].second == 0)st.erase(st.lower_bound(f[j][x].first.second));
			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				// cout << tmp[x].second << ' ' << tmp[x].first.second << endl;
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
				}
				if(tmp[x].second == -1)st.insert(tmp[x].first.second);
				else if(tmp[x].second == 0)st.erase(st.lower_bound(tmp[x].first.first));
			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st2;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st2.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st2.begin();it!=st2.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	multiset<int> st;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1)st.insert(f[i][j].first.second);
			else if(f[i][j].second == 0)st.erase(st.find(f[i][j].first.second));
		}
		// cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			// cout << i << ' ' << j << endl;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1)st.insert(f[j][x].first.second);
				else if(f[j][x].second == 0)st.erase(st.find(f[j][x].first.second));
			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				// cout << tmp[x].second << ' ' << tmp[x].first.second << endl;
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
				}
				if(tmp[x].second == -1)st.insert(tmp[x].first.second);
				else if(tmp[x].second == 0)st.erase(st.find(tmp[x].first.first));
			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st.begin();it!=st.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
		f[id[q[i]]].push_back({{x[i] + r[i],x[i]},1});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1)st.insert(f[i][j].first.second);
			else if(f[i][j].second == 1)st.erase(f[i][j].first.second);
		}
		cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1)st.insert(f[j][x].first.second);
				else if(f[j][x].second == 1)st.erase(f[j][x].first.second);
			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
					// cout << tmp[x].second << ' ' << tmp[x].first.first << endl;
				}
				if(tmp[x].second == -1)st.insert(tmp[x].first.second);
				else if(tmp[x].second == 1)st.erase(tmp[x].first.second);
			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<int,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st.begin();it!=st.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({x[i] - r[i],-1});
		f[id[q[i]]].push_back({x[i],1});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == -1)m += cur;
			cur -= f[i][j].second;
		}
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == -1)m2 += cur2;
				cur2 -= f[j][x].second;
			}
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			while(1){
				if(id1 == f[i].size() && id2 == f[j].size())break;
				if(id1 == f[i].size()){
					if(f[j][id2].second == -1)sum += cnt;
					cnt -= f[j][id2++].second;
				}else if(id2 == f[j].size()){
					if(f[i][id1].second == -1)sum += cnt;
					cnt -= f[i][id1++].second;
				}else if(f[j][id2].first < f[i][id1].first){
					if(f[j][id2].second == -1)sum += cnt;
					cnt -= f[j][id2++].second;
				}else{
					if(f[i][id1].second == -1)sum += cnt;
					cnt -= f[i][id1++].second;
				}
			}
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st2;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st2.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st2.begin();it!=st2.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	multiset<int> st;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1)st.insert(f[i][j].first.second);
			else if(f[i][j].second == 0)st.erase(f[i][j].first.first);
		}
		// cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1)st.insert(f[j][x].first.second);
				else if(f[j][x].second == 0)st.erase(f[j][x].first.first);
			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					// cout << ' ' << tmp[x].first.first << ' ' << distance(st.begin(),it) << endl;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
					// cout << tmp [x].second << ' ' << tmp[x].first.first << endl;
				}
				if(tmp[x].second == -1)st.insert(tmp[x].first.second);
				else if(tmp[x].second == 0)st.erase(tmp[x].first.first);
			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st.begin();it!=st.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
		f[id[q[i]]].push_back({{x[i] + r[i],x[i]},1});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1)st.insert(f[i][j].first.second);
			else if(f[i][j].second == 1)st.erase(f[i][j].first.second);
		}
		// cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1)st.insert(f[j][x].first.second);
				else if(f[j][x].second == 1)st.erase(f[j][x].first.second);
			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
					// cout << tmp[x].second << ' ' << tmp[x].first.first << endl;
				}
				if(tmp[x].second == -1)st.insert(tmp[x].first.second);
				else if(tmp[x].second == 1)st.erase(tmp[x].first.second);
			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<int,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st.begin();it!=st.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		q[i] = id[q[i]];
		f[q[i]].push_back({x[i] - r[i],-1});
		f[q[i]].push_back({x[i] + r[i] + 1,1});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == -1)m += cur;
			cur -= f[i][j].second;
		}
		ans += m;
		for(int j=i+1;j<n;j++){
			if(abs(fr[j]-fr[i]) > k)break;
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == -1)m2 += cur2;
				cur2 -= f[j][x].second;
			}
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			while(1){
				if(id1 == f[i].size() && id2 == f[j].size())break;
				if(id1 == f[i].size()){
					if(f[j][id2].second == -1)sum += cnt;
					cnt -= f[j][id2++].second;
				}else if(id2 == f[j].size()){
					if(f[i][id1].second == -1)sum += cnt;
					cnt -= f[i][id1++].second;
				}else if(f[j][id2].first < f[i][id1].first){
					if(f[j][id2].second == -1)sum += cnt;
					cnt -= f[j][id2++].second;
				}else{
					if(f[i][id1].second == -1)sum += cnt;
					cnt -= f[i][id1++].second;
				}
			}
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
ll ans;
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		for(int j=0;j<i;j++){
			if(abs(x[i]-x[j]) <= r[i] && abs(x[i]-x[j]) <= r[j] && abs(q[i]-q[j])<=k)ans++;
		}
	}

	printf("%lld\n", ans);
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

#define debug(x) '[' << #x << " is: " << x << "] "
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 3e6;

map<int,int> mp;
indexed_set mech[nax];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.precision(10);
	cout << fixed;
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n,k;
	cin>>n>>k;
	vector<pair<int,pair<int,int>>> r;
	set<int> st;
	for(int i=0;i<n;i++){
		int x,rad,q;
		cin>>x>>rad>>q;
		r.push_back({-rad,{x,q}});
		st.insert(q);
	}
	int cnt = 0,lst = *st.begin();
	for(auto it : st){
		int dif = it - lst;
		if(dif > 20){
			dif = 21;
		}
		cnt+=dif;
		mp[it] = cnt;
		lst = it;
	}
	sort(r.begin(),r.end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		int x,rad,q;
		x = r[i].second.first;
		rad = -r[i].first;
		q = mp[r[i].second.second];
		// cout << q << ' ' << r[i].second.second << endl;
		for(int j=max(0,q-k);j<=q+k;j++){
			// if(q == 9)cout << j << endl;
			if(mech[j].empty())continue;
			ll fr = x-rad;
			ll to = x+rad;
			// cout << fr << ' ' << to << endl;
			ans +=  mech[j].order_of_key(to+1) - mech[j].order_of_key(fr);
		}
		mech[q].insert(x);
	}
	cout << ans << endl;
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

#define debug(x) '[' << #x << " is: " << x << "] "
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

map<int,indexed_set> mech;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.precision(10);
	cout << fixed;
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n,k;
	cin>>n>>k;
	vector<pair<int,pair<int,int>>> r;
	for(int i=0;i<n;i++){
		int x,rad,q;
		cin>>x>>rad>>q;
		r.push_back({-rad,{x,q}});
	}
	sort(r.begin(),r.end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		int x,rad,q;
		x = r[i].second.first;
		rad = -r[i].first;
		q = r[i].second.second;
		for(int j=q-k;j<=q+k;j++){
			indexed_set tmp = mech[j];
			if(tmp.empty())continue;
			ll fr = x-rad;
			ll to = x+rad;
			ans +=  tmp.order_of_key(to+1) - tmp.order_of_key(fr);
		}
		mech[q].insert(x);
	}
	cout << ans << endl;
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

#define debug(x) '[' << #x << " is: " << x << "] "
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

unordered_map<int,indexed_set> mech;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.precision(10);
	cout << fixed;
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n,k;
	cin>>n>>k;
	vector<pair<int,pair<int,int>>> r;
	for(int i=0;i<n;i++){
		int x,rad,q;
		cin>>x>>rad>>q;
		r.push_back({-rad,{x,q}});
	}
	sort(r.begin(),r.end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		int x,rad,q;
		x = r[i].second.first;
		rad = -r[i].first;
		q = r[i].second.second;
		for(int j=q-k;j<=q+k;j++){
			ll fr = x-rad;
			ll to = x+rad;
			ans +=  mech[j].order_of_key(to+1) - mech[j].order_of_key(fr);
		}
		mech[q].insert(x);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st.begin();it!=st.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1)st.insert(f[i][j].first.second);
			else if(f[i][j].second == 0)st.erase(f[i][j].first.second);
		}
		// cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1)st.insert(f[j][x].first.second);
				else if(f[j][x].second == 0)st.erase(f[j][x].first.second);
			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					// cout << ' ' << tmp[x].first.first << ' ' << distance(st.begin(),it) << endl;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
					// cout << tmp [x].second << ' ' << tmp[x].first.first << endl;
				}
				if(tmp[x].second == -1)st.insert(tmp[x].first.second);
				else if(tmp[x].second == 0)st.erase(tmp[x].first.first);
			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<int,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st.begin();it!=st.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({x[i] - r[i],-1});
		f[id[q[i]]].push_back({x[i],1});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == -1)m += cur;
			cur -= f[i][j].second;
		}
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == -1)m2 += cur2;
				cur2 -= f[j][x].second;
			}
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			while(1){
				if(id1 == f[i].size() && id2 == f[j].size())break;
				if(id1 == f[i].size()){
					if(f[j][id2].second == -1)sum += cnt;
					cnt -= f[j][id2++].second;
				}else if(id2 == f[j].size()){
					if(f[i][id1].second == -1)sum += cnt;
					cnt -= f[i][id1++].second;
				}else if(f[j][id2].first == f[i][id1].first && f[i][id1].second == -1){
					if(f[i][id1].second == -1)sum += cnt;
					cnt -= f[i][id1++].second;
				}else if(f[j][id2].first == f[i][id1].first && f[j][id2].second == -1){
					if(f[j][id2].second == -1)sum += cnt;
					cnt -= f[j][id2++].second;
				}else if(f[j][id2].first < f[i][id1].first){
					if(f[j][id2].second == -1)sum += cnt;
					cnt -= f[j][id2++].second;
				}else{
					if(f[i][id1].second == -1)sum += cnt;
					cnt -= f[i][id1++].second;
				}
			}
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

#define debug(x) '[' << #x << " is: " << x << "] "
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

map<int,vector<int>> mech;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.precision(10);
	cout << fixed;
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n,k;
	cin>>n>>k;
	vector<pair<int,pair<int,int>>> r;
	for(int i=0;i<n;i++){
		int x,rad,q;
		cin>>x>>rad>>q;
		r.push_back({rad,{x,q}});
	}
	sort(r.begin(),r.end());
	ll ans = 0;
	for(int i=0;i<n;i++){
		int x,rad,q;
		x = r[i].second.first;
		rad = r[i].first;
		q = r[i].second.second;
		for(int j=q-k;j<=q+k;j++){
			vector<int> tmp = mech[j];
			if(tmp.empty())continue;
			ll fr = x-rad;
			ll to = x+rad;
			ans += upper_bound(tmp.begin(),tmp.end(),to) - tmp.begin() - 1;
			ans -= lower_bound(tmp.begin(),tmp.end(),fr) - tmp.begin() - 1;
		}
		mech[q].push_back(x);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st2;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st2.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st2.begin();it!=st2.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	multiset<int> st;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1)st.insert(f[i][j].first.second);
			else if(f[i][j].second == 0)st.erase(f[i][j].first.second);
		}
		// cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1)st.insert(f[j][x].first.second);
				else if(f[j][x].second == 0)st.erase(f[j][x].first.second);
			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					// cout << ' ' << tmp[x].first.first << ' ' << distance(st.begin(),it) << endl;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
					// cout << tmp [x].second << ' ' << tmp[x].first.first << endl;
				}
				if(tmp[x].second == -1)st.insert(tmp[x].first.second);
				else if(tmp[x].second == 0)st.erase(tmp[x].first.first);
			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+50;
const int M = 15000000;
const int oo = 1e9;
const int mod = 1e9+7;

int x[N],r[N],q[N];
map<int,int> id,fr;
unordered_map<int,int> mp;
vector<pair<pair<int,int>,int>> f[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	set<int> st2;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		st2.insert(q[i]);
	}
	int cnt = 0;
	for(auto it = st2.begin();it!=st2.end();it++)id[*it] = cnt++;
	for(int i=0;i<n;i++){
		fr[id[q[i]]] = q[i];
		f[id[q[i]]].push_back({{x[i] - r[i],x[i]},-1});
		f[id[q[i]]].push_back({{x[i],x[i]+r[i]},0});
	}
	for(int i=0;i<n;i++)sort(f[i].begin(),f[i].end());
	ll ans = 0;
	set<int> st;
	for(int i=0;i<n;i++){
		if(!f[i].size())continue;
		st.clear();
		ll cur = 0,m = 0,sum = 0,cnt=0;
		for(int j = 0;j<f[i].size();j++){
			if(f[i][j].second == 0){
				auto it = st.upper_bound(f[i][j].first.second);
				if(it==st.begin())continue;
				it--;
				if(it==st.begin())continue;
				m += distance(st.begin(),it);
			}else{
				// cout << f[i][j].second << ' ' << f[i][j].first.first << endl;
			}
			if(f[i][j].second == -1){
				mp[f[i][j].second]++;
				st.insert(f[i][j].first.second);
			}
			else if(f[i][j].second == 0){
				mp[f[i][j].second]--;
				if(!mp[f[i][j].second])st.erase(f[i][j].first.second);
			}
		}
		// cout << m << endl;
		ans += m;
		for(int j=i+1;j<n;j++){
			if(!f[j].size())continue;
			if(abs(fr[j]-fr[i]) > k)break;
			// cout << i << ' ' << j << endl;
			st.clear();
			ll cur2 = 0,m2 = 0;
			for(int x = 0;x<f[j].size();x++){
				if(f[j][x].second == 0){
					auto it = st.upper_bound(f[j][x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					m2 += distance(st.begin(),it);
				}
				if(f[j][x].second == -1){
					mp[f[j][x].second]++;
					st.insert(f[j][x].first.second);
				}
				else if(f[j][x].second == 0){
					mp[f[j][x].second]--;
					if(!mp[f[j][x].second])st.erase(f[j][x].first.second);
				}

			}
			st.clear();
			vector<pair<pair<int,int>,int>> tmp = f[i];
			for(int x=0;x<f[j].size();x++)tmp.push_back(f[j][x]);
			sort(tmp.begin(),tmp.end());
			sum = 0;
			cnt = 0;
			int id1=0,id2=0;
			for(int x = 0;x<tmp.size();x++){
				// cout << tmp[x].second << ' ' << tmp[x].first.second << endl;
				if(tmp[x].second == 0){
					auto it = st.upper_bound(tmp[x].first.second);
					if(it==st.begin())continue;
					it--;
					if(it==st.begin())continue;
					sum += distance(st.begin(),it);
				}else{
				}
				if(tmp[x].second == -1){
					mp[tmp[x].second]++;
					st.insert(tmp[x].first.second);
				}
				else if(tmp[x].second == 0){
					mp[tmp[x].second]--;
					if(!mp[tmp[x].second])st.erase(tmp[x].first.second);
				}

			}
			// cout << sum << ' ' << m << ' ' << m2 << endl;
			ans += sum - m - m2;
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
 typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

typedef long long ll;
const int N = 1e5 + 1;

struct Robo {
    int x, r, q, id;

    Robo(int _x = 0, int _r = 0, int _q = 0, int id = 0) :
        x(_x), r(_r), q(_q), id(id) {}

    bool operator < (const Robo& b) const {
        return r > b.r || (r == b.r && id < b.id);
    }
};

Robo robo[N];
map<int, ordered_set> mp;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    for(int i = 0; i < n; i++) {
        int x, r, q;
        scanf("%d %d %d", &x, &r, &q);
        robo[i] = Robo(x, r, q, i);
    }

    sort(robo, robo+n);
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        int lo = max(0, robo[i].q - k);
        int hi = min(1000 * 1000 * 1000, robo[i].q + k);
        int lx = robo[i].x - robo[i].r;
        int rx = robo[i].x + robo[i].r;
        for (int j = lo; j <= hi; ++j) {
            if (mp.count(j)) {
        		int v1 = mp[j].order_of_key({rx+1, -1});
        		int v2 = mp[j].order_of_key({lx, -1});
            	ans += v1 - v2;
            }
        }
        mp[robo[i].q].insert({robo[i].x, i});
    }

    printf("%lld\n", ans);
    return 0;
}


// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <functional> // for less
// #include <iostream>
// #include<unordered_map>

// #define pii pair<int,int>
// using namespace __gnu_pbds;
// using namespace std;
// struct robot
// {
// 	int x,r,q,id;
// };
// bool cmpr(robot a, robot b)
// {
// 	return a.r<b.r;
// }
// typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //sorted by x

// unordered_map<int, ordered_set>IQ;
// vector<robot>R;
// int32_t main()
// {
//     ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
// 	int N, K, x, r, q, id;

// 	cin>>N>>K;
// 	for(id=0; id<N; ++id)
// 	{
// 		cin>>x>>r>>q;
// 		IQ[q].insert({x, id+1});
// 		R.push_back({x,r,q,id+1});
// 	}
// 	sort(R.begin(),R.end(),cmpr);
// 	long long ans=0;
// 	for(auto rob:R)
// 	{
// 		r=rob.r;
// 		q=rob.q;
// 		x=rob.x;
//         id=rob.id;
// 		IQ[q].erase({x,id});
// 		int right=IQ[q].order_of_key({x+r+1, -1});
// 		int left=IQ[q].order_of_key({x-r, -1});
// 		ans+=right-left;

// 		for(int k=1; k<=K; ++k)
// 		{
// 			if(IQ.find(q-k)!=IQ.end())
// 			{
// 				int right=IQ[q-k].order_of_key({x+r+1, -1});
//         		int left=IQ[q-k].order_of_key({x-r, -1});
//         		ans+=right-left;
// 			}
// 			if(IQ.find(q+k)!=IQ.end())
// 			{
// 				int right=IQ[q+k].order_of_key({x+r+1, -1});
// 		        int left=IQ[q+k].order_of_key({x-r, -1});
// 		        ans+=right-left;
// 			}
// 		}
// 	}
// 	cout<<ans<<endl;
// }

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int int64_t
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> bt;

struct robot
{
	int x, r, q;
	bool operator<(robot &o)
	{
		return r < o.r;
	}
};

int32_t main()
{
	int n, k;
	cin >> n >> k;
	vector<robot> a(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i].x >> a[i].r >> a[i].q;
	}
	sort(a.begin(), a.end());

	map<int, bt> C;
	for (int i = 0; i < n; ++i)
	{
		C[a[i].q].insert({a[i].x, i});
	}

	int ans = 0;
	for (int i = 0; i < n - 1; ++i)
	{
		C[a[i].q].erase(C[a[i].q].find({a[i].x,i}));
		for (int iq = a[i].q - k; iq <= a[i].q + k; ++iq)
		{
			if (C.find(iq)==C.end() || C[iq].empty())
				continue;
			int left=C[iq].order_of_key({a[i].x+a[i].r+1,-1});
			int right=C[iq].order_of_key({a[i].x-a[i].r, -1});
			ans+=right-left;
		}
	}
	cout << ans;
}


// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <functional> // for less
// #include <iostream>
// #include<unordered_map>

// #define pii pair<int,int>
// using namespace __gnu_pbds;
// using namespace std;
// struct robot
// {
// 	int x,r,q,id;
// };
// bool cmpr(robot a, robot b)
// {
// 	return a.r<b.r;
// }
// typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //sorted by x

// unordered_map<int, ordered_set>IQ;
// vector<robot>R;
// int32_t main()
// {
//     ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
// 	int N, K, x, r, q, id;

// 	cin>>N>>K;
// 	for(id=0; id<N; ++id)
// 	{
// 		cin>>x>>r>>q;
// 		IQ[q].insert({x, id+1});
// 		R.push_back({x,r,q,id+1});
// 	}
// 	sort(R.begin(),R.end(),cmpr);
// 	long long ans=0;
// 	for(auto rob:R)
// 	{
// 		r=rob.r;
// 		q=rob.q;
// 		x=rob.x;
//         id=rob.id;
// 		IQ[q].erase({x,id});
// 		int right=IQ[q].order_of_key({x+r+1, -1});
// 		int left=IQ[q].order_of_key({x-r, -1});
// 		ans+=right-left;

// 		for(int k=1; k<=K; ++k)
// 		{
// 			if(IQ.find(q-k)!=IQ.end())
// 			{
// 				int right=IQ[q-k].order_of_key({x+r+1, -1});
//         		int left=IQ[q-k].order_of_key({x-r, -1});
//         		ans+=right-left;
// 			}
// 			if(IQ.find(q+k)!=IQ.end())
// 			{
// 				int right=IQ[q+k].order_of_key({x+r+1, -1});
// 		        int left=IQ[q+k].order_of_key({x-r, -1});
// 		        ans+=right-left;
// 			}
// 		}
// 	}
// 	cout<<ans<<endl;
// }

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int int64_t
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> bt;

struct robot
{
	int x, r, q;
	bool operator<(robot &o)
	{
		return r < o.r;
	}
};

int32_t main()
{
	int n, k;
	cin >> n >> k;
	vector<robot> a(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i].x >> a[i].r >> a[i].q;
	}
	sort(a.begin(), a.end());

	map<int, bt> C;
	for (int i = 0; i < n; ++i)
	{
		C[a[i].q].insert({a[i].x, i});
	}

	int ans = 0;
	for (int i = 0; i < n - 1; ++i)
	{
		C[a[i].q].erase(C[a[i].q].find({a[i].x,i}));
		for (int iq = a[i].q - k; iq <= a[i].q + k; ++iq)
		{
			if (C[iq].empty())
				continue;
			int right=C[iq].order_of_key({a[i].x+a[i].r+1,-1});
			int left=C[iq].order_of_key({a[i].x-a[i].r, -1});
			ans+=right-left;
		}
	}
	cout << ans;
}


// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <functional> // for less
// #include <iostream>
// #include<unordered_map>

// #define pii pair<int,int>
// using namespace __gnu_pbds;
// using namespace std;
// struct robot
// {
// 	int x,r,q,id;
// };
// bool cmpr(robot a, robot b)
// {
// 	return a.r<b.r;
// }
// typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //sorted by x

// unordered_map<int, ordered_set>IQ;
// vector<robot>R;
// int32_t main()
// {
//     ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
// 	int N, K, x, r, q, id;

// 	cin>>N>>K;
// 	for(id=0; id<N; ++id)
// 	{
// 		cin>>x>>r>>q;
// 		IQ[q].insert({x, id+1});
// 		R.push_back({x,r,q,id+1});
// 	}
// 	sort(R.begin(),R.end(),cmpr);
// 	long long ans=0;
// 	for(auto rob:R)
// 	{
// 		r=rob.r;
// 		q=rob.q;
// 		x=rob.x;
//         id=rob.id;
// 		IQ[q].erase({x,id});
// 		int right=IQ[q].order_of_key({x+r+1, -1});
// 		int left=IQ[q].order_of_key({x-r, -1});
// 		ans+=right-left;

// 		for(int k=1; k<=K; ++k)
// 		{
// 			if(IQ.find(q-k)!=IQ.end())
// 			{
// 				int right=IQ[q-k].order_of_key({x+r+1, -1});
//         		int left=IQ[q-k].order_of_key({x-r, -1});
//         		ans+=right-left;
// 			}
// 			if(IQ.find(q+k)!=IQ.end())
// 			{
// 				int right=IQ[q+k].order_of_key({x+r+1, -1});
// 		        int left=IQ[q+k].order_of_key({x-r, -1});
// 		        ans+=right-left;
// 			}
// 		}
// 	}
// 	cout<<ans<<endl;
// }

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int int64_t
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> bt;

struct robot
{
	int x, r, q;
	bool operator<(robot &o)
	{
		return r < o.r;
	}
};

int32_t main()
{
	int n, k;
	cin >> n >> k;
	vector<robot> a(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i].x >> a[i].r >> a[i].q;
	}
	sort(a.begin(), a.end());

	map<int, bt> C;
	for (int i = 0; i < n; ++i)
	{
		C[a[i].q].insert({a[i].x, i});
	}

	int ans = 0;
	for (int i = 0; i < n - 1; ++i)
	{
		C[a[i].q].erase(C[a[i].q].find({a[i].x,i}));
		for (int iq = a[i].q - k; iq <= a[i].q + k; ++iq)
		{
			if (C.find(iq)==C.end() || C[iq].empty())
				continue;
			int right=C[iq].order_of_key({a[i].x+a[i].r+1,-1});
			int left=C[iq].order_of_key({a[i].x-a[i].r, -1});
			ans+=right-left;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 200006
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, ans, fw[MAXN];
vector <ll> v;
struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
		v.clear();
		fw.clear();
		n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		sort(all(v));
		v.resize( unique(all(v)) - v.begin() );
		fw.clear();
		n = v.size();
		fw.resize(n+2);
	}
	void update(ll x, ll nv)
	{
		x = lbd(v, x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		if(x <= 0)return 0;
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lbd(v, a) - v.begin();
		b = ubd(v, b) - v.begin() - 1;
		++a, ++b;
		if(b == 0 || a > b || a > n || b > n)return 0;
		if(a == 1)return sum(b);
		return sum(b) - sum(a-1);
	}
};

map <ll, fen/***/> mp;

struct arr
{
	ll p, r, iq;
} A[MAXN];
bool cmp(arr a, arr b)
{
	return a.r > b.r;
}
int main()
{
	cin >> n >> k;
	rep(i, 0, n)cin >> A[i].p >> A[i].r >> A[i].iq;
	sort(A, A+n, cmp);
	rep(i, 0, n)v.pb(A[i].p);
	sort(all(v));
	rep(i, 0, n)
	{
		if(mp.find(A[i].iq) == mp.end())mp[A[i].iq] = fen();
		mp[A[i].iq].add_elem(A[i].p);
	}
	for(auto i : mp)i.s.init();
	rep(i, 0, n)
	{
		ll pos = lower_bound(all(v), A[i].p)-v.begin()+1; //+1 bcoz one-index
		ll lb = A[i].p-A[i].r;
		ll ub = A[i].p+A[i].r;
		if(lb <= ub)
		{
			rep(j, max(0LL, A[i].iq-k), A[i].iq+k+1)
			{
				if(mp.find(j) == mp.end())continue;
				ans += mp[j].range_sum(lb, ub);
			}
		}
		mp[A[i].iq].update(A[i].p, 1);
	}
	cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
	    v.clear();
	    fw.clear();
	    n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
	    sort(all(v));
	    v.resize( unique(all(v)) - v.begin());
	    n = v.size();
		//no more adding
		fw.resize(n+2);
		rep(i, 0, n+2)fw[0] = 0;
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p=0, r=0, iq=0;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f, ind = A[i].s.s;
		if(type == 0)
		{
			//mp[iq]->update(bots[A[i].s.s].p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k+1)
			{
				if(mp.find(i) == mp.end())continue;
        //if(mp[i]->range_sum(p-r, p+r) > 0)cout << ind << ' ' << i << '\n';
				sum += mp[i]->range_sum(p-r, p+r);
			}
			mp[iq]->update(p, 1);
		}
		if(type == 2)
		{
			mp[iq]->update(bots[A[i].s.s].p, -1);
		}
		// if(type == 1)cout << sum << endl;
		ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
	    v.clear();
	    fw.clear();
	    n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		//no more adding
		fw.resize(n+2);
		rep(i, 0, n+2)fw[0] = 0;
		sort(all(v));
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p=0, r=0, iq=0;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f;
		if(type == 0)
		{
			mp[iq]->update(p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k)
			{
				if(mp.find(i) == mp.end())continue;
				sum += mp[i]->range_sum(p-r, p+r);
			}
			--sum;
		}
		if(type == 2)
		{
			mp[iq]->update(p, -1);
		}
		ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
	    v.clear();
	    fw.clear();
	    n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
	    sort(all(v));
	    v.resize( unique(all(v)) - v.begin());
	    n = v.size();
		//no more adding
		fw.resize(n+2);
		rep(i, 0, n+2)fw[0] = 0;
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p=0, r=0, iq=0;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f;
		if(type == 0)
		{
			mp[iq]->update(bots[A[i].s.s].p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k+1)
			{
				if(mp.find(i) == mp.end())continue;
				sum += mp[i]->range_sum(p-r, p+r);
			}
			--sum;
		}
		if(type == 2)
		{
			mp[iq]->update(bots[A[i].s.s].p, -1);
		}
		if(type == 1)cout << sum << endl;
		ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		//no more adding
		fw.resize(n);
		sort(all(v));
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p, r, iq;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f;
		if(type == 0)
		{
			mp[iq]->update(p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k)
			{
				if(mp.find(i) == mp.end())continue;
				sum += mp[i]->range_sum(p-r, p+r);
			}
			--sum;
		}
		if(type == 2)
		{
			mp[iq]->update(p, -1);
		}
		if(type == 1)ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 200006
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, ans, fw[MAXN];
vector <ll> v;
struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
		v.clear();
		fw.clear();
		n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		sort(all(v));
		v.resize( unique(all(v)) - v.begin() );
		fw.clear();
		n = v.size();
		fw.resize(n+2);
	}
	void update(ll x, ll nv)
	{
		x = lbd(v, x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		if(x <= 0)return 0;
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lbd(v, a) - v.begin();
		b = ubd(v, b) - v.begin() - 1;
		++a, ++b;
		if(b == 0 || a > b || a > n || b > n)return 0;
		if(a == 1)return sum(b);
		return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct arr
{
	ll p, r, iq;
} A[MAXN];
bool cmp(arr a, arr b)
{
	return a.r > b.r;
}
int main()
{
	cin >> n >> k;
	rep(i, 0, n)cin >> A[i].p >> A[i].r >> A[i].iq;
	sort(A, A+n, cmp);
	rep(i, 0, n)v.pb(A[i].p);
	sort(all(v));
	rep(i, 0, n)
	{
		if(mp.find(A[i].iq) == mp.end())mp[A[i].iq] = new fen;
		mp[A[i].iq]->add_elem(A[i].p);
	}
	for(auto i : mp)i.s->init();
	rep(i, 0, n)
	{
		ll pos = lower_bound(all(v), A[i].p)-v.begin()+1; //+1 bcoz one-index
		ll lb = A[i].p-A[i].r;
		ll ub = A[i].p+A[i].r;
		if(lb <= ub)
		{
			rep(j, max(0LL, A[i].iq-k), A[i].iq+k+1)
			{
				if(mp.find(j) == mp.end())continue;
				ans += mp[j]->range_sum(lb, ub);
			}
		}
		mp[A[i].iq]->update(A[i].p, 1);
	}
	cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq;
vector <ll> v;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
  ll fw[MAXN];

};

unordered_map <ll, fen*> mp;

struct ai
{
  ll p, r, iq;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
  FAST //ios_base speedup macro above
  cin >> n >> k;
  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
  rep(i, 0, n)
  {
    init(i);
    v.pb(p);
    A[co++] = spi(p-r, pi(0, iq));
    A[co++] = spi(p, pi(1, iq));
    A[co++] = spi(p+r, pi(2, iq));
  }
  sort(A, A+co, cmp);
  sort(all(v));
  v.resize( unique(all(v)) - v.begin() );
  rep(i, 0, co)
  {
    p = A[i].f, iq = A[i].s.s;
    ll type = A[i].s.f;
    if(type == 0);
  }
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
	    v.clear();
	    fw.clear();
	    n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		//no more adding
		fw.resize(n);
		rep(i, 0, n)fw[0] = 0;
		sort(all(v));
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p, r, iq;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f;
		if(type == 0)
		{
			mp[iq]->update(p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k)
			{
				if(mp.find(i) == mp.end())continue;
				sum += mp[i]->range_sum(p-r, p+r);
			}
			--sum;
		}
		if(type == 2)
		{
			mp[iq]->update(p, -1);
		}
		if(type == 1)ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
	    v.clear();
	    fw.clear();
	    n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
	    sort(all(v));
	    v.resize( unique(all(v)) - v.begin());
	    n = v.size();
		//no more adding
		fw.resize(n+2);
		rep(i, 0, n+2)fw[0] = 0;
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p=0, r=0, iq=0;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		// v.pb(p);
		// vec_q.pb(iq);
		// A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  // sort(all(v));
	  // v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f, ind = A[i].s.s;
		if(type == 0)
		{
			//mp[iq]->update(bots[A[i].s.s].p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k+1)
			{
				if(mp.find(i) == mp.end())continue;
        //if(mp[i]->range_sum(p-r, p+r) > 0)cout << ind << ' ' << i << '\n';
				sum += mp[i]->range_sum(p-r, p+r);
			}
			mp[iq]->update(p, 1);
		}
		if(type == 2)
		{
			mp[iq]->update(bots[A[i].s.s].p, -1);
		}
		// if(type == 1)cout << sum << endl;
		ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 200006
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, ans, fw[MAXN];
vector <ll> v;
struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
		v.clear();
		fw.clear();
		n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		sort(all(v));
		v.resize( unique(all(v)) - v.begin() );
		fw.clear();
		n = v.size();
		fw.resize(n+2);
	}
	void update(ll x, ll nv)
	{
		x = lbd(v, x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		if(x <= 0)return 0;
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lbd(v, a) - v.begin();
		b = ubd(v, b) - v.begin() - 1;
		++a, ++b;
		if(b == 0 || a > b || a > n || b > n)return 0;
		if(a == 1)return sum(b);
		return sum(b) - sum(a-1);
	}
};

map <ll, fen/***/> mp;

struct arr
{
	ll p, r, iq;
} A[MAXN];
bool cmp(arr a, arr b)
{
	return a.r > b.r;
}
int main()
{
	cin >> n >> k;
	rep(i, 0, n)cin >> A[i].p >> A[i].r >> A[i].iq;
	sort(A, A+n, cmp);
	rep(i, 0, n)v.pb(A[i].p);
	sort(all(v));
	rep(i, 0, n)
	{
		if(mp.find(A[i].iq) == mp.end())mp[A[i].iq] = new fen;
		mp[A[i].iq]->add_elem(A[i].p);
	}
	for(auto i : mp)i.s->init();
	rep(i, 0, n)
	{
		ll pos = lower_bound(all(v), A[i].p)-v.begin()+1; //+1 bcoz one-index
		ll lb = A[i].p-A[i].r;
		ll ub = A[i].p+A[i].r;
		if(lb <= ub)
		{
			rep(j, max(0LL, A[i].iq-k), A[i].iq+k+1)
			{
				if(mp.find(j) == mp.end())continue;
				ans += mp[j]->range_sum(lb, ub);
			}
		}
		mp[A[i].iq]->update(A[i].p, 1);
	}
	cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
	    v.clear();
	    fw.clear();
	    n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
	    sort(all(v));
	    v.resize( unique(all(v)) - v.begin());
	    n = v.size();
		//no more adding
		fw.resize(n+2);
		rep(i, 0, n+2)fw[0] = 0;
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p=0, r=0, iq=0;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f;
		if(type == 0)
		{
			mp[iq]->update(bots[A[i].s.s].p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k+1)
			{
				if(mp.find(i) == mp.end())continue;
				sum += mp[i]->range_sum(p-r, p+r);
			}
			--sum;
		}
		if(type == 2)
		{
			mp[iq]->update(bots[A[i].s.s].p, -1);
		}
		ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	v.clear();
	vector <ll> fw;
	fw.clear();
	ll n = 0;
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		//no more adding
		fw.resize(n);
		rep(i, 0, n)fw[0] = 0;
		sort(all(v));
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p, r, iq;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f;
		if(type == 0)
		{
			mp[iq]->update(p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k)
			{
				if(mp.find(i) == mp.end())continue;
				sum += mp[i]->range_sum(p-r, p+r);
			}
			--sum;
		}
		if(type == 2)
		{
			mp[iq]->update(p, -1);
		}
		if(type == 1)ans += sum;
	  }
	  cout << ans << '\n';
}


#include <bits/stdc++.h>
// #include "lib.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define LLINF 1234567890//987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define MAXN 100006
#define MAXK 26
#define MAXX 15000006
#define rep(kk, l1, l2)for(ll kk = l1; kk < l2; kk++)
#define bg(ms) *ms.begin()
#define ed(ms) *prev(ms.end(), 1)
#define addedge(a, b, c) v[a].pb(pi(b, c)); v[b].pb(pi(a, c))
#define ph push
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
ll n, k, co, p, r, iq, ans;
vector <ll> v, vec_q;
spi A[MAXN*3];

//for each iq, store a vector or something and then set size to be num of for each iq

struct fen
{
	vector <ll> v;
	vector <ll> fw;
	ll n = 0;
	fen()
	{
	    v.clear();
	    fw.clear();
	    n = 0;
	}
	void add_elem(ll x)
	{
		v.pb(x);
		n++;
	}
	void init()
	{
		//no more adding
		fw.resize(n+2);
		rep(i, 0, n+2)fw[0] = 0;
		sort(all(v));
	}
	void update(ll x, ll nv)
	{
		x = lower_bound(all(v), x) - v.begin();
		++x;
		for(; x<=n; x+=x&(-x))fw[x] += nv;
	}
	ll sum(ll x)
	{
		ll res = 0;
		for(; x; x-=x&(-x))res += fw[x];
		return res;
	}
	ll range_sum(ll a, ll b)
	{
		a = lower_bound(all(v), a) - v.begin();
		if(a == v.size())return 0;
		b = upper_bound(all(v), b) - v.begin() - 1;
		if(b == -1 || a > b)return 0;
		++a, ++b;
		if(a == 1)return sum(b);
		else return sum(b) - sum(a-1);
	}
};

map <ll, fen*> mp;

struct ai
{
  ll p=0, r=0, iq=0;
} bots[MAXN];

bool cmp(spi x, spi y)
{
  if(x.f == y.f)return x.s.f < y.s.f;
  return x.f < y.f;
}

void init(ll x)
{
  p = bots[x].p;
  r = bots[x].r;
  iq = bots[x].iq;
  return;
}

int main()
{
	  FAST //ios_base speedup macro above
	  cin >> n >> k;
	  rep(i, 0, n)cin >> bots[i].p >> bots[i].r >> bots[i].iq;
	  rep(i, 0, n)
	  {
		init(i);
		v.pb(p);
		vec_q.pb(iq);
		A[co++] = spi(max(p-r, 0LL), pi(0, i));
		A[co++] = spi(p, pi(1, i));
		A[co++] = spi(p+r, pi(2, i));
	  }
	  rep(i, 0, n)
	  {
		  init(i);
		  if(mp.find(iq) == mp.end())mp[iq] = new fen;
		  mp[iq]->add_elem(p);
	  }
	  for(auto i : mp)
	  {
		  i.s->init();
	  }

	  sort(A, A+co, cmp);
	  sort(all(v));
	  v.resize( unique(all(v)) - v.begin() );
	  rep(i, 0, co)
	  {
		ll sum = 0;
		p = A[i].f, iq = bots[A[i].s.s].iq, r = bots[A[i].s.s].r;
		ll type = A[i].s.f;
		if(type == 0)
		{
			mp[iq]->update(bots[A[i].s.s].p, 1);
		}
		if(type == 1)
		{
			rep(i, max(0LL, iq-k), iq+k)
			{
				if(mp.find(i) == mp.end())continue;
				sum += mp[i]->range_sum(p-r, p+r);
			}
			--sum;
		}
		if(type == 2)
		{
			mp[iq]->update(bots[A[i].s.s].p, -1);
		}
		ans += sum;
	  }
	  cout << ans << '\n';
}


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

mt19937 rnd;

template<typename T>
struct Treap{
    struct Node{
        T val, sum;
        int pri, cnt;
        Node *lch, *rch;
        Node(T v):val(v), pri(rnd()), cnt(1), sum(v){
            lch = rch = nullptr;
        }
    };

    Node* node;

    Treap(Node* n=nullptr){ node = n; }
    Treap(T v){ node = new Node(v); }
    void initialize(T v){ node = new Node(v); }

    int count(Node* t){ return !t ? 0 : t->cnt; }
    T   sum  (Node* t){ return !t ? 0 : t->sum; }
    int count(){ return count(node); }
    T   sum  (){ return sum  (node); }

    Node* update(Node* t){
        t->cnt = count(t->lch) + count(t->rch) + 1;
        t->sum =   sum(t->lch) +   sum(t->rch) + t->val;
        return t;
    }

    Node* merge(Node* l, Node* r){
        if(!l || !r) return !l ? r : l;
        if(l->val > r->val) swap(l, r);
        if(l->pri > r->pri){
            l->rch = merge(l->rch, r);
            return update(l);
        }else{
            r->lch = merge(l, r->lch);
            return update(r);
        }
    }
    void merge(Treap* tr){ node = merge(tr->node); }

    pair<Node*, Node*> split(Node* t, T v){
        if(!t) return make_pair(nullptr, nullptr);
        if(v < t->val){
            auto s = split(t->lch, v);
            t->lch = s.second;
            return {s.first, update(t)};
        }else{
            auto s = split(t->rch, v);
            t->rch = s.first;
            return {update(t), s.second};
        }
    }
    pair<Treap*, Treap*> split(T v){
        auto p = split(node, v);
        node = p.first;
        auto t2 = new Treap(p.second);
        return make_pair(this, t2);
    }

    void insert(T v){
        auto s = split(node, v);
        auto n = new Node(v);
        node = merge(s.first, n);
        node = merge(node, s.second);
    }

    void erase(int v){
        auto s1 = split(node, v-1);
        auto s2 = split(s1.second, v);
        merge(s1.first, s2.second);
    }

    int getcount(Node* t, T v, bool larger){
        if(!t) return 0;
        int ret = 0;
        if(larger){
            if(v <= t->val) ret++;
            ret += v <= t->val ? count(t->rch) : getcount(t->rch, v, larger);
            ret += v <= t->val ? getcount(t->lch, v, larger) : 0;
        }else{
            if(v >= t->val) ret++;
            ret += v >= t->val ? count(t->lch) : getcount(t->lch, v, larger);
            ret += v >= t->val ? getcount(t->rch, v, larger) : 0;
        }
        return ret;
    }
    int getcount(T v, bool larger){ return getcount(node, v, larger); }

    T getsum(Node* t, T v, bool larger){
        if(!t) return 0;
        T ret = 0;
        if(larger){
            if(v <= t->val) ret += t->val;
            ret += v <= t->val ? sum(t->rch) : getsum(t->rch, v, larger);
            ret += v <= t->val ? getsum(t->lch, v, larger) : 0;
        }else{
            if(v >= t->val) ret += t->val;
            ret += v >= t->val ? sum(t->lch) : getsum(t->lch, v, larger);
            ret += v >= t->val ? getsum(t->rch, v, larger) : 0;
        }
        return ret;
    }
    T getsum(T v, bool larger){ return getsum(node, v, larger); }
};

int main(){
    int64_t i, j, k;
    int N, K;
    cin >> N >> K;
    vector<vector<int>> RXQ(N);
    for(i=0; i<N; i++){
        int x, r, q;
        scanf("%d %d %d", &x, &r, &q);
        RXQ[i] = {r, x, q};
    }
    sort(RXQ.rbegin(), RXQ.rend());

    map<int, Treap<int>> trees;
    int64_t ans = 0;
    for(auto&& v : RXQ){
        int r = v[0], x = v[1], q = v[2];
        for(k=q-K; k<=q+K; k++){
            if(trees.count(k)){
                ans += trees[k].getcount(x-r, true) - trees[k].getcount(x+r+1, true);
            }
        }
        trees.count(q) ? trees[q].insert(x) : trees[q].initialize(x);
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#define int long long
using namespace __gnu_pbds;
using namespace std;
struct tup
{
    int x,r,q;
    tup(int xx,int rr,int qq)
    {
        x=xx;r=rr;q=qq;
    }
    bool operator<(const tup aa)
    {
        return r>aa.r;
    }
};
map<int,int> id1;
vector<int> id2;
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
ordered_set s[100005];
vector<tup> v;
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n,l=0,k,ans=0;cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        int x,r,q;cin>>x>>r>>q;
        id1[q]=1;
        v.push_back(tup(x,r,q));
    }
    for(auto &i:id1)
    {
        i.second=l++;
        id2.push_back(i.first);
    }
    for(int i=0;i<n;i++)
    {
        int ind=id1[v[i].q];
        for(int j=0;j<=k && j+ind<l;j++)
        {
            if(id2[ind+j]>v[i].q+k)break;
            ans+=s[ind+j].order_of_key({v[i].x+v[i].r+1,0})-s[ind+j].order_of_key({v[i].x-v[i].r,0});
        }
        for(int j=1;j<=k && ind-j>=0;j++)
        {
            if(id2[ind-j]<v[i].q+k)break;
            ans+=s[ind-j].order_of_key({v[i].x+v[i].r+1,0})-s[ind-j].order_of_key({v[i].x-v[i].r,0});
        }
        s[ind].insert({v[i].x,i});
    }
    cout<<ans;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#define int long long
using namespace __gnu_pbds;
using namespace std;
struct tup
{
    int x,r,q;
    tup(int xx,int rr,int qq)
    {
        x=xx;r=rr;q=qq;
    }
    bool operator<(const tup aa)
    {
        return r>aa.r;
    }
};
map<int,int> id1;
vector<int> id2;
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
ordered_set s[100005];
vector<tup> v;
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n,l=0,k,ans=0;cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        int x,r,q;cin>>x>>r>>q;
        id1[q]=1;
        v.push_back(tup(x,r,q));
    }
    for(auto &i:id1)
    {
        i.second=l++;
        id2.push_back(i.first);
    }
    for(int i=0;i<n;i++)
    {
        int ind=id1[v[i].q];
        for(int j=0;j<=k && j+ind<l;j++)
        {
            if(id2[ind+j]>v[i].q+k)break;
            ans+=s[ind+j].order_of_key({v[i].x+v[i].r+1,0})-s[ind+j].order_of_key({v[i].x-v[i].r,0});
        }
        for(int j=1;j<=k && ind-j>=0;j++)
        {
            if(id2[ind-j]<v[i].q-k)break;
            ans+=s[ind-j].order_of_key({v[i].x+v[i].r+1,0})-s[ind-j].order_of_key({v[i].x-v[i].r,0});
        }
        s[ind].insert({v[i].x,i});
    }
    cout<<ans;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#define int long long
using namespace __gnu_pbds;
using namespace std;
struct tup
{
    int x,r,q;
    tup(int xx,int rr,int qq)
    {
        x=xx;r=rr;q=qq;
    }
    bool operator<(const tup aa)
    {
        return r>aa.r;
    }
};
map<int,int> id1;
vector<int> id2;
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
ordered_set s[100005];
vector<tup> v;
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n,l=0,k,ans=0;cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        int x,r,q;cin>>x>>r>>q;
        id1[q]=1;
        v.push_back(tup(x,r,q));
    }
    for(auto &i:id1)
    {
        i.second=l++;
        id2.push_back(i.first);
    }
    sort(v.begin(),v.end());
    for(int i=0;i<n;i++)
    {
        int ind=id1[v[i].q];
        for(int j=0;j<=k && j+ind<l;j++)
        {
            if(id2[ind+j]>v[i].q+k)break;
            ans+=s[ind+j].order_of_key({v[i].x+v[i].r+1,0})-s[ind+j].order_of_key({v[i].x-v[i].r,0});
        }
        for(int j=1;j<=k && ind-j>=0;j++)
        {
            if(id2[ind-j]<v[i].q-k)break;
            ans+=s[ind-j].order_of_key({v[i].x+v[i].r+1,0})-s[ind-j].order_of_key({v[i].x-v[i].r,0});
        }
        s[ind].insert({v[i].x,i});
    }
    cout<<ans;

}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N =1e5+5;

int n;
ll K;
int c[3*N];
int lim=3*N;
int cc;
ll dis[3*N];
ll ans;

struct node
{
    ll x,y;
    ll L,R,r;
}a[N];

int lowbit(int x)
{
    return x&(-x);
}

void update(int x,ll val)
{
    for(;x<lim;x+=lowbit(x)){
        c[x]+=val;
    }
}

ll query(int x)
{
    ll sum=0;
    for(;x>0;x-=lowbit(x)){
        sum+=c[x];
    }
    return sum;
}

void clearr(int x)
{
    for(;x<lim;x+=lowbit(x)){
        if(c[x]==0) break;
        c[x]=0;
    }
}

bool cmp2(node a,node b)
{
    return a.y<b.y;
}

void cdq(int l,int r)
{
    if(l==r)
        return ;
    int mid=l+r>>1;
    cdq(l,mid),cdq(mid+1,r);
    int p1,p2;
    p1=p2=mid+1;
    //cout<<"p1 "<<p1<<" p2 "<<p2<<endl;
    for(int i=l;i<=mid;i++){
        while(p2<=r&&a[p2].y<=a[i].y+K){
            update(a[p2].x,1);
            p2++;
        }
        while(p1<p2&&a[p1].y<a[i].y-K){
            update(a[p1].x,-1);
            p1++;
        }
        //cout<<"p1 "<<p1<<" p2 "<<p2<<endl;
        ans+=query(a[i].R)-query(a[i].L-1);
    }
    //cout<<"l "<<l<<" r "<<r<<" ans "<<ans<<endl;
    for(int i=p1;i<p2;i++) clearr(a[i].x);
    inplace_merge(a+l,a+mid+1,a+r+1,cmp2);
}

bool cmp1(node a,node b)
{
    return a.r<b.r;
}

int main()
{
    scanf("%d %lld",&n,&K);
    cc=0;
    for(int i=1;i<=n;i++){
        scanf("%lld %lld %lld",&a[i].x, &a[i].r, &a[i].y);
        dis[++cc]=a[i].x;
        dis[++cc]=a[i].x-a[i].r;
        dis[++cc]=a[i].x+a[i].r;
    }

    sort(dis+1,dis+cc+1);
    cc=unique(dis+1,dis+cc+1)-(dis+1);

    for(int i=1;i<=n;i++){
        int in=lower_bound(dis+1,dis+cc+1,a[i].x-a[i].r)-dis;
        a[i].L=in;
        in=lower_bound(dis+1,dis+cc+1,a[i].x+a[i].r)-dis;
        a[i].R=in;
        in=lower_bound(dis+1,dis+cc+1,a[i].x)-dis;
        a[i].x=in;
    }
    /*
    cout<<"*****"<<endl;
    for(int i=1;i<=n;i++){
        printf("%lld %lld %lld\n",a[i].L,a[i].x,a[i].R);
    }
    */
    sort(a+1,a+n+1,cmp1);
    cdq(1,n);
    printf("%lld\n",ans);

    return 0;
}

/*

4 2
5 10 2
7 3 4
2 5 3
4 2 5

2 2
7 3 4
4 2 5

*/


#include<bits/stdc++.h>
#define ll long long
#define N 200005

using namespace std;
inline int Get()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while('0'<=ch&&ch<='9')
    {
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    return x*f;
}

int n,k,cc;
struct robot
{
    ll x,R,q;
    ll l,r;
    bool operator <(const robot &a)const
    {
        return R<a.R;
    }
} s[N];

ll ans;
ll d[N<<2];
int tem[N<<2];

int low(int i)
{
    return i&(-i);
}

void add(int v,int f)
{
    for(int i=v; i<=cc; i+=low(i))
        tem[i]+=f;
}

void Clear(int v)
{
    for(int i=v; i<=cc; i+=low(i))
        tem[i]=0;
}

int query(int v)
{
    int ans=0;
    for(int i=v; i; i-=low(i))
        ans+=tem[i];
    return ans;
}

bool cmp(const robot &a,const robot &b)
{
    return a.q<b.q;
}

deque<int>st;

void CDQ(int l,int r)
{
    if(l==r)
        return ;
    int mid=l+r>>1;
    CDQ(l,mid),CDQ(mid+1,r);
    int st=mid+1,ed=mid;
    for(int i=l; i<=mid; i++)
    {
        while(ed<r&&s[ed+1].q<=s[i].q+k)
        {
            ed++;
            add(s[ed].x,1);
        }
        while(st<=ed&&s[st].q<s[i].q-k)
        {
            add(s[st].x,-1);
            st++;
        }
        ans+=query(s[i].r)-query(s[i].l-1);
    }
    for(int i=mid+1; i<=r; i++)
        Clear(s[i].x);
    inplace_merge(s+l,s+mid+1,s+r+1,cmp);
}

int main()
{
    n=Get(),k=Get();
    for(int i=1; i<=n; i++)
    {
        s[i].x=Get(),s[i].R=Get(),s[i].q=Get();
        d[++d[0]]=s[i].x;
        d[++d[0]]=s[i].x-s[i].R;
        d[++d[0]]=s[i].x+s[i].R;
    }
    sort(d+1,d+1+d[0]);
    cc=unique(d+1,d+1+d[0])-d;
    for(int i=1; i<=n; i++)
    {
        int in=lower_bound(d+1,d+cc+1,s[i].x-s[i].R)-d;
        s[i].l=in;//lower_bound(d+1,d+cc+1,s[i].x-s[i].R)-d;
        in=lower_bound(d+1,d+cc+1,s[i].x+s[i].R)-d;
        s[i].r=in;//lower_bound(d+1,d+cc+1,s[i].x+s[i].R)-d;
        in=lower_bound(d+1,d+cc+1,s[i].x)-d;
        s[i].x=in;
    }
    /*
    for(int i=1;i<=n;i++){
        printf("%lld %lld %lld\n",s[i].l,s[i].x,s[i].r);
    }
    */
    sort(s+1,s+1+n);
    /*cout<<"*******"<<endl;
    for(int i=1;i<=n;i++){
        printf("%d %d %d\n",s[i].l,s[i].x,s[i].r);
    }
    */

    CDQ(1,n);
    cout<<ans;
    return 0;
}

/*

10 5
41 436 1
478 604 2
169 153 2
358 382 1
334 716 8
500 895 7
724 726 1
464 538 9
962 912 7
467 299 5

*/


#include<bits/stdc++.h>
#define ll long long
#define N 200005

using namespace std;
inline int Get()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while('0'<=ch&&ch<='9')
    {
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    return x*f;
}

int n,k,cc;
struct robot
{
    ll x,R,q;
    ll l,r;
    bool operator <(const robot &a)const
    {
        return R<a.R;
    }
} s[N];

ll ans;
ll d[N<<2];
int tem[N<<2];

int low(int i)
{
    return i&(-i);
}

void add(int v,int f)
{
    for(int i=v; i<=cc; i+=low(i))
        tem[i]+=f;
}

void Clear(int v)
{
    for(int i=v; i<=cc; i+=low(i))
        tem[i]=0;
}

int query(int v)
{
    int ans=0;
    for(int i=v; i; i-=low(i))
        ans+=tem[i];
    return ans;
}

bool cmp(const robot &a,const robot &b)
{
    return a.q<b.q;
}

deque<int>st;

void CDQ(int l,int r)
{
    if(l==r)
        return ;
    int mid=l+r>>1;
    CDQ(l,mid),CDQ(mid+1,r);
    int st=mid+1,ed=mid;
    for(int i=l; i<=mid; i++)
    {
        while(ed<r&&s[ed+1].q<=s[i].q+k)
        {
            ed++;
            add(s[ed].x,1);
        }
        while(st<=ed&&s[st].q<s[i].q-k)
        {
            add(s[st].x,-1);
            st++;
        }
        ans+=query(s[i].r)-query(s[i].l-1);
    }
    for(int i=mid+1; i<=r; i++)
        Clear(s[i].x);
    inplace_merge(s+l,s+mid+1,s+r+1,cmp);
}

int main()
{
    n=Get(),k=Get();
    for(int i=1; i<=n; i++)
    {
        s[i].x=Get(),s[i].R=Get(),s[i].q=Get();
        d[++d[0]]=s[i].x;
        d[++d[0]]=s[i].x-s[i].R;
        d[++d[0]]=s[i].x+s[i].R;
    }
    //sort(d+1,d+1+d[0]);
    cc=unique(d+1,d+1+d[0])-d;
    for(int i=1; i<=n; i++)
    {
        s[i].l=lower_bound(d+1,d+cc,s[i].x-s[i].R)-d;
        s[i].r=lower_bound(d+1,d+cc,s[i].x+s[i].R)-d;
        s[i].x=lower_bound(d+1,d+cc,s[i].x)-d;
    }
    sort(s+1,s+1+n);
    CDQ(1,n);
    cout<<ans;
    return 0;
}


#include"bits/stdc++.h"
#define pb push_back
#define int long long
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define rz(x) resize(x)
#define sz(x) (int)(x).size()
#define F first
#define S second
#define endl cout<<'\n';
#define loli ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
const int maxn = 1e5;
const int max_n = 4e6;
const int C = 1e9;
int rt[max_n] , lson[max_n] , rson[max_n] , sum[max_n];
int id , cnt;
struct T
{
	int x , r , iq;
	bool operator<(const T &a)const{
		return r > a.r;
	}
};
vector<T> v;
map<int,int> mp;
void update(int &p , int l , int r , int x){
	if(!p) p = ++id;
	sum[p]++;
	if(l == r) return;
	int m = l+r>>1;
	if(x <= m) update(lson[p] , l , m , x);
	else update(rson[p] , m+1 , r , x);
}
int query(int p , int l , int r , int L , int R ){
	if(!p) return 0;
	if(L <= l and r <= R){
		return sum[p];
	}
	int m = l+r>>1 , ans = 0;
	if(L <= m) ans += query(lson[p] , l , m , L , R );
	if(R > m)  ans += query(rson[p] , m+1,r , L , R );
	return ans ;
}
inline void latina(){
	int n , k;
	cin >> n >> k;
	v.rz(n);
	for(int i=0 ; i<n ; ++i){
		cin >> v[i].x >> v[i].r >> v[i].iq;
		if(mp[v[i].iq] == 0) mp[v[i].iq] = ++cnt;
	}
	sort(all(v));
	int ans = 0;
	for(auto &c : v){
		int rad , l  , r , L , R;
		rad = c.r , l = max(0ll , c.iq-k) , r = min(C , c.iq+k);
		L = max(0ll , c.x-rad) , R = min(C , c.x+rad);
		for(int i=l ; i<=r ; ++i){
			if(mp.find(i) == mp.end())continue;
			ans += query(rt[mp[i]] , 1 , C , L , R);
		}
		update(rt[mp[c.iq]] , 1 , C , c.x);
	}
	cout << ans << '\n';
}
signed main(){loli;latina();}


#include<bits/stdc++.h>
#define x first
#define y second
#define FOR(a,b,c) for(int a=(b),a##_end=(c);a<=a##_end;++a)
#define ROF(a,b,c) for(int a=(b),a##_end=(c);a>=a##_end;--a)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f,N=3e5+10;
template<class T>inline bool chkmin(T &A,T B){return B<A?A=B,1:0;}
template<class T>inline bool chkmax(T &A,T B){return A<B?A=B,1:0;}
int n,K,x[N],l[N],r[N],q[N];
int rt[N],B[N],T[N],h,tot;
vector<int>beg[N],ed[N],now[N];
struct Segment_Tree{
	#define lson pl,mid,ch[p][0]
	#define rson mid+1,pr,ch[p][1]
	int tot,sum[N*20],ch[N*20][2];
	void Up(int p){sum[p]=sum[ch[p][0]]+sum[ch[p][1]];}
	void Update(int pl,int pr,int &p,int k,int t){
		if(!p)p=++tot;
		if(pl==pr){sum[p]+=t;return;}
		int mid=(pl+pr)>>1;
		if(k<=mid)Update(lson,k,t);
		else Update(rson,k,t);
		Up(p);
	}
	int Query(int pl,int pr,int p,int l,int r){
		if(!p)return 0;
		if(pl==l&&pr==r)return sum[p];
		int mid=(pl+pr)>>1;
		if(r<=mid)return Query(lson,l,r);
		else if(mid<l)return Query(rson,l,r);
		return Query(lson,l,mid)+Query(rson,mid+1,r);
	}
}Tr;
int main(){
	ll ans=0;
	scanf("%d%d",&n,&K);
	FOR(i,1,n){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		T[++h]=q[i];
		B[++tot]=x[i];
		B[++tot]=x[i]-r[i];
		B[++tot]=x[i]+r[i];
	}
	sort(B+1,B+1+tot);
	tot=unique(B+1,B+1+tot)-B-1;
	sort(T+1,T+1+h);
	h=unique(T+1,T+1+h)-T-1;
	FOR(i,1,n){
		x[i]=lower_bound(B+1,B+1+tot,x[i])-B;
		l[i]=lower_bound(B+1,B+1+tot,x[i]-r[i])-B;
		r[i]=lower_bound(B+1,B+1+tot,x[i]+r[i])-B;
		beg[l[i]].push_back(i);
		ed[r[i]].push_back(i);
		now[x[i]].push_back(i);
		q[i]=lower_bound(T+1,T+1+h,q[i])-T;
	}
	FOR(i,1,tot){
		for(auto y:beg[i])
			Tr.Update(1,tot,rt[q[y]],x[y],1);
		for(auto y:now[i]){
			FOR(j,q[y],h){
				if(T[j]-T[q[y]]>K)break;
				ans+=Tr.Query(1,tot,rt[j],l[y],r[y]);
			}
			ROF(j,q[y],1){
				if(T[q[y]]-T[j]>K)break;
				ans+=Tr.Query(1,tot,rt[j],l[y],r[y]);
			}
		}
		for(auto y:ed[i])
			Tr.Update(1,tot,rt[q[y]],x[y],-1);
	}
	printf("%lld\n",(ans-n)/2);
	return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
//#define bug(x) cerr<<#x<<'='<<x<<' '
//#define debug(x) cerr<<#x<<'='<<x<<'\n'
#define FOR(a,b,c) for(int a=(b),a##_end=(c);a<=a##_end;++a)
#define ROF(a,b,c) for(int a=(b),a##_end=(c);a>=a##_end;--a)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f,N=3e5+10;
template<class T>inline bool chkmin(T &A,T B){return B<A?A=B,1:0;}
template<class T>inline bool chkmax(T &A,T B){return A<B?A=B,1:0;}
int n,K,x[N],l[N],r[N],q[N];
int rt[N],B[N],T[N],h,tot;
vector<int>beg[N],ed[N],now[N];
struct Segment_Tree{
	#define lson pl,mid,ch[p][0]
	#define rson mid+1,pr,ch[p][1]
	int tot,sum[N*20],ch[N*20][2];
	void Up(int p){sum[p]=sum[ch[p][0]]+sum[ch[p][1]];}
	void Update(int pl,int pr,int &p,int k,int t){
		if(!p)p=++tot;
		if(pl==pr){sum[p]+=t;return;}
		int mid=(pl+pr)>>1;
		if(k<=mid)Update(lson,k,t);
		else Update(rson,k,t);
		Up(p);
	}
	int Query(int pl,int pr,int p,int l,int r){
		if(!p)return 0;
		if(pl==l&&pr==r)return sum[p];
		int mid=(pl+pr)>>1;
		if(r<=mid)return Query(lson,l,r);
		else if(mid<l)return Query(rson,l,r);
		return Query(lson,l,mid)+Query(rson,mid+1,r);
	}
}Tr;
int main(){
//	freopen("input.txt","r",stdin);
//	freopen("out1.txt","w",stdout);
	ll ans=0;
	scanf("%d%d",&n,&K);
	FOR(i,1,n){
		scanf("%d%d%d",&x[i],&r[i],&q[i]);
		T[++h]=q[i];
		B[++tot]=x[i];
		B[++tot]=x[i]-r[i];
		B[++tot]=x[i]+r[i];
	}
	sort(B+1,B+1+tot);
	tot=unique(B+1,B+1+tot)-B-1;
	sort(T+1,T+1+h);
	h=unique(T+1,T+1+h)-T-1;
	FOR(i,1,n){
		l[i]=lower_bound(B+1,B+1+tot,x[i]-r[i])-B;
		r[i]=lower_bound(B+1,B+1+tot,x[i]+r[i])-B;
		x[i]=lower_bound(B+1,B+1+tot,x[i])-B;
		beg[l[i]].push_back(i);
		ed[r[i]].push_back(i);
		now[x[i]].push_back(i);
		q[i]=lower_bound(T+1,T+1+h,q[i])-T;
	}
	FOR(i,1,tot){
		for(auto y:beg[i])
			Tr.Update(1,tot,rt[q[y]],x[y],1);
		for(auto y:now[i]){
			FOR(j,q[y],h){
				if(T[j]-T[q[y]]>K)break;
				ans+=Tr.Query(1,tot,rt[j],l[y],r[y]);
			}
			ROF(j,q[y]-1,1){
				if(T[q[y]]-T[j]>K)break;
				ans+=Tr.Query(1,tot,rt[j],l[y],r[y]);
			}
//			bug(y),debug(ans);
		}
		for(auto y:ed[i])
			Tr.Update(1,tot,rt[q[y]],x[y],-1);
	}
	printf("%lld\n",(ans-n)/2);
	return 0;
}