#include <bits/stdc++.h>
using namespace std;
struct Fenwick2D {
int n, m;
vector<vector<int>> bit;
vector<vector<int>> f;
Fenwick2D(int n, int m) : n(n), m(m), bit(n + 1), f(n + 1) {}
void fakeAdd(int i0, int j0) {
for (int i = i0 + 1; i <= n; i += i & -i) {
for (int j = j0 + 1; j <= m; j += j & -j) {
f[i].push_back(j);
}
}
}
void work() {
for (int i = 1; i <= n; i++) {
f[i].push_back(0);
sort(f[i].begin(), f[i].end());
f[i].resize(unique(f[i].begin(), f[i].end()) - f[i].begin());
bit[i].resize(f[i].size(), 0);
}
}
void add(int i0, int j0, int x) {
for (int i = i0 + 1; i <= n; i += i & -i) {
for (int j = lower_bound(f[i].begin(), f[i].end(), j0 + 1) - f[i].begin(); j < (int)f[i].size(); j += j & -j) {
bit[i][j] += x;
}
}
}
int sum(int i0, int j0) {
int res = 0;
for (int i = i0; i > 0; i -= i & -i) {
for (int j = upper_bound(f[i].begin(), f[i].end(), j0) - f[i].begin() - 1; j > 0; j -= j & -j) {
res += bit[i][j];
}
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
constexpr int C = 1e9;
int n, q;
cin >> n >> q;
vector<array<int, 2>> a(n);
vector<int> xs, ys;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
x = C - x;
y = C - y;
a[i] = {x, y};
xs.push_back(x);
ys.push_back(y);
}
sort(a.begin(), a.end(), [&](array<int, 2> x, array<int, 2> y) { return x[0] + x[1] < y[0] + y[1]; });
vector<array<int, 4>> queries(q);
vector<int> ans(q);
for (int i = 0; i < q; i++) {
int x, y, z;
cin >> x >> y >> z;
x = C - x + 1;
y = C - y + 1;
queries[i] = {x, y, z, i};
}
sort(queries.begin(), queries.end(), [&](array<int, 4> x, array<int, 4> y) { return x[2] > y[2]; });
sort(xs.begin(), xs.end());
sort(ys.begin(), ys.end());
xs.resize(unique(xs.begin(), xs.end()) - xs.begin());
ys.resize(unique(ys.begin(), ys.end()) - ys.begin());
int X = xs.size();
int Y = ys.size();
Fenwick2D fen(X, Y);
for (auto &[x, y] : a) {
x = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
y = lower_bound(ys.begin(), ys.end(), y) - ys.begin();
fen.fakeAdd(x, y);
}
fen.work();
int ptr = 0;
for (auto [x, y, z, id] : queries) {
while (ptr < n && C + C - xs[a[ptr][0]] - ys[a[ptr][1]] >= z) {
auto [x, y] = a[ptr];
fen.add(x, y, +1);
ptr++;
}
x = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
y = lower_bound(ys.begin(), ys.end(), y) - ys.begin();
ans[id] = fen.sum(x, y);
}
for (int x : ans) {
cout << x << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
10 ms |
1776 KB |
Output is correct |
8 |
Correct |
9 ms |
1628 KB |
Output is correct |
9 |
Correct |
9 ms |
1784 KB |
Output is correct |
10 |
Correct |
5 ms |
1116 KB |
Output is correct |
11 |
Correct |
6 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
464 KB |
Output is correct |
13 |
Correct |
9 ms |
1628 KB |
Output is correct |
14 |
Correct |
9 ms |
1624 KB |
Output is correct |
15 |
Correct |
9 ms |
1736 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
635 ms |
54624 KB |
Output is correct |
2 |
Correct |
616 ms |
54548 KB |
Output is correct |
3 |
Correct |
620 ms |
54460 KB |
Output is correct |
4 |
Correct |
181 ms |
19428 KB |
Output is correct |
5 |
Correct |
197 ms |
14424 KB |
Output is correct |
6 |
Correct |
58 ms |
6852 KB |
Output is correct |
7 |
Correct |
605 ms |
54548 KB |
Output is correct |
8 |
Correct |
623 ms |
54748 KB |
Output is correct |
9 |
Correct |
542 ms |
54636 KB |
Output is correct |
10 |
Correct |
123 ms |
8596 KB |
Output is correct |
11 |
Correct |
99 ms |
14536 KB |
Output is correct |
12 |
Correct |
34 ms |
4720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
635 ms |
54624 KB |
Output is correct |
2 |
Correct |
616 ms |
54548 KB |
Output is correct |
3 |
Correct |
620 ms |
54460 KB |
Output is correct |
4 |
Correct |
181 ms |
19428 KB |
Output is correct |
5 |
Correct |
197 ms |
14424 KB |
Output is correct |
6 |
Correct |
58 ms |
6852 KB |
Output is correct |
7 |
Correct |
605 ms |
54548 KB |
Output is correct |
8 |
Correct |
623 ms |
54748 KB |
Output is correct |
9 |
Correct |
542 ms |
54636 KB |
Output is correct |
10 |
Correct |
123 ms |
8596 KB |
Output is correct |
11 |
Correct |
99 ms |
14536 KB |
Output is correct |
12 |
Correct |
34 ms |
4720 KB |
Output is correct |
13 |
Correct |
694 ms |
54444 KB |
Output is correct |
14 |
Correct |
745 ms |
54576 KB |
Output is correct |
15 |
Correct |
652 ms |
54620 KB |
Output is correct |
16 |
Correct |
169 ms |
19400 KB |
Output is correct |
17 |
Correct |
213 ms |
14772 KB |
Output is correct |
18 |
Correct |
62 ms |
7048 KB |
Output is correct |
19 |
Correct |
696 ms |
54628 KB |
Output is correct |
20 |
Correct |
666 ms |
54640 KB |
Output is correct |
21 |
Correct |
735 ms |
54540 KB |
Output is correct |
22 |
Correct |
125 ms |
9960 KB |
Output is correct |
23 |
Correct |
113 ms |
14444 KB |
Output is correct |
24 |
Correct |
38 ms |
4716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
10 ms |
1776 KB |
Output is correct |
8 |
Correct |
9 ms |
1628 KB |
Output is correct |
9 |
Correct |
9 ms |
1784 KB |
Output is correct |
10 |
Correct |
5 ms |
1116 KB |
Output is correct |
11 |
Correct |
6 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
464 KB |
Output is correct |
13 |
Correct |
9 ms |
1628 KB |
Output is correct |
14 |
Correct |
9 ms |
1624 KB |
Output is correct |
15 |
Correct |
9 ms |
1736 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
635 ms |
54624 KB |
Output is correct |
20 |
Correct |
616 ms |
54548 KB |
Output is correct |
21 |
Correct |
620 ms |
54460 KB |
Output is correct |
22 |
Correct |
181 ms |
19428 KB |
Output is correct |
23 |
Correct |
197 ms |
14424 KB |
Output is correct |
24 |
Correct |
58 ms |
6852 KB |
Output is correct |
25 |
Correct |
605 ms |
54548 KB |
Output is correct |
26 |
Correct |
623 ms |
54748 KB |
Output is correct |
27 |
Correct |
542 ms |
54636 KB |
Output is correct |
28 |
Correct |
123 ms |
8596 KB |
Output is correct |
29 |
Correct |
99 ms |
14536 KB |
Output is correct |
30 |
Correct |
34 ms |
4720 KB |
Output is correct |
31 |
Correct |
694 ms |
54444 KB |
Output is correct |
32 |
Correct |
745 ms |
54576 KB |
Output is correct |
33 |
Correct |
652 ms |
54620 KB |
Output is correct |
34 |
Correct |
169 ms |
19400 KB |
Output is correct |
35 |
Correct |
213 ms |
14772 KB |
Output is correct |
36 |
Correct |
62 ms |
7048 KB |
Output is correct |
37 |
Correct |
696 ms |
54628 KB |
Output is correct |
38 |
Correct |
666 ms |
54640 KB |
Output is correct |
39 |
Correct |
735 ms |
54540 KB |
Output is correct |
40 |
Correct |
125 ms |
9960 KB |
Output is correct |
41 |
Correct |
113 ms |
14444 KB |
Output is correct |
42 |
Correct |
38 ms |
4716 KB |
Output is correct |
43 |
Correct |
798 ms |
78168 KB |
Output is correct |
44 |
Correct |
833 ms |
78248 KB |
Output is correct |
45 |
Correct |
778 ms |
78168 KB |
Output is correct |
46 |
Correct |
230 ms |
28512 KB |
Output is correct |
47 |
Correct |
226 ms |
19808 KB |
Output is correct |
48 |
Correct |
65 ms |
7840 KB |
Output is correct |
49 |
Correct |
715 ms |
77488 KB |
Output is correct |
50 |
Correct |
745 ms |
78244 KB |
Output is correct |
51 |
Correct |
667 ms |
77916 KB |
Output is correct |
52 |
Correct |
131 ms |
11712 KB |
Output is correct |
53 |
Correct |
118 ms |
22456 KB |
Output is correct |