#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y, z;
};
struct Query {
int a, b, c, id;
};
const int B = 850;
const int MAXSZ = 6e5+9;
int bit[MAXSZ]; // binary/fenwick indexed tree
int sz; // bit size
int bit_sum(int r) {
int sum = 0;
for (++r; r > 0; r -= r&-r)
sum += bit[r];
return sum;
}
int bit_sum(int l, int r) {
return bit_sum(r) - bit_sum(l-1);
}
void bit_add(int r, int delta) {
for (++r; r <= sz; r += r&-r)
bit[r] += delta;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, Q;
cin >> N >> Q;
set<int> values;
vector<Point> pts(N);
for (int i = 0; i < N; ++i) {
Point& p = pts[i];
cin >> p.x >> p.y;
p.z = p.x + p.y;
values.insert(p.x);
values.insert(p.y);
values.insert(p.z);
}
vector<Query> qs(Q);
for (int i = 0; i < Q; ++i) {
Query& q = qs[i];
cin >> q.a >> q.b >> q.c;
q.id = i;
values.insert(q.a);
values.insert(q.b);
values.insert(q.c);
}
unordered_map<int,int> comp;
for (int v : values) {
comp[v] = comp.size();
}
for (Query& q : qs) {
q.a = comp[q.a];
q.b = comp[q.b];
q.c = comp[q.c];
}
for (Point& p : pts) {
p.x = comp[p.x];
p.y = comp[p.y];
p.z = comp[p.z];
}
sz = comp.size() + 1;
sort(qs.begin(), qs.end(), [](const Query& l, const Query& r) {
return l.c < r.c;
});
sort(pts.begin(), pts.end(), [](const Point& l, const Point& r) {
return l.z < r.z;
});
int blocks = (N+B-1)/B;
vector<vector<Query>> block_queries(blocks);
int j = 0;
for (int block = 1; block < blocks; ++block) {
int i = B*block;
while (j < Q and qs[j].c <= pts[i].z) {
block_queries[block-1].push_back(qs[j]);
++j;
}
}
while (j < Q) {
block_queries.back().push_back(qs[j]);
++j;
}
vector<int> ans(Q);
for (int block = 0; block < blocks; ++block) {
if (block_queries[block].empty()) continue;
memset(bit, 0, sizeof(int)*sz);
vector<Point> in_block;
in_block.reserve(B);
for (int i = B*block; i < N and i < B*(block+1); ++i) {
in_block.push_back(pts[i]);
}
vector<Point> over_block;
over_block.reserve(N-B*block);
for (int i = B*(block+1); i < N; ++i) {
over_block.push_back(pts[i]);
bit_add(pts[i].y, 1);
}
sort(over_block.begin(), over_block.end(), [&](const Point& l, const Point& r) {
return l.x > r.x;
});
sort(block_queries[block].begin(), block_queries[block].end(), [&](const Query& l, const Query& r) {
return l.a < r.a;
});
for (const Query& q : block_queries[block]) {
int cnt_in = 0;
int cnt_over = 0;
// points in block
for (const Point& p : in_block) {
if (p.x >= q.a and p.y >= q.b and p.z >= q.c) ++cnt_in;
}
// points over block
while ((not over_block.empty()) and over_block.back().x < q.a) {
const Point& p = over_block.back();
bit_add(p.y, -1);
over_block.pop_back();
}
cnt_over = bit_sum(q.b, sz-1);
ans[q.id] = cnt_in + cnt_over;
}
}
for (int i = 0; i < Q; ++i) {
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
260 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
14 ms |
2184 KB |
Output is correct |
8 |
Correct |
20 ms |
2188 KB |
Output is correct |
9 |
Correct |
14 ms |
2132 KB |
Output is correct |
10 |
Correct |
16 ms |
1620 KB |
Output is correct |
11 |
Correct |
14 ms |
1668 KB |
Output is correct |
12 |
Correct |
7 ms |
468 KB |
Output is correct |
13 |
Correct |
18 ms |
1876 KB |
Output is correct |
14 |
Correct |
11 ms |
1876 KB |
Output is correct |
15 |
Correct |
11 ms |
1876 KB |
Output is correct |
16 |
Correct |
6 ms |
1108 KB |
Output is correct |
17 |
Correct |
8 ms |
1364 KB |
Output is correct |
18 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
19224 KB |
Output is correct |
2 |
Correct |
376 ms |
19332 KB |
Output is correct |
3 |
Correct |
283 ms |
19256 KB |
Output is correct |
4 |
Correct |
224 ms |
15960 KB |
Output is correct |
5 |
Correct |
326 ms |
15932 KB |
Output is correct |
6 |
Correct |
118 ms |
6200 KB |
Output is correct |
7 |
Correct |
375 ms |
19140 KB |
Output is correct |
8 |
Correct |
353 ms |
19104 KB |
Output is correct |
9 |
Correct |
443 ms |
18848 KB |
Output is correct |
10 |
Correct |
241 ms |
15280 KB |
Output is correct |
11 |
Correct |
199 ms |
15268 KB |
Output is correct |
12 |
Correct |
139 ms |
6080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
19224 KB |
Output is correct |
2 |
Correct |
376 ms |
19332 KB |
Output is correct |
3 |
Correct |
283 ms |
19256 KB |
Output is correct |
4 |
Correct |
224 ms |
15960 KB |
Output is correct |
5 |
Correct |
326 ms |
15932 KB |
Output is correct |
6 |
Correct |
118 ms |
6200 KB |
Output is correct |
7 |
Correct |
375 ms |
19140 KB |
Output is correct |
8 |
Correct |
353 ms |
19104 KB |
Output is correct |
9 |
Correct |
443 ms |
18848 KB |
Output is correct |
10 |
Correct |
241 ms |
15280 KB |
Output is correct |
11 |
Correct |
199 ms |
15268 KB |
Output is correct |
12 |
Correct |
139 ms |
6080 KB |
Output is correct |
13 |
Correct |
1138 ms |
21736 KB |
Output is correct |
14 |
Correct |
868 ms |
19540 KB |
Output is correct |
15 |
Correct |
269 ms |
19348 KB |
Output is correct |
16 |
Correct |
754 ms |
16440 KB |
Output is correct |
17 |
Correct |
714 ms |
16444 KB |
Output is correct |
18 |
Correct |
216 ms |
6988 KB |
Output is correct |
19 |
Correct |
1027 ms |
21764 KB |
Output is correct |
20 |
Correct |
1043 ms |
20656 KB |
Output is correct |
21 |
Correct |
1060 ms |
21780 KB |
Output is correct |
22 |
Correct |
183 ms |
15300 KB |
Output is correct |
23 |
Correct |
192 ms |
15292 KB |
Output is correct |
24 |
Correct |
114 ms |
6132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
260 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
14 ms |
2184 KB |
Output is correct |
8 |
Correct |
20 ms |
2188 KB |
Output is correct |
9 |
Correct |
14 ms |
2132 KB |
Output is correct |
10 |
Correct |
16 ms |
1620 KB |
Output is correct |
11 |
Correct |
14 ms |
1668 KB |
Output is correct |
12 |
Correct |
7 ms |
468 KB |
Output is correct |
13 |
Correct |
18 ms |
1876 KB |
Output is correct |
14 |
Correct |
11 ms |
1876 KB |
Output is correct |
15 |
Correct |
11 ms |
1876 KB |
Output is correct |
16 |
Correct |
6 ms |
1108 KB |
Output is correct |
17 |
Correct |
8 ms |
1364 KB |
Output is correct |
18 |
Correct |
3 ms |
468 KB |
Output is correct |
19 |
Correct |
342 ms |
19224 KB |
Output is correct |
20 |
Correct |
376 ms |
19332 KB |
Output is correct |
21 |
Correct |
283 ms |
19256 KB |
Output is correct |
22 |
Correct |
224 ms |
15960 KB |
Output is correct |
23 |
Correct |
326 ms |
15932 KB |
Output is correct |
24 |
Correct |
118 ms |
6200 KB |
Output is correct |
25 |
Correct |
375 ms |
19140 KB |
Output is correct |
26 |
Correct |
353 ms |
19104 KB |
Output is correct |
27 |
Correct |
443 ms |
18848 KB |
Output is correct |
28 |
Correct |
241 ms |
15280 KB |
Output is correct |
29 |
Correct |
199 ms |
15268 KB |
Output is correct |
30 |
Correct |
139 ms |
6080 KB |
Output is correct |
31 |
Correct |
1138 ms |
21736 KB |
Output is correct |
32 |
Correct |
868 ms |
19540 KB |
Output is correct |
33 |
Correct |
269 ms |
19348 KB |
Output is correct |
34 |
Correct |
754 ms |
16440 KB |
Output is correct |
35 |
Correct |
714 ms |
16444 KB |
Output is correct |
36 |
Correct |
216 ms |
6988 KB |
Output is correct |
37 |
Correct |
1027 ms |
21764 KB |
Output is correct |
38 |
Correct |
1043 ms |
20656 KB |
Output is correct |
39 |
Correct |
1060 ms |
21780 KB |
Output is correct |
40 |
Correct |
183 ms |
15300 KB |
Output is correct |
41 |
Correct |
192 ms |
15292 KB |
Output is correct |
42 |
Correct |
114 ms |
6132 KB |
Output is correct |
43 |
Correct |
1671 ms |
62060 KB |
Output is correct |
44 |
Correct |
1638 ms |
62096 KB |
Output is correct |
45 |
Correct |
1467 ms |
61760 KB |
Output is correct |
46 |
Correct |
1101 ms |
44848 KB |
Output is correct |
47 |
Correct |
1043 ms |
44848 KB |
Output is correct |
48 |
Correct |
202 ms |
6504 KB |
Output is correct |
49 |
Correct |
1540 ms |
61908 KB |
Output is correct |
50 |
Correct |
1761 ms |
62084 KB |
Output is correct |
51 |
Correct |
1509 ms |
61756 KB |
Output is correct |
52 |
Correct |
798 ms |
34328 KB |
Output is correct |
53 |
Correct |
347 ms |
26260 KB |
Output is correct |