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/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a,b) uniform_int_distribution<int>(a,b)(rng)
#define ins insert
#define ssize(x) (int((x).size()))
#define bs(args...) binary_search(args)
#define lb(args...) lower_bound(args)
#define ub(args...) upper_bound(args)
#define all(x) (x).begin(),(x).end()
#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define die exit(0)
template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;
constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------
template <class T, int ...Ns> struct BIT {
    T val = 0; void upd(T v) { val += v; }
    T query() { return val; }
};
template <class T, int N, int... Ns> struct BIT<T, N, Ns...> {
    BIT<T,Ns...> bit[N+1];
    template<typename... Args> void upd(int pos, Args... args) { assert(pos > 0);
        for (; pos<=N; pos+=pos&-pos) bit[pos].upd(args...); }
    template<typename... Args> T sum(int r, Args... args) {
        T res=0; for (;r;r-=r&-r) res += bit[r].query(args...);
        return res; }
    template<typename... Args> T query(int l, int r, Args...
            args) { return sum(r,args...)-sum(l-1,args...); }
};
const int N = 1e5+5;
int b, n, d, m;
struct point {
    int x, y, z;
    bool operator<(point o)const{
        return mt(x, y, z) < mt(o.x, o.y, o.z);
    }
} a[N];
void one() {
    for(int i = 0; i < n; ++i)
        cin >> a[i].x;
    sort(a,a+n);
    ll ans = 0;
    for(int i = 0; i < n; ++i){
        int j = lb(a,a+n, point{a[i].x-d, -inf, -inf})-a;
        ans += i-j;
    }
    cout << ans;
}
void two(){
    for(int i = 0; i < n; ++i){
        cin >> a[i].x >> a[i].y;
        tie(a[i].x, a[i].y) = mp(a[i].x+a[i].y, a[i].y-a[i].x+75'000);
    }
    sort(a,a+n);
    BIT<int, 150'005> bit;
    ll ans = 0;
    int j = 0;
    for(int i = 0; i < n; ++i){
        while(a[i].x-a[j].x>d){
            bit.upd(a[j].y, -1);
            ++j;
        }
        ans += bit.query(max(1, a[i].y-d), min(150'004, a[i].y+d));
        bit.upd(a[i].y, 1);
    }
    cout << ans << el;
}
BIT<int, 250, 250, 250> bit2;
void three() {
    vc<array<int, 4>> a(n);
    for(int i = 0; i < n; ++i){
        int x, y, z;
        cin >> x >> y >> z;
        a[i] = {x+y+z, x+y-z+74, x-y+z+74, x-y-z+150};
    }
    sort(all(a));
#ifdef _DEBUG
    clog << "Line " << __LINE__ << ":\n";
    for(int i = 0; i < n; ++i)
        clog << a[i][0] << sp << a[i][1] << sp << a[i][2] << sp << a[i][3] << el;
    clog << el;
#endif
    ll ans = 0;
    int j = 0;
    for(int i = 0; i < n; ++i){
        while(a[i][0] - a[j][0] > d){
            bit2.upd(a[j][1], a[j][2], a[j][3], -1);
            ++j;
        }
        ans += bit2.query(max(1, a[i][1]-d), min(249, a[i][1]+d),
                          max(1, a[i][2]-d), min(249, a[i][2]+d),
                          max(1, a[i][3]-d), min(249, a[i][3]+d));
//                          max(1, a[i][2]-d), max(1, a[i][3]-d),
//                         min(249, a[i][1]+d), min(249, a[i][2]+d), min(249, a[i][3]+d));
        bit2.upd(a[i][1], a[i][2], a[i][3], 1);
//        for(int j = a[i].x; j > 0 && a[i].x-j <= d; --j){
//            int r = d - (a[i].x-j);
//            ans += bitg2[j].query(max(a[i].y-r, 1), max(a[i].z-r, 1),
//                    min(154, a[i].y+r), min(154, a[i].y+r));
//        }
//        bitg2[a[i].x].upd(a[i].y, a[i].z, 1);
    }
    cout << ans;
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout << fixed; clog << fixed; clog << unitbuf;
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif
    cin >> b >> n >> d >> m;
    if(b==1)
        one();
    else if(b==2)
        two();
    else
        three();
}
| # | 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... |