이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define int long long
const int N = 1e6;
int fen[N];
void modif(int u, int val) {
for(int i = u + 1; i < N; i += i & -i) fen[i] += val;
}
int query(int l, int r) {
int ans = 0;
for(int i = r + 1; i >= 1; i -= i & -i) ans += fen[i];
for(int i = l; i >= 1; i -= i & -i) ans -= fen[i];
return ans;
}
const int K = 500, offset = 200;
int ugh[K][K][K];
void modif(int a, int b, int c, int val) {
for(int i = a + offset; i < K; i += i & -i) {
for(int j = b + offset; j < K; j += j & -j) {
for(int f = c + offset; f < K; f += f & -f) {
ugh[i][j][f] += val;
}
}
}
}
int query(int x1, int y1, int z1, int x2, int y2, int z2) {
x1 += offset, x2 += offset, y1 += offset, y2 += offset, z1 += offset, z2 += offset;
int ans = 0;
for(int i = x2; i >= 1; i -= i & -i) {
for(int j = y2; j >= 1; j -= j & -j) {
for(int k = z2; k >= 1; k -= k & -k) {
ans += ugh[i][j][k];
}
for(int k = z1 - 1; k >= 1; k -= k & -k) {
ans -= ugh[i][j][k];
}
}
for(int j = y1 - 1; j >= 1; j -= j & -j) {
for(int k = z2; k >= 1; k -= k & -k) {
ans -= ugh[i][j][k];
}
for(int k = z1 - 1; k >= 1; k -= k & -k) {
ans += ugh[i][j][k];
}
}
}
for(int i = x1 - 1; i >= 1; i -= i & -i) {
for(int j = y2; j >= 1; j -= j & -j) {
for(int k = z2; k >= 1; k -= k & -k) {
ans -= ugh[i][j][k];
}
for(int k = z1 - 1; k >= 1; k -= k & -k) {
ans += ugh[i][j][k];
}
}
for(int j = y1 - 1; j >= 1; j -= j & -j) {
for(int k = z2; k >= 1; k -= k & -k) {
ans += ugh[i][j][k];
}
for(int k = z1 - 1; k >= 1; k -= k & -k) {
ans -= ugh[i][j][k];
}
}
}
return ans;
}
void solve() {
int b, n, d, m; cin >> b >> n >> d >> m;
if(b == 1) {
vector<int> a(n);
for(int i = 0; i < n; ++i) cin >> a[i];
sort(all(a));
int ans = 0;
for(int i = 0; i < n; ++i) {
int j = upper_bound(all(a), a[i] + d) - a.begin();
ans += j - i - 1;
}
cout << ans << "\n";
} else if(b == 2) {
vector<pair<int, int>> a;
for(int i = 0; i < n; ++i) {
int x, y; cin >> x >> y;
a.pb({x - y, x + y});
}
queue<int> q;
sort(all(a));
int ans = 0;
for(int i = 0; i < n; ++i) {
while(!q.empty() && abs(a[i].first - a[q.front()].first) > d) {
modif(a[q.front()].second, -1);
q.pop();
}
ans += query(a[i].second - d, min(a[i].second + d, N - 2));
q.push(i);
modif(a[i].second, 1);
}
cout << ans << "\n";
} else {
vector<array<int, 4>> a;
for(int i = 0; i < n; ++i) {
int x, y, z; cin >> x >> y >> z;
a.pb({x + y + z, x + y - z, x - y + z, x - y - z});
}
sort(all(a));
queue<int> q;
int ans = 0;
for(int i = 0; i < n; ++i) {
while(!q.empty() && abs(a[i][0] - a[q.front()][0]) > d) {
modif(a[q.front()][1], a[q.front()][2], a[q.front()][3], -1);
q.pop();
}
ans += query(a[i][1] - d, a[i][2] - d, a[i][3] - d, min(a[i][1] + d, K - offset - 1), min(a[i][2] + d, K - offset - 1), min(a[i][3] + d, K - offset - 1));
modif(a[i][1], a[i][2], a[i][3], 1);
q.push(i);
}
cout << ans << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
# | 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... |