#include <stdio.h>
#define N 100000
#define Q 100000
#define M (N + Q)
#define H_ 18 /* H_ = ceil(log2(M)) */
#define N1 (N * (H_ + 1) + 1)
#define N_ (1 << H_)
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[M], yy[M], zz[M];
int *ww;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = ww[ii[j]] != ww[i_] ? ww[ii[j]] - ww[i_] : i_ - ii[j];
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
int vv[N1], ll[N1], rr[N1], sz[N1], ii_[N1], u_, l_, r_;
int node(int i) {
static int _ = 1;
vv[_] = rand_();
sz[_] = 1;
ii_[_] = i;
return _++;
}
void pul(int u) {
sz[u] = sz[ll[u]] + sz[rr[u]] + 1;
}
void split(int u, int i) {
if (u == 0) {
u_ = l_ = r_ = 0;
return;
}
if (yy[ii_[u]] < yy[i]) {
split(rr[u], i);
rr[u] = l_, l_ = u;
} else if (yy[ii_[u]] > yy[i]) {
split(ll[u], i);
ll[u] = r_, r_ = u;
} else {
u_ = u, l_ = ll[u], r_ = rr[u];
ll[u] = rr[u] = 0;
}
pul(u);
}
int merge(int u, int v) {
if (u == 0)
return v;
if (v == 0)
return u;
if (vv[u] < vv[v]) {
rr[u] = merge(rr[u], v), pul(u);
return u;
} else {
ll[v] = merge(u, ll[v]), pul(v);
return v;
}
}
int tr_add(int u, int i) {
split(u, i);
return merge(merge(l_, node(i)), r_);
}
int tr_query(int u, int y) {
int k = 0;
while (u)
if (yy[ii_[u]] >= y)
k += sz[rr[u]] + 1, u = ll[u];
else
u = rr[u];
return k;
}
int uu[N_ * 2], n_;
void update(int i, int i_) {
for (i += n_; i > 0; i >>= 1)
uu[i] = tr_add(uu[i], i_);
}
int query(int l, int y) {
int r = n_ - 1, k = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((l & 1) == 1)
k += tr_query(uu[l++], y);
return k;
}
int main() {
static int ii[M], ans[Q];
int n, q, m, h, i, i_;
scanf("%d%d", &n, &q), m = n + q;
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i], &yy[i]), zz[i] = xx[i] + yy[i];
for (h = 0; h < q; h++)
scanf("%d%d%d", &xx[n + h], &yy[n + h], &zz[n + h]);
for (i = 0; i < m; i++)
ii[i] = i;
ww = xx, sort(ii, 0, m);
for (i = 0; i < m; i++)
xx[ii[i]] = i;
ww = yy, sort(ii, 0, m);
for (i = 0; i < m; i++)
yy[ii[i]] = i;
ww = zz, sort(ii, 0, m);
n_ = 1;
while (n_ < m)
n_ <<= 1;
for (i = m - 1; i >= 0; i--) {
i_ = ii[i];
if (i_ < n)
update(xx[i_], i_);
else
ans[i_ - n] = query(xx[i_], yy[i_]);
}
for (h = 0; h < q; h++)
printf("%d\n", ans[h]);
return 0;
}
Compilation message
examination.c: In function 'main':
examination.c:125:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | scanf("%d%d", &n, &q), m = n + q;
| ^~~~~~~~~~~~~~~~~~~~~
examination.c:127:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d%d", &xx[i], &yy[i]), zz[i] = xx[i] + yy[i];
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
examination.c:129:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%d%d%d", &xx[n + h], &yy[n + h], &zz[n + h]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
11 ms |
1492 KB |
Output is correct |
8 |
Correct |
11 ms |
1404 KB |
Output is correct |
9 |
Correct |
11 ms |
1492 KB |
Output is correct |
10 |
Correct |
12 ms |
1364 KB |
Output is correct |
11 |
Correct |
9 ms |
1384 KB |
Output is correct |
12 |
Correct |
10 ms |
1276 KB |
Output is correct |
13 |
Correct |
15 ms |
1460 KB |
Output is correct |
14 |
Correct |
12 ms |
1448 KB |
Output is correct |
15 |
Correct |
16 ms |
1456 KB |
Output is correct |
16 |
Correct |
7 ms |
1408 KB |
Output is correct |
17 |
Correct |
14 ms |
1364 KB |
Output is correct |
18 |
Correct |
6 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1763 ms |
45616 KB |
Output is correct |
2 |
Correct |
1776 ms |
45620 KB |
Output is correct |
3 |
Correct |
1841 ms |
45524 KB |
Output is correct |
4 |
Correct |
1497 ms |
44836 KB |
Output is correct |
5 |
Correct |
966 ms |
44444 KB |
Output is correct |
6 |
Correct |
722 ms |
43544 KB |
Output is correct |
7 |
Correct |
1589 ms |
45508 KB |
Output is correct |
8 |
Correct |
1468 ms |
45532 KB |
Output is correct |
9 |
Correct |
1311 ms |
45492 KB |
Output is correct |
10 |
Correct |
712 ms |
43912 KB |
Output is correct |
11 |
Correct |
1138 ms |
44576 KB |
Output is correct |
12 |
Correct |
327 ms |
42916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1763 ms |
45616 KB |
Output is correct |
2 |
Correct |
1776 ms |
45620 KB |
Output is correct |
3 |
Correct |
1841 ms |
45524 KB |
Output is correct |
4 |
Correct |
1497 ms |
44836 KB |
Output is correct |
5 |
Correct |
966 ms |
44444 KB |
Output is correct |
6 |
Correct |
722 ms |
43544 KB |
Output is correct |
7 |
Correct |
1589 ms |
45508 KB |
Output is correct |
8 |
Correct |
1468 ms |
45532 KB |
Output is correct |
9 |
Correct |
1311 ms |
45492 KB |
Output is correct |
10 |
Correct |
712 ms |
43912 KB |
Output is correct |
11 |
Correct |
1138 ms |
44576 KB |
Output is correct |
12 |
Correct |
327 ms |
42916 KB |
Output is correct |
13 |
Correct |
1535 ms |
46024 KB |
Output is correct |
14 |
Correct |
1856 ms |
45996 KB |
Output is correct |
15 |
Correct |
1833 ms |
45588 KB |
Output is correct |
16 |
Correct |
1412 ms |
45280 KB |
Output is correct |
17 |
Correct |
840 ms |
44608 KB |
Output is correct |
18 |
Correct |
703 ms |
43636 KB |
Output is correct |
19 |
Correct |
1653 ms |
46056 KB |
Output is correct |
20 |
Correct |
1675 ms |
45916 KB |
Output is correct |
21 |
Correct |
1385 ms |
46068 KB |
Output is correct |
22 |
Correct |
659 ms |
43908 KB |
Output is correct |
23 |
Correct |
1191 ms |
44728 KB |
Output is correct |
24 |
Correct |
299 ms |
42972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
11 ms |
1492 KB |
Output is correct |
8 |
Correct |
11 ms |
1404 KB |
Output is correct |
9 |
Correct |
11 ms |
1492 KB |
Output is correct |
10 |
Correct |
12 ms |
1364 KB |
Output is correct |
11 |
Correct |
9 ms |
1384 KB |
Output is correct |
12 |
Correct |
10 ms |
1276 KB |
Output is correct |
13 |
Correct |
15 ms |
1460 KB |
Output is correct |
14 |
Correct |
12 ms |
1448 KB |
Output is correct |
15 |
Correct |
16 ms |
1456 KB |
Output is correct |
16 |
Correct |
7 ms |
1408 KB |
Output is correct |
17 |
Correct |
14 ms |
1364 KB |
Output is correct |
18 |
Correct |
6 ms |
1364 KB |
Output is correct |
19 |
Correct |
1763 ms |
45616 KB |
Output is correct |
20 |
Correct |
1776 ms |
45620 KB |
Output is correct |
21 |
Correct |
1841 ms |
45524 KB |
Output is correct |
22 |
Correct |
1497 ms |
44836 KB |
Output is correct |
23 |
Correct |
966 ms |
44444 KB |
Output is correct |
24 |
Correct |
722 ms |
43544 KB |
Output is correct |
25 |
Correct |
1589 ms |
45508 KB |
Output is correct |
26 |
Correct |
1468 ms |
45532 KB |
Output is correct |
27 |
Correct |
1311 ms |
45492 KB |
Output is correct |
28 |
Correct |
712 ms |
43912 KB |
Output is correct |
29 |
Correct |
1138 ms |
44576 KB |
Output is correct |
30 |
Correct |
327 ms |
42916 KB |
Output is correct |
31 |
Correct |
1535 ms |
46024 KB |
Output is correct |
32 |
Correct |
1856 ms |
45996 KB |
Output is correct |
33 |
Correct |
1833 ms |
45588 KB |
Output is correct |
34 |
Correct |
1412 ms |
45280 KB |
Output is correct |
35 |
Correct |
840 ms |
44608 KB |
Output is correct |
36 |
Correct |
703 ms |
43636 KB |
Output is correct |
37 |
Correct |
1653 ms |
46056 KB |
Output is correct |
38 |
Correct |
1675 ms |
45916 KB |
Output is correct |
39 |
Correct |
1385 ms |
46068 KB |
Output is correct |
40 |
Correct |
659 ms |
43908 KB |
Output is correct |
41 |
Correct |
1191 ms |
44728 KB |
Output is correct |
42 |
Correct |
299 ms |
42972 KB |
Output is correct |
43 |
Correct |
1498 ms |
47952 KB |
Output is correct |
44 |
Correct |
1517 ms |
48140 KB |
Output is correct |
45 |
Correct |
1818 ms |
48144 KB |
Output is correct |
46 |
Correct |
1446 ms |
46624 KB |
Output is correct |
47 |
Correct |
806 ms |
45900 KB |
Output is correct |
48 |
Correct |
601 ms |
43380 KB |
Output is correct |
49 |
Correct |
1524 ms |
47924 KB |
Output is correct |
50 |
Correct |
1233 ms |
47820 KB |
Output is correct |
51 |
Correct |
1211 ms |
47784 KB |
Output is correct |
52 |
Correct |
648 ms |
45420 KB |
Output is correct |
53 |
Correct |
1126 ms |
45480 KB |
Output is correct |