#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
const int MAX_Q = 100000;
const int MAX_AIB = MAX_N + MAX_Q;
typedef struct Node {
Node *l, *r;
int key, prio, size;
Node(int k) {
key = k;
prio = rand();
size = 1;
l = r = NULL;
}
void refresh() {
size = 1;
if(l != NULL) size += l->size;
if(r != NULL) size += r->size;
}
} *Treap;
Treap join(Treap a, Treap b) {
if(a == NULL) return b;
if(b == NULL) return a;
if(a->prio < b->prio) {
a->r = join(a->r, b);
a->refresh();
return a;
} else {
b->l = join(a, b->l);
b->refresh();
return b;
}
}
void split(Treap t, int val, Treap &l, Treap &r) {
l = r = NULL;
if(t == NULL) return;
if(t->key < val) {
split(t->r, val, t->r, r);
t->refresh();
l = t;
} else {
split(t->l, val, l, t->l);
t->refresh();
r = t;
}
}
Treap add(Treap t, int x) {
Treap l, r;
split(t, x, l, r);
return join(join(l, new Node(x)), r);
}
struct Student {
int t, s, sum;
} v[MAX_N];
struct Query {
int a, b, c;
int *rez;
} query[MAX_Q];
int rez[MAX_Q];
bool cmp1(Student a, Student b) {
return a.sum > b.sum;
}
bool cmp2(Query a, Query b) {
return a.c > b.c;
}
Treap aib[1+MAX_AIB];
void init2dstructure() {
for(int i = 0; i <= MAX_AIB; ++i)
aib[i] = NULL;
}
static inline int lsb(int x) {
return x & (-x);
}
void updatePoint(int x, int y) {
while(x <= MAX_AIB) {
aib[x] = add(aib[x], y);
x += lsb(x);
}
}
int queryRectangle(int a, int b) {
int rez = 0;
while(a > 0) {
Treap l, r;
split(aib[a], b + 1, l, r);
if(l != NULL)
rez = rez + l->size;
aib[a] = join(l, r);
a -= lsb(a);
}
return rez;
}
bool cmp(int *a, int *b) {
return *a < *b;
}
int *coordA[MAX_AIB], *coordB[MAX_AIB];
void normalize(int n, int **v) {
sort(v, v + n, cmp);
int last = *v[0], j = MAX_AIB;
for(int i = 0; i < n; ++i)
if(*v[i] == last)
*v[i] = j;
else {
last = *v[i];
*v[i] = --j;
}
}
int main() {
#ifdef HOME
FILE *fin = fopen("input.in", "r");
FILE *fout = fopen("output.out", "w");
#else
FILE *fin = stdin;
FILE *fout = stdout;
#endif
int n, q;
int top = 0;
fscanf(fin, "%d%d", &n, &q);
for(int i = 0; i < n; ++i) {
fscanf(fin, "%d%d", &v[i].t, &v[i].s);
v[i].sum = v[i].t + v[i].s;
coordA[top] = &v[i].t;
coordB[top++] = &v[i].s;
}
for(int i = 0; i < q; ++i) {
fscanf(fin, "%d%d%d", &query[i].a, &query[i].b, &query[i].c);
query[i].rez = &rez[i];
coordA[top] = &query[i].a;
coordB[top++] = &query[i].b;
}
normalize(top, coordA);
normalize(top, coordB);
sort(v, v + n, cmp1);
sort(query, query + q, cmp2);
init2dstructure();
int lastup = 0;
for(int i = 0; i < q; ++i) {
while(lastup < n && v[lastup].sum >= query[i].c) {
updatePoint(v[lastup].t, v[lastup].s);
++lastup;
}
*query[i].rez = queryRectangle(query[i].a, query[i].b);
}
for(int i = 0; i < q; ++i)
fprintf(fout, "%d\n", rez[i]);
fclose(fin);
fclose(fout);
return 0;
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:145:8: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d%d", &n, &q);
~~~~~~^~~~~~~~~~~~~~~~~~~~~
examination.cpp:147:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d%d", &v[i].t, &v[i].s);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:153:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d%d%d", &query[i].a, &query[i].b, &query[i].c);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1920 KB |
Output is correct |
2 |
Correct |
3 ms |
2048 KB |
Output is correct |
3 |
Correct |
3 ms |
1920 KB |
Output is correct |
4 |
Correct |
3 ms |
1920 KB |
Output is correct |
5 |
Correct |
3 ms |
1920 KB |
Output is correct |
6 |
Correct |
3 ms |
1920 KB |
Output is correct |
7 |
Correct |
13 ms |
3072 KB |
Output is correct |
8 |
Correct |
13 ms |
3072 KB |
Output is correct |
9 |
Correct |
15 ms |
3072 KB |
Output is correct |
10 |
Correct |
14 ms |
3072 KB |
Output is correct |
11 |
Correct |
13 ms |
2560 KB |
Output is correct |
12 |
Correct |
12 ms |
2560 KB |
Output is correct |
13 |
Correct |
17 ms |
3072 KB |
Output is correct |
14 |
Correct |
16 ms |
3064 KB |
Output is correct |
15 |
Correct |
15 ms |
3072 KB |
Output is correct |
16 |
Correct |
8 ms |
2292 KB |
Output is correct |
17 |
Correct |
11 ms |
3072 KB |
Output is correct |
18 |
Correct |
8 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2021 ms |
50280 KB |
Output is correct |
2 |
Correct |
2067 ms |
50980 KB |
Output is correct |
3 |
Correct |
2025 ms |
51048 KB |
Output is correct |
4 |
Correct |
1241 ms |
50808 KB |
Output is correct |
5 |
Correct |
526 ms |
22040 KB |
Output is correct |
6 |
Correct |
284 ms |
22156 KB |
Output is correct |
7 |
Correct |
1782 ms |
51900 KB |
Output is correct |
8 |
Correct |
1552 ms |
50808 KB |
Output is correct |
9 |
Correct |
1094 ms |
51932 KB |
Output is correct |
10 |
Correct |
201 ms |
14604 KB |
Output is correct |
11 |
Correct |
536 ms |
50680 KB |
Output is correct |
12 |
Correct |
111 ms |
14456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2021 ms |
50280 KB |
Output is correct |
2 |
Correct |
2067 ms |
50980 KB |
Output is correct |
3 |
Correct |
2025 ms |
51048 KB |
Output is correct |
4 |
Correct |
1241 ms |
50808 KB |
Output is correct |
5 |
Correct |
526 ms |
22040 KB |
Output is correct |
6 |
Correct |
284 ms |
22156 KB |
Output is correct |
7 |
Correct |
1782 ms |
51900 KB |
Output is correct |
8 |
Correct |
1552 ms |
50808 KB |
Output is correct |
9 |
Correct |
1094 ms |
51932 KB |
Output is correct |
10 |
Correct |
201 ms |
14604 KB |
Output is correct |
11 |
Correct |
536 ms |
50680 KB |
Output is correct |
12 |
Correct |
111 ms |
14456 KB |
Output is correct |
13 |
Correct |
1585 ms |
50768 KB |
Output is correct |
14 |
Correct |
2163 ms |
50856 KB |
Output is correct |
15 |
Correct |
1964 ms |
51088 KB |
Output is correct |
16 |
Correct |
926 ms |
50952 KB |
Output is correct |
17 |
Correct |
393 ms |
22168 KB |
Output is correct |
18 |
Correct |
313 ms |
22216 KB |
Output is correct |
19 |
Correct |
1559 ms |
50788 KB |
Output is correct |
20 |
Correct |
1791 ms |
50884 KB |
Output is correct |
21 |
Correct |
1181 ms |
51428 KB |
Output is correct |
22 |
Correct |
199 ms |
14768 KB |
Output is correct |
23 |
Correct |
527 ms |
50928 KB |
Output is correct |
24 |
Correct |
115 ms |
14584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1920 KB |
Output is correct |
2 |
Correct |
3 ms |
2048 KB |
Output is correct |
3 |
Correct |
3 ms |
1920 KB |
Output is correct |
4 |
Correct |
3 ms |
1920 KB |
Output is correct |
5 |
Correct |
3 ms |
1920 KB |
Output is correct |
6 |
Correct |
3 ms |
1920 KB |
Output is correct |
7 |
Correct |
13 ms |
3072 KB |
Output is correct |
8 |
Correct |
13 ms |
3072 KB |
Output is correct |
9 |
Correct |
15 ms |
3072 KB |
Output is correct |
10 |
Correct |
14 ms |
3072 KB |
Output is correct |
11 |
Correct |
13 ms |
2560 KB |
Output is correct |
12 |
Correct |
12 ms |
2560 KB |
Output is correct |
13 |
Correct |
17 ms |
3072 KB |
Output is correct |
14 |
Correct |
16 ms |
3064 KB |
Output is correct |
15 |
Correct |
15 ms |
3072 KB |
Output is correct |
16 |
Correct |
8 ms |
2292 KB |
Output is correct |
17 |
Correct |
11 ms |
3072 KB |
Output is correct |
18 |
Correct |
8 ms |
2304 KB |
Output is correct |
19 |
Correct |
2021 ms |
50280 KB |
Output is correct |
20 |
Correct |
2067 ms |
50980 KB |
Output is correct |
21 |
Correct |
2025 ms |
51048 KB |
Output is correct |
22 |
Correct |
1241 ms |
50808 KB |
Output is correct |
23 |
Correct |
526 ms |
22040 KB |
Output is correct |
24 |
Correct |
284 ms |
22156 KB |
Output is correct |
25 |
Correct |
1782 ms |
51900 KB |
Output is correct |
26 |
Correct |
1552 ms |
50808 KB |
Output is correct |
27 |
Correct |
1094 ms |
51932 KB |
Output is correct |
28 |
Correct |
201 ms |
14604 KB |
Output is correct |
29 |
Correct |
536 ms |
50680 KB |
Output is correct |
30 |
Correct |
111 ms |
14456 KB |
Output is correct |
31 |
Correct |
1585 ms |
50768 KB |
Output is correct |
32 |
Correct |
2163 ms |
50856 KB |
Output is correct |
33 |
Correct |
1964 ms |
51088 KB |
Output is correct |
34 |
Correct |
926 ms |
50952 KB |
Output is correct |
35 |
Correct |
393 ms |
22168 KB |
Output is correct |
36 |
Correct |
313 ms |
22216 KB |
Output is correct |
37 |
Correct |
1559 ms |
50788 KB |
Output is correct |
38 |
Correct |
1791 ms |
50884 KB |
Output is correct |
39 |
Correct |
1181 ms |
51428 KB |
Output is correct |
40 |
Correct |
199 ms |
14768 KB |
Output is correct |
41 |
Correct |
527 ms |
50928 KB |
Output is correct |
42 |
Correct |
115 ms |
14584 KB |
Output is correct |
43 |
Correct |
1479 ms |
53780 KB |
Output is correct |
44 |
Correct |
1494 ms |
53900 KB |
Output is correct |
45 |
Correct |
1836 ms |
53748 KB |
Output is correct |
46 |
Correct |
1032 ms |
53736 KB |
Output is correct |
47 |
Correct |
389 ms |
22124 KB |
Output is correct |
48 |
Correct |
267 ms |
21876 KB |
Output is correct |
49 |
Correct |
1336 ms |
56420 KB |
Output is correct |
50 |
Correct |
1002 ms |
53880 KB |
Output is correct |
51 |
Correct |
1103 ms |
56512 KB |
Output is correct |
52 |
Correct |
193 ms |
14840 KB |
Output is correct |
53 |
Correct |
492 ms |
53508 KB |
Output is correct |