#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define int long long
#define ld double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)
int B, N, D, M;
int n;
vector<vector<int>> rows;
struct SegmentTree {
vector<vector<int>> st;
SegmentTree(int N_) {
n = N_;
st.assign(4*n, vector<int>(0));
build();
}
void build(int id=1, int l=0, int r=n) {
if(l+1 == r) {
st[id] = rows[l];
return;
}
int mid = (l+r)/2;
build(id*2 , l, mid);
build(id*2+1, mid, r);
merge(st[id*2].begin(), st[id*2].end(), st[id*2+1].begin(), st[id*2+1].end(), back_inserter(st[id]));
}
int query(int r1, int c1, int r2, int c2, int id=1, int l=0, int r=n) {
if(l >= r1 && r <= r2) {
return upper_bound(st[id].begin(), st[id].end(), c2)-lower_bound(st[id].begin(), st[id].end(), c1);
}
if(l >= r2 || r <= r1) return 0;
int mid = (l+r)/2;
return query(r1, c1, r2, c2, id*2 , l, mid) + query(r1, c1, r2, c2, id*2+1, mid, r);
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> B >> N >> D >> M;
if(B == 1) {
vector<int> a(N);
for(auto &x: a)
cin >> x;
sort(a.begin(), a.end());
int ans = 0;
for(auto x: a)
ans += upper_bound(a.begin(), a.end(), x+D)-lower_bound(a.begin(), a.end(), x-D)-1;
cout << ans/2 << endl;
}
if(B == 2) {
vector<pair<int, int>> a(N);
rows.assign(2*M-1, vector<int>(0));
for(int i = 0; i < N; i++) {
int x, y;
cin >> x >> y;
x--; y--;
rows[x+y].push_back(M-1-(x-y));
a[i] = {x+y, M-1-(x-y)};
}
for(auto &v: rows)
sort(v.begin(), v.end());
SegmentTree st(2*M-1);
int ans = 0;
for(auto p: a) {
ans += st.query(p.first-D, p.second-D, p.first+D+1, p.second+D)-1;
int cur = 0;
for(auto q: a)
if(q != p && max(abs(p.first-q.first), abs(p.second-q.second)) <= D) {
cur++;
}
}
// if(ans%2) return -1;
cout << (ans)/2 << endl;
}
if(B == 3) {
vector<vector<vector<int>>> grid(M*3, vector<vector<int>>(M*3, vector<int>(M*2, 0)));
vector<pair<int, pair<int, int>>> a(N);
for(int i = 0; i < N; i++) {
int x, y, z;
cin >> x >> y >> z;
x--; y--; z--;
a[i] = {x+y+z+1, {M*2-1+x-y-z+1, M*2-1+y-z+1}};
grid[x+y+z + 1][M*2-1 + x-y-z + 1][M-1 + y-z + 1]++;
}
for(int x = 1; x < M*3; x++) {
for(int y = 1; y < M*3; y++) {
for(int z = 1; z < M*2; z++) {
grid[x][y][z] += grid[x-1][y][z];
grid[x][y][z] += grid[x][y-1][z];
grid[x][y][z] += grid[x][y][z-1];
grid[x][y][z] -= grid[x-1][y-1][z];
grid[x][y][z] -= grid[x][y-1][z-1];
grid[x][y][z] -= grid[x-1][y][z-1];
grid[x][y][z] += grid[x-1][y-1][z-1];
}
}
}
int ans = 0;
for(auto p: a) {
int x, y, z;
x = p.first; y = p.second.first; z = p.second.second;
int x1 = max(0ll, x-D-1);
int y1 = max(0ll, y-D-1);
int z1 = max(0ll, z-D-1);
int x2 = min(M*3-1, x+D);
int y2 = min(M*3-1, y+D);
int z2 = min(M*2-1, z+D);
ans += grid[x2][y2][z2];
ans -= grid[x1][y2][z2];
ans -= grid[x2][y1][z2];
ans -= grid[x2][y2][z1];
ans += grid[x1][y1][z2];
ans += grid[x2][y1][z1];
ans += grid[x1][y2][z1];
ans -= grid[x1][y1][z1];
}
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
1240 KB |
Output is correct |
2 |
Correct |
23 ms |
1284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
1308 KB |
Output is correct |
2 |
Correct |
31 ms |
1308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
1308 KB |
Output is correct |
2 |
Correct |
37 ms |
1308 KB |
Output is correct |
3 |
Correct |
39 ms |
1308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
18460 KB |
Output is correct |
2 |
Correct |
29 ms |
18592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
18592 KB |
Output is correct |
2 |
Correct |
57 ms |
18592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
432 ms |
18592 KB |
Output is correct |
2 |
Correct |
337 ms |
18592 KB |
Output is correct |
3 |
Correct |
297 ms |
18592 KB |
Output is correct |
4 |
Correct |
276 ms |
18592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
702 ms |
42484 KB |
Output is correct |
2 |
Correct |
603 ms |
42484 KB |
Output is correct |
3 |
Correct |
221 ms |
42484 KB |
Output is correct |
4 |
Correct |
326 ms |
42484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
93 ms |
62440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
62440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
112 ms |
62440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
191 ms |
67032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |