#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(time(0));   bool isPrime(long long a) {     long long i;     for (i = 2; i * i <= a; i++) {       if (a % i == 0) {         return 0;       }     }     return (a != 1);   }   bool isPalindrom(const string &s, int l, int r) {     for (int i = 0; i < (r - l + 1) / 2; i++) {       if (s[l + i] != s[r - i]) {         return 0;       }     }     return 1;   }   template <class type1>   type1 gcd(type1 a, type1 b) {     type1 t;     while (b) {       t = b;       b = a % b;       a = t;     }     return a;   }   long long positiveMod(long long v, long long md) {     v = v % md;     if (v < 0) {       v += md;     }     return v;   }   class information {    public:     vector<pair<long long, long long> > dat;     vector<long long> *stL, *stR;     long long iq, n;     void build() {       n = dat.size();       sort(dat.begin(), dat.end());       stL = new vector<long long>[4 * n];       stR = new vector<long long>[4 * n];       buildSt(1, 0, n - 1);     }     void buildSt(long long v, long long l, long long r) {       if (l == r) {         stL[v].push_back(dat[l].first - dat[l].second);         stR[v].push_back(dat[l].first + dat[l].second);       } else {         long long mid = (l + r) / 2;         buildSt(v * 2, l, mid);         buildSt(v * 2 + 1, mid + 1, r);         int i1, i2;         i1 = i2 = 0;         while (i1 != stL[v * 2].size() || i2 != stL[v * 2 + 1].size()) {           if (i1 == stL[v * 2].size()) {             stL[v].push_back(stL[v * 2 + 1][i2]);             i2++;           } else if (i2 == stL[v * 2 + 1].size()) {             stL[v].push_back(stL[v * 2][i1]);             i1++;           } else {             if (stL[v * 2][i1] < stL[v * 2 + 1][i2]) {               stL[v].push_back(stL[v * 2][i1]);               i1++;             } else {               stL[v].push_back(stL[v * 2 + 1][i2]);               i2++;             }           }         }         i1 = i2 = 0;         while (i1 != stR[v * 2].size() || i2 != stR[v * 2 + 1].size()) {           if (i1 == stR[v * 2].size()) {             stR[v].push_back(stR[v * 2 + 1][i2]);             i2++;           } else if (i2 == stR[v * 2 + 1].size()) {             stR[v].push_back(stR[v * 2][i1]);             i1++;           } else {             if (stR[v * 2][i1] > stR[v * 2 + 1][i2]) {               stR[v].push_back(stR[v * 2][i1]);               i1++;             } else {               stR[v].push_back(stR[v * 2 + 1][i2]);               i2++;             }           }         }       }     }     long long getSum(long long v, long long l, long long r, long long tl,                      long long tr, long long val) {       if (l == tl && tr == r) {         long long mid, res = 0;         l = 0;         r = stL[v].size();         while (l != r) {           mid = (l + r) / 2;           if (stL[v][mid] > val) {             r = mid;           } else {             l = mid + 1;           }         }         res += stL[v].size() - l;         l = 0;         r = stR[v].size();         while (l != r) {           mid = (l + r) / 2;           if (stR[v][mid] >= val) {             l = mid + 1;           } else {             r = mid;           }         }         res += stR[v].size() - l;         return res;       } else {         long long mid = (l + r) / 2;         if (tr <= mid) {           return getSum(v * 2, l, mid, tl, tr, val);         } else if (tl > mid) {           return getSum(v * 2 + 1, mid + 1, r, tl, tr, val);         } else {           return getSum(v * 2, l, mid, tl, mid, val) +                  getSum(v * 2 + 1, mid + 1, r, mid + 1, tr, val);         }       }     }     long long getCount(long long x, long long rad) {       long long l, r, i, j, st, en, mid;       l = 0;       r = n;       while (l != r) {         mid = (l + r) / 2;         if (dat[mid].first >= x - rad) {           r = mid;         } else {           l = mid + 1;         }       }       st = l;       l = -1;       r = n - 1;       while (l != r) {         mid = (l + r + 1) / 2;         if (dat[mid].first <= x + rad) {           l = mid;         } else {           r = mid - 1;         }       }       en = r;       if (en < st) {         return 0;       } else {         return (en - st + 1) - getSum(1, 0, n - 1, st, en, x);       }     }   };   vector<information> inf;   vector<pair<long long, pair<long long, long long> > > inp;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n, k, i, j, a, b, c, l, r, res = 0;     cin >> n >> k;     for (i = 0; i < n; i++) {       cin >> a >> b >> c;       inp.push_back({c, {a, b}});     }     sort(inp.begin(), inp.end());     for (i = 0; i < n;) {       l = i;       r = i;       while (r < n) {         if (inp[r].first == inp[l].first) {           r++;         } else {           break;         }       }       information t;       t.iq = inp[l].first;       for (j = l; j < r; j++) {         t.dat.push_back(inp[j].second);       }       inf.push_back(t);       inf.back().build();       i = r;     }     for (i = 0; i < inf.size(); i++) {       a = 0;       for (auto it : inf[i].dat) {         a += inf[i].getCount(it.first, it.second);         for (j = i - 1; j >= 0; j--) {           if ((inf[i].iq - inf[j].iq) > k) {             break;           } else {             res += inf[j].getCount(it.first, it.second);           }         }       }       res += (a - inf[i].n) / 2;     }     cout << res <<   n ;     return 0;   }   