#include <bits/stdc++.h>
#define pb push_back
using namespace std;
struct BITree {
int size;
vector<int> tree;
void init(int N) {
size = N;
tree.assign(size, 0);
}
void add(int i, int v) {
for (int idx = i; idx < size; idx = idx | (idx + 1)) {
tree[idx] += v;
}
}
int get(int i) {
int res = 0;
for (int idx = i; idx >= 0; idx = (idx & (idx + 1)) - 1) {
res += tree[idx];
}
return res;
}
int get(int l, int r) {
return get(r - 1) - get(l - 1);
}
int rget(int i) {
return get(i, size);
}
};
const int STUDENT = 0;
const int QUERY = 1;
struct Event {
int type;
int x, y;
int z;
// For queries only
int id = -1;
};
bool sumsort(const Event& a, const Event& b)
{
if (a.z == b.z) {
// We want to count a student that has grades equal to a query
return a.type < b.type;
}
return a.z > b.z;
}
bool xsort(const Event& a, const Event& b)
{
return a.x > b.x;
}
int N, Q;
vector<int> ycords;
vector<int> ans;
BITree b;
int yfind(int v)
{
int pos = lower_bound(ycords.begin(), ycords.end(), v) - ycords.begin();
return pos;
}
void add_student(int y, int v)
{
int pos = yfind(y);
b.add(pos, v);
}
void update_query(int y, int id)
{
int pos = yfind(y);
int res = b.rget(pos);
ans[id] += res;
}
void cdq_dq(vector<Event>& events)
{
if (events.size() <= 1) {
return;
}
int mid = events.size() / 2;
vector<Event> e1(mid), e2(events.size() - mid);
for (int i = 0; i < mid; i++) {
e1[i] = events[i];
}
for (int i = mid; i < events.size(); i++) {
e2[i - mid] = events[i];
}
cdq_dq(e1);
cdq_dq(e2);
// Check how the STUDENTS of e1 affect the QUERIES of e2
sort(e1.begin(), e1.end(), xsort);
sort(e2.begin(), e2.end(), xsort);
int curp = 0;
for (int i = 0; i < e2.size(); i++) {
if (e2[i].type == STUDENT) {
continue;
}
while (curp < e1.size() && e1[curp].x >= e2[i].x) {
if (e1[curp].type == STUDENT) {
add_student(e1[curp].y, 1);
}
curp++;
}
update_query(e2[i].y, e2[i].id);
}
while (curp > 0) {
if (e1[curp - 1].type == STUDENT) {
add_student(e1[curp - 1].y, -1);
}
curp--;
}
}
int main(void)
{
scanf("%d %d", &N, &Q);
vector<Event> initial(N + Q);
ans.assign(Q, 0);
for (int i = 0; i < N; i++) {
int x, y;
scanf("%d %d", &x, &y);
initial[i].type = STUDENT;
initial[i].x = x;
initial[i].y = y;
initial[i].z = x + y;
initial[i].id = -1;
ycords.pb(initial[i].y);
}
for (int i = 0; i < Q; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
initial[i + N].type = QUERY;
initial[i + N].x = x;
initial[i + N].y = y;
initial[i + N].z = z;
initial[i + N].id = i;
ycords.pb(initial[i + N].y);
}
sort(ycords.begin(), ycords.end());
ycords.resize(unique(ycords.begin(), ycords.end()) - ycords.begin());
b.init(ycords.size());
sort(initial.begin(), initial.end(), sumsort);
cdq_dq(initial);
for (int i = 0; i < Q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
Compilation message
examination.cpp: In function 'void cdq_dq(std::vector<Event>&)':
examination.cpp:99:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = mid; i < events.size(); i++) {
| ~~^~~~~~~~~~~~~~~
examination.cpp:111:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (int i = 0; i < e2.size(); i++) {
| ~~^~~~~~~~~~~
examination.cpp:116:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | while (curp < e1.size() && e1[curp].x >= e2[i].x) {
| ~~~~~^~~~~~~~~~~
examination.cpp: In function 'int main()':
examination.cpp:136:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | scanf("%d %d", &N, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~
examination.cpp:142:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
examination.cpp:154:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
154 | scanf("%d %d %d", &x, &y, &z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
10 ms |
860 KB |
Output is correct |
8 |
Correct |
10 ms |
860 KB |
Output is correct |
9 |
Correct |
10 ms |
856 KB |
Output is correct |
10 |
Correct |
7 ms |
860 KB |
Output is correct |
11 |
Correct |
9 ms |
856 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
860 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
860 KB |
Output is correct |
17 |
Correct |
6 ms |
932 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
401 ms |
16192 KB |
Output is correct |
2 |
Correct |
411 ms |
16292 KB |
Output is correct |
3 |
Correct |
406 ms |
16328 KB |
Output is correct |
4 |
Correct |
255 ms |
15084 KB |
Output is correct |
5 |
Correct |
331 ms |
15424 KB |
Output is correct |
6 |
Correct |
200 ms |
14584 KB |
Output is correct |
7 |
Correct |
388 ms |
16068 KB |
Output is correct |
8 |
Correct |
370 ms |
15980 KB |
Output is correct |
9 |
Correct |
369 ms |
16140 KB |
Output is correct |
10 |
Correct |
275 ms |
15560 KB |
Output is correct |
11 |
Correct |
244 ms |
15064 KB |
Output is correct |
12 |
Correct |
149 ms |
14336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
401 ms |
16192 KB |
Output is correct |
2 |
Correct |
411 ms |
16292 KB |
Output is correct |
3 |
Correct |
406 ms |
16328 KB |
Output is correct |
4 |
Correct |
255 ms |
15084 KB |
Output is correct |
5 |
Correct |
331 ms |
15424 KB |
Output is correct |
6 |
Correct |
200 ms |
14584 KB |
Output is correct |
7 |
Correct |
388 ms |
16068 KB |
Output is correct |
8 |
Correct |
370 ms |
15980 KB |
Output is correct |
9 |
Correct |
369 ms |
16140 KB |
Output is correct |
10 |
Correct |
275 ms |
15560 KB |
Output is correct |
11 |
Correct |
244 ms |
15064 KB |
Output is correct |
12 |
Correct |
149 ms |
14336 KB |
Output is correct |
13 |
Correct |
537 ms |
16632 KB |
Output is correct |
14 |
Correct |
482 ms |
16580 KB |
Output is correct |
15 |
Correct |
397 ms |
16200 KB |
Output is correct |
16 |
Correct |
321 ms |
15500 KB |
Output is correct |
17 |
Correct |
428 ms |
15924 KB |
Output is correct |
18 |
Correct |
211 ms |
14616 KB |
Output is correct |
19 |
Correct |
544 ms |
16632 KB |
Output is correct |
20 |
Correct |
540 ms |
16612 KB |
Output is correct |
21 |
Correct |
525 ms |
16492 KB |
Output is correct |
22 |
Correct |
276 ms |
15608 KB |
Output is correct |
23 |
Correct |
213 ms |
15060 KB |
Output is correct |
24 |
Correct |
149 ms |
14300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
10 ms |
860 KB |
Output is correct |
8 |
Correct |
10 ms |
860 KB |
Output is correct |
9 |
Correct |
10 ms |
856 KB |
Output is correct |
10 |
Correct |
7 ms |
860 KB |
Output is correct |
11 |
Correct |
9 ms |
856 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
860 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
860 KB |
Output is correct |
17 |
Correct |
6 ms |
932 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
19 |
Correct |
401 ms |
16192 KB |
Output is correct |
20 |
Correct |
411 ms |
16292 KB |
Output is correct |
21 |
Correct |
406 ms |
16328 KB |
Output is correct |
22 |
Correct |
255 ms |
15084 KB |
Output is correct |
23 |
Correct |
331 ms |
15424 KB |
Output is correct |
24 |
Correct |
200 ms |
14584 KB |
Output is correct |
25 |
Correct |
388 ms |
16068 KB |
Output is correct |
26 |
Correct |
370 ms |
15980 KB |
Output is correct |
27 |
Correct |
369 ms |
16140 KB |
Output is correct |
28 |
Correct |
275 ms |
15560 KB |
Output is correct |
29 |
Correct |
244 ms |
15064 KB |
Output is correct |
30 |
Correct |
149 ms |
14336 KB |
Output is correct |
31 |
Correct |
537 ms |
16632 KB |
Output is correct |
32 |
Correct |
482 ms |
16580 KB |
Output is correct |
33 |
Correct |
397 ms |
16200 KB |
Output is correct |
34 |
Correct |
321 ms |
15500 KB |
Output is correct |
35 |
Correct |
428 ms |
15924 KB |
Output is correct |
36 |
Correct |
211 ms |
14616 KB |
Output is correct |
37 |
Correct |
544 ms |
16632 KB |
Output is correct |
38 |
Correct |
540 ms |
16612 KB |
Output is correct |
39 |
Correct |
525 ms |
16492 KB |
Output is correct |
40 |
Correct |
276 ms |
15608 KB |
Output is correct |
41 |
Correct |
213 ms |
15060 KB |
Output is correct |
42 |
Correct |
149 ms |
14300 KB |
Output is correct |
43 |
Correct |
596 ms |
19028 KB |
Output is correct |
44 |
Correct |
598 ms |
19028 KB |
Output is correct |
45 |
Correct |
520 ms |
18980 KB |
Output is correct |
46 |
Correct |
313 ms |
16668 KB |
Output is correct |
47 |
Correct |
465 ms |
17536 KB |
Output is correct |
48 |
Correct |
224 ms |
14452 KB |
Output is correct |
49 |
Correct |
518 ms |
18972 KB |
Output is correct |
50 |
Correct |
567 ms |
18932 KB |
Output is correct |
51 |
Correct |
532 ms |
18780 KB |
Output is correct |
52 |
Correct |
403 ms |
17608 KB |
Output is correct |
53 |
Correct |
225 ms |
15812 KB |
Output is correct |