/*
* Author: Nonoze
* Created: Sunday 04/01/2026
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef DEBUG
#define dbg(...)
#endif
// #define cout cerr << "OUT: "
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)
template<typename T> void read(T& x) { cin >> x; }
template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second); }
template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); }
template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); }
template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; }
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(v) sort(all(v)), v.erase(unique(all(v)), (v).end())
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define QYES quit("YES")
#define QNO quit("NO")
#define int long long
#define double long double
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=20;
void solve();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
// cin >> tt;
while(tt--) solve();
return 0;
}
template<class T>
struct segtree {
vector<T> st;
int n;
T idElement;
segtree() {}
segtree(int nn, T idEl, vector<T> creation={}) {
n=nn, idElement=idEl;
st.resize(n*2, idElement);
if (!creation.empty()) {
for (int i=0; i<n; i++) st[i+n]=creation[i];
build();
}
}
void build() {
for (int i=n-1; i>0; i--) st[i] = combine(st[i<<1], st[i<<1|1]);
}
void update(int p, T val) {
for (st[p += n] += val; p > 1; p >>= 1) st[p>>1] = combine(st[p], st[p^1]);
}
T query(int l, int r) {
T resl = idElement, resr = idElement;
r++;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) resl = combine(resl, st[l++]);
if (r&1) resr = combine(st[--r], resr);
}
return combine(resl, resr);
}
T combine(T l, T r) {
T ans=l+r;
return ans;
}
};
struct grid {
int x, y, z;
int a, b;
};
int b, n, k, m;
int sub1() {
vector<int> a(n);
for (auto &u: a) cin >> u;
sort(all(a));
int pos=1, ans=0;
for (int i=0; i<n; i++) {
pos=max(pos, i+1);
while (pos<n && a[pos]-a[i]<=k) pos++;
pos--;
ans+=pos-i;
}
return ans;
}
int sub2() {
vector<grid> a(n);
for (auto &u: a) cin >> u.x >> u.y, --u.x, --u.y, u.a=u.x+u.y, u.b=u.x-u.y+m;
/*
dist(a, b) = max({
(a.x-b.x)+(a.y-b.y),
(a.x-b.x)+(b.y-a.y),
(b.x-a.x)+(a.y-b.y),
(b.x-a.x)+(b.y-a.y)
})
a.c=a.x+a.y, a.d=a.x-a.y
dist(a, b) = max({
(a.c - b.c),
(a.d - b.d),
(b.d - a.d),
(b.c - a.c)
})
*/
vector<vector<grid>> buckets(2*m+1); for (auto &u: a) buckets[u.a].pb(u);
segtree<int> st(2*m+1, 0);
int ans=0;
for (int i=0; i<=2*m; i++) {
if (i>k) for (auto &u: buckets[i-k-1]) st.update(u.b, -1);
for (auto &u: buckets[i]) {
assert(u.b>=0 && u.b<=2*m);
int low=max(0LL, u.b - k);
int high=min(2*m, u.b + k);
int res=st.query(low, high);
ans+=res;
st.update(u.b, 1);
}
}
return ans;
}
int sub3() {
vector<grid> a(n);
for (auto &u: a) {
cin >> u.x >> u.y >> u.z;
u.x--, u.y--, u.z--;
}
if (n<=10000) {
int ans=0;
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (abs(a[i].x-a[j].x)
+abs(a[i].y-a[j].y)
+abs(a[i].z-a[j].z)<=k) ans++;
}
}
cout << ans << endl;
} else {
int ans=0;
int comp=0;
vector<vector<vector<int>>> tab(m+1, vector<vector<int>>(m+1, vector<int>(m+1, 0)));
for (int i=0; i<n; i++) tab[a[i].x][a[i].y][a[i].z]++;
for (int i=0; i<n; i++) {
int x=a[i].x, y=a[i].y, z=a[i].z;
for (int a=max(0LL, x-k); a<=min(m, x+k); a++) {
int restant=k-abs(x-a);
for (int b=max(0LL, y-restant); b<=min(m, y+restant); b++) {
int res=restant-abs(y-b);
for (int c=max(0LL, z-res); c<=min(m, z+res); c++) {
if (tab[a][b][c]>1) comp++;
ans+=tab[a][b][c];
}
}
}
ans--;
}
cout << ans/2 << endl;
}
}
void solve() {
cin >> b >> n >> k >> m;
cout << (b==1 ? sub1() : (b==2 ? sub2() : sub3())) << endl;
return;
}
Compilation message (stderr)
pairs.cpp: In function 'long long int sub3()':
pairs.cpp:202:1: warning: no return statement in function returning non-void [-Wreturn-type]
202 | }
| ^| # | 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... |