#include<bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT, int SZ> using AR = array<T,SZ>;
tcT, int... Ns> struct BIT {
T val = 0; void upd(T v) { val += v; }
T query() { return val; }
};
tcT, 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...);
}
};
int B, N, D, M;
BIT<int, 305, 305, 305> bit1;
void solve() {
cin>>B>>N>>D>>M;
ll ans = 0;
if(B == 1) {
vi A(N);
F0R(i,N) cin>>A[i];
sort(all(A));
int lo = 0, hi = 0;
F0R(i,N) {
while(hi+1<N && A[hi+1]-A[i] <= D) hi++;
while(lo<N && A[i]-A[lo] > D) lo++;
ans += hi-lo;
}
}
else if(B == 2) {
BIT<int, 150005> bit;
V<AR<int,2>> A(N);
F0R(i,N) {
int x, y;
cin>>x>>y;
A[i] = {x+y, x-y};
}
sort(all(A));
int lo = 0, hi = -1;
F0R(i,N) {
while(hi+1<N && A[hi+1][0] - A[i][0] <= D) bit.upd(A[++hi][1]+M+1, 1);
while(lo<N && A[i][0] - A[lo][0] > D) bit.upd(A[lo++][1]+M+1, -1);
ans += bit.query(max(1, A[i][1]-D+M+1), min(2*M+1, A[i][1]+D+M+1))-1;
}
}
else if(B == 3) {
V<AR<int,4>> A(N);
F0R(i,N) {
int x, y, z;
cin>>x>>y>>z;
A[i] = {x+y+z, x+y-z, x-y+z, x-y-z};
}
sort(all(A));
int lo = 0, hi = -1;
F0R(i,N) {
while(hi+1<N && A[hi+1][0] - A[i][0] <= D) {
hi++;
bit1.upd(A[hi][1]+2*M+1, A[hi][2]+2*M+1, A[hi][3]+2*M+1, 1);
}
while(lo<N && A[i][0] - A[lo][0] > D) {
bit1.upd(A[lo][1]+2*M+1, A[lo][2]+2*M+1, A[lo][3]+2*M+1, -1);
lo++;
}
ans += bit1.query(max(1, A[i][1]-D+2*M+1), min(4*M+1, A[i][1]+D+2*M+1), max(1, A[i][2]-D+2*M+1), min(4*M+1, A[i][2]+D+2*M+1), max(1, A[i][3]-D+2*M+1), min(4*M+1, A[i][3]+D+2*M+1))-1;
}
}
cout << ans/2 << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message
pairs.cpp: In function 'void setIO(std::string)':
pairs.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1696 KB |
Output is correct |
2 |
Correct |
12 ms |
1696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
2072 KB |
Output is correct |
2 |
Correct |
17 ms |
2064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
2076 KB |
Output is correct |
2 |
Correct |
19 ms |
2132 KB |
Output is correct |
3 |
Correct |
18 ms |
2196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
2196 KB |
Output is correct |
2 |
Correct |
26 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2388 KB |
Output is correct |
2 |
Correct |
38 ms |
2460 KB |
Output is correct |
3 |
Correct |
37 ms |
2464 KB |
Output is correct |
4 |
Correct |
30 ms |
2468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
2844 KB |
Output is correct |
2 |
Correct |
34 ms |
2720 KB |
Output is correct |
3 |
Correct |
33 ms |
2724 KB |
Output is correct |
4 |
Correct |
34 ms |
2712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15188 KB |
Output is correct |
2 |
Correct |
9 ms |
15260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
4536 KB |
Output is correct |
2 |
Correct |
96 ms |
4556 KB |
Output is correct |
3 |
Correct |
64 ms |
4504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
23976 KB |
Output is correct |
2 |
Correct |
186 ms |
24044 KB |
Output is correct |
3 |
Correct |
91 ms |
24100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
33324 KB |
Output is correct |
2 |
Correct |
214 ms |
33356 KB |
Output is correct |
3 |
Correct |
115 ms |
33356 KB |
Output is correct |