#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
#define ii pair<int, int>
#define f first
#define s second
int b, n, d, m;
struct FT {
vector<int> ft;
int n;
void init(int _n) {
n = _n;
ft.assign(n+1, 0);
}
void upd(int k, int v) {
while (k <= n) {
ft[k] += v;
k += k&-k;
}
}
int qry(int k) {
k = min(k, n-1);
if (k <= 0) return 0;
int sum = 0;
while (k) {
sum += ft[k];
k -= k&-k;
}
return sum;
}
};
struct FT2D {
vector<FT> ft;
int X, Y;
void init(int _x, int _y) {
X = _x; Y = _y;
ft.resize(X+1);
for (int i = 0; i <= X; i++) {
ft[i].init(Y);
}
}
void upd(int x, int y, int v) {
if (x == 0 || y == 0) return;
x = min(x, X-1); y = min(y, Y-1);
while (x < X) {
ft[x].upd(y, v);
x += x&-x;
}
}
int qry(int x, int y) {
if (x <= 0 || y <= 0) return 0;
x = min(x, X-1); y = min(y, Y-1);
int sum = 0;
while (x) {
sum += ft[x].qry(y);
x -= x & -x;
}
return sum;
}
};
int main() {
cin.tie(0); ios_base::sync_with_stdio(false);
cin >> b >> n >> d >> m;
if (b == 1) {
Tree<pair<int, int>> TS;
long long ans = 0;
vector<int> nums;
for (int i = 0; i < n; i++) {
int x; cin >> x;
nums.push_back(x);
}
sort(nums.begin(), nums.end());
int ct = 0;
for (int x : nums) {
ans += ct-TS.order_of_key(make_pair(x-d, 0));
TS.insert(make_pair(x, ct++));
}
cout << ans << endl;
} else if (b == 2) {
vector<ii> nums;
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
nums.push_back(make_pair(x-y, x+y));
}
sort(nums.begin(), nums.end());
int l = 0;
FT ft; ft.init(2*m+1);
long long ans = 0;
for (int r = 0; r < n; r++) {
while (nums[r].f - nums[l].f > d) {
ft.upd(nums[l].s, -1);
l++;
}
ans += ft.qry(nums[r].s+d) - ft.qry(nums[r].s-d-1);
ft.upd(nums[r].s, 1);
}
cout << ans << endl;
} else {
vector<ii> nums[m+1];
for (int i = 0; i < n; i++) {
int x, y, z; cin >> x >> y >> z;
nums[z].push_back(make_pair(x-y+m, x+y));
}
FT2D ft[m+1];
for (int i = 1; i <= m; i++) {
ft[i].init(2*m+1, 2*m+1);
for (ii j : nums[i]) {
ft[i].upd(j.f, j.s, 1);
}
}
long long ans = 0;
for (int i = 1; i <= m; i++) {
for (ii j : nums[i]) {
for (int k = 1; k <= m; k++) {
int d2 = d - abs(k - i);
if (d2 <= 0) break;
ans += ft[k].qry(j.f + d2, j.s + d2) - ft[k].qry(j.f + d2, j.s - d2 - 1) - ft[k].qry(j.f - d2 - 1, j.s + d2) + ft[k].qry(j.f - d2 - 1, j.s - d2 - 1);
}
}
}
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
7076 KB |
Output is correct |
2 |
Correct |
96 ms |
7064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
7028 KB |
Output is correct |
2 |
Correct |
96 ms |
7028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
7024 KB |
Output is correct |
2 |
Correct |
103 ms |
7032 KB |
Output is correct |
3 |
Correct |
103 ms |
7028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
1532 KB |
Output is correct |
2 |
Correct |
31 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
1532 KB |
Output is correct |
2 |
Correct |
37 ms |
1532 KB |
Output is correct |
3 |
Correct |
36 ms |
1532 KB |
Output is correct |
4 |
Correct |
35 ms |
1532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
1776 KB |
Output is correct |
2 |
Correct |
43 ms |
1784 KB |
Output is correct |
3 |
Correct |
42 ms |
1776 KB |
Output is correct |
4 |
Correct |
49 ms |
1776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
7680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
2136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
5880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
423 ms |
9720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |