/*
* 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;
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() {
if (k>=3*m) return n*(n-1)/2;
vector<grid> a(n);
for (auto &u: a) {
cin >> u.x >> u.y >> u.z; u.x--, u.y--, u.z--;
u.a=u.y+u.z;
u.b=u.y-u.z + m;
}
vector<vector<vector<int>>> prefix(m, vector<vector<int>>(2*m+1, vector<int>(2*m+1, 0)));
for (auto &u: a) prefix[u.x][u.a][u.b]++;
for (int x=0; x<m; x++) {
for (int i=0; i<=2*m; i++) {
for (int j=0; j<=2*m; j++) {
if (j>0) prefix[x][i][j] += prefix[x][i][j-1];
if (i>0) prefix[x][i][j] += prefix[x][i-1][j];
if (i>0 && j>0) prefix[x][i][j] -= prefix[x][i-1][j-1];
}
}
}
int ans=0;
for (auto &u: a) {
int cnt=0;
for (int x= max(0LL, u.x - k); x<= min(m-1, u.x + k); x++) {
int lowa = max(0LL, u.a - (k - abs(u.x - x))), higha = min(2*m, u.a + (k - abs(u.x - x)));
int lowb = max(0LL, u.b - (k - abs(u.x - x))), highb = min(2*m, u.b + (k - abs(u.x - x)));
int res=prefix[x][higha][highb];
if (lowa>0) res -= prefix[x][lowa-1][highb];
if (lowb>0) res -= prefix[x][higha][lowb-1];
if (lowa>0 && lowb>0) res += prefix[x][lowa-1][lowb-1];
cnt+=res;
}
ans+=cnt-1;
}
return ans/2;
}
void solve() {
cin >> b >> n >> k >> m;
cout << (b==1 ? sub1() : (b==2 ? sub2() : sub3())) << endl;
return;
}
| # | 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... |