This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <unistd.h>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
#endif
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl '\n'
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < (int) n; i++)
#define pb push_back
#define fi first
#define se second
int mod = 1e9+7;
int inf = 1e6;
const int N = 3e5;
int t[N];
struct bit2d {
    vi bit;
    int n, m;
    void init(int n, int m) {
        this->n = n, this->m = m;
        bit.assign(n*m, 0);
    }
    void update(int y, int x, int val) {
        y = min(y, n), x = min(x, m);
        for(int i = y; i > 0 && i <= n; i += (i & -i)) {
            for(int j = x; j > 0 && j <= m; j += (j & -j)) {
                bit[(i-1)*m + j-1] += val;
            }
        }
    }
    int query(int y, int x) {
        int res = 0;
        y = min(y, n), x = min(x, m);
        for(int i = y; i > 0 && i <= n; i -= (i & -i)) {
            for(int j = x; j > 0 && j <= m; j -= (j & -j)) {
                res += bit[(i-1)*m + j-1];
            }
        }
        return res;
    }
};
void solve() {
    int d, n, k, m;
    cin >> d >> n >> k >> m;
    if(d == 1) {
        vi a(n);
        F(i, 0, n) cin >> a[i];
        sort(a.begin(), a.end());
        queue<int> last;
        int ans = 0;
        F(i, 0, n) {
            while(last.size() && last.front() < a[i] - k) last.pop();
            ans += last.size();
            last.push(a[i]);
        }
        cout << ans << endl;
    } else if(d == 2) {
        vpi a(n);
        F(i, 0, n) cin >> a[i].fi >> a[i].se;
        F(i, 0, n) {
            int y = a[i].fi, x = a[i].se;
            a[i].fi = x - y;
            a[i].se = x + y;
        }
        sort(a.begin(), a.end());
        Tree<pii> st;
        int ans = 0;
        queue<int> q;
        F(i, 0, n) {
            while(q.size() && a[q.front()].fi < a[i].fi - k) {
                st.erase({a[q.front()].se, q.front()});
                q.pop();
            }
            ans += st.order_of_key({a[i].se + k, inf}) - st.order_of_key({a[i].se - k, -1});
            st.insert({a[i].se, i});
            q.push(i);
        }
        cout << ans << endl;
    } else if(d == 3) {
        vector<pair<int, pii>> a(n);
        F(i, 0, n) {
            cin >> a[i].se.fi >> a[i].se.se >> a[i].fi;
            int y = a[i].se.fi, x = a[i].se.se;
            a[i].se.fi = x + y;
            a[i].se.se = x - y + m;
        }
        sort(a.begin(), a.end());
        int MAX_IDK = 4*m+5;
        bit2d bit[MAX_IDK];
        F(i, 0, MAX_IDK) bit[i].init(MAX_IDK, MAX_IDK);
        int ans = 0;
        F(i, 0, n) {
            F(z, 0, MAX_IDK) {
                int nk = k - abs(z - a[i].fi);
                if(nk < 0) continue;
                ans += bit[z].query(a[i].se.fi + nk, a[i].se.se + nk) - bit[z].query(a[i].se.fi + nk, a[i].se.se - nk - 1) - bit[z].query(a[i].se.fi - nk - 1, a[i].se.se + nk) + bit[z].query(a[i].se.fi - nk - 1, a[i].se.se - nk - 1);
            }
            bit[a[i].fi].update(a[i].se.fi, a[i].se.se, 1);
        }
        cout << ans << endl;
    }
}
void setIO() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    #ifdef Local
        freopen("local.in", "r", stdin);
        freopen("local.out", "w", stdout);
    #else 
        // freopen("friendcross.in","r",stdin);
        // freopen("friendcross.out","w",stdout);
    #endif
}
signed main() {
    setIO();
    int t = 1;
    //cin >> t;
    while(t--) solve();
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |