This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// when B=1, can just use sliding window
// when B=2, need to convert coordinates to (x-y, x+y)
// add event x-y-D, query event x-y, remove event x-y+D
// add/remove point at (x+y)
// maintain fenwick tree and query range [x+y-D, x+y+D]
// when B=3, need to convert coordinates to (x-y, x+y-z, x+y+z)
// add event x-y-D, query event x-y, remove event x-y+D
// add/remove point at (x+y-z) X (x+y+z)
// maintain 2d fenwick tree and query ranges [x+y-z-D, x+y-z+D] X [x+y+z-D, x+y+z+D]
struct BIT {
vector<int> value;
BIT(int size) { value.resize(size); }
void update(int i, int x) {
while (i < value.size()) {
value[i] += x;
i += i & (-i);
}
}
int query(int i) {
int sum = 0;
while (i > 0) {
sum += value[i];
i -= i & (-i);
}
return sum;
}
int query(int l, int r) {
return query(min((int)value.size()-1, r)) - query(max(0, l-1));
}
};
struct BIT2 {
vector<BIT> value;
BIT2(int size, int size2) { value.resize(size, BIT(size2)); }
void update(int i, int j, int x) {
while (i < value.size()) {
value[i].update(j, x);
i += i & (-i);
}
}
int query(int i, int jl, int jr) {
int sum = 0;
while (i > 0) {
sum += value[i].query(jl, jr);
i -= i & (-i);
}
return sum;
}
int query(int il, int ir, int jl, int jr) {
return query(min((int)value.size()-1, ir), jl, jr) - query(max(0, il-1), jl, jr);
}
};
int main() {
int B, N, D, M;
cin >> B >> N >> D >> M;
if (B == 1) {
// sweep line with 0D BIT
vector<array<int,2>> events;
for (int i=0; i<N; i++) {
int X;
cin >> X;
events.push_back({X+D+1, -1});
events.push_back({X, 0});
events.push_back({X-D-1, +1});
}
sort(events.begin(), events.end());
ll ans = 0;
int curr = 0;
for (array<int,2> ev : events) {
if (ev[1] == 0) {
ans += curr - 1;
} else {
curr += ev[1];
}
}
cout << ans/2 << "\n";
} else if (B == 2) {
// sweep line with 1D BIT
BIT tree(M+M+1);
vector<array<int,3>> events;
for (int i=0; i<N; i++) {
int X, Y;
cin >> X >> Y;
events.push_back({X-Y+D+1, -1, X+Y});
events.push_back({X-Y, 0, X+Y});
events.push_back({X-Y-D-1, +1, X+Y});
}
sort(events.begin(), events.end());
ll ans = 0;
for (array<int,3> ev : events) {
if (ev[1] == 0) {
ans += tree.query(ev[2]-D, ev[2]+D) - 1;
} else {
tree.update(ev[2], ev[1]);
}
}
cout << ans/2 << "\n";
} else {
// sweep line with 2D BIT
BIT2 tree(M+M+M+1, M+M+M+1);
vector<array<int,4>> events;
for (int i=0; i<N; i++) {
int X, Y, Z;
cin >> X >> Y >> Z;
events.push_back({X-Y+D+1, -1, X+Y-Z+M, X+Y+Z});
events.push_back({X-Y, 0, X+Y-Z+M, X+Y+Z});
events.push_back({X-Y-D-1, +1, X+Y-Z+M, X+Y+Z});
}
sort(events.begin(), events.end());
ll ans = 0;
for (array<int,4> ev : events) {
if (ev[1] == 0) {
ans += tree.query(ev[2]-D, ev[2]+D, ev[3]-D, ev[3]+D) - 1;
} else {
tree.update(ev[2], ev[3], ev[1]);
}
}
cout << ans/2 << "\n";
}
return 0;
}
Compilation message (stderr)
pairs.cpp: In member function 'void BIT::update(int, int)':
pairs.cpp:22:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | while (i < value.size()) {
| ~~^~~~~~~~~~~~~~
pairs.cpp: In member function 'void BIT2::update(int, int, int)':
pairs.cpp:44:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<BIT>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | while (i < value.size()) {
| ~~^~~~~~~~~~~~~~
# | 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... |