#include <stdio.h>
#include <string.h>
#define N 200000
#define H_ 18 /* H_ = ceil(log2(N)) */
#define N_ (1 << H_ + 1)
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N * 2];
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)
if (xx[ii[j]] == xx[i_])
j++;
else if (xx[ii[j]] < xx[i_]) {
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 st[N_ * 2], lz[N_], h_, n_;
void build(int n) {
int i;
h_ = 0;
while (1 << h_ < n * 2)
h_++;
n_ = 1 << h_;
memset(lz, -1, n_ * sizeof *lz);
for (i = 0; i < n_ * 2; i++)
st[i] = n;
}
void put(int i, int x) {
st[i] = x;
if (i < n_)
lz[i] = x;
}
void pus(int i) {
if (lz[i] != -1)
put(i << 1 | 0, lz[i]), put(i << 1 | 1, lz[i]), lz[i] = -1;
}
void pul(int i) {
if (lz[i] == -1)
st[i] = min(st[i << 1 | 0], st[i << 1 | 1]);
}
void push(int i) {
int h;
for (h = h_; h > 0; h--)
pus(i >> h);
}
void pull(int i) {
while (i > 1)
pul(i >>= 1);
}
void update(int l, int r, int x) {
int l_ = l += n_, r_ = r += n_;
push(l_), push(r_);
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
put(l++, x);
if ((r & 1) == 0)
put(r--, x);
}
pull(l_), pull(r_);
}
int query(int l, int r) {
int x = INF;
push(l += n_), push(r += n_);
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x = min(x, st[l++]);
if ((r & 1) == 0)
x = min(x, st[r--]);
}
return x;
}
int main() {
static int ii[N * 2], jj[H_ + 1][N];
int n, q, h, i, j, x;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1]), xx[i << 1 | 1]++;
for (i = 0; i < n * 2; i++)
ii[i] = i;
sort(ii, 0, n * 2);
for (i = 0, x = 0; i < n * 2; i++)
xx[ii[i]] = i + 1 == n * 2 || xx[ii[i + 1]] != xx[ii[i]] ? x++ : x;
build(n);
for (i = n - 1; i >= 0; i--) {
jj[0][i] = query(xx[i << 1 | 0], xx[i << 1 | 1] - 1);
update(xx[i << 1 | 0], xx[i << 1 | 1] - 1, i);
}
jj[0][n] = n;
for (i = n - 1; i >= 0; i--)
jj[0][i] = min(jj[0][i], jj[0][i + 1]);
for (h = 1; h <= H_; h++)
for (i = 0; i <= n; i++)
jj[h][i] = jj[h - 1][jj[h - 1][i]];
scanf("%d", &q);
while (q--) {
int k;
scanf("%d%d", &i, &j), i--, j--;
k = 0;
for (h = H_; h >= 0; h--)
if (jj[h][i] <= j)
i = jj[h][i], k += 1 << h;
k++;
printf("%d\n", k);
}
return 0;
}
Compilation message
Main.c:6:15: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
6 | #define N_ (1 << H_ + 1)
| ^~
Main.c:39:8: note: in expansion of macro 'N_'
39 | int st[N_ * 2], lz[N_], h_, n_;
| ^~
Main.c:6:15: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
6 | #define N_ (1 << H_ + 1)
| ^~
Main.c:39:20: note: in expansion of macro 'N_'
39 | int st[N_ * 2], lz[N_], h_, n_;
| ^~
Main.c: In function 'main':
Main.c:111:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:113:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | scanf("%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1]), xx[i << 1 | 1]++;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:130:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
Main.c:134:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
23580 KB |
Output is correct |
2 |
Correct |
215 ms |
23720 KB |
Output is correct |
3 |
Correct |
213 ms |
23800 KB |
Output is correct |
4 |
Correct |
222 ms |
23908 KB |
Output is correct |
5 |
Correct |
227 ms |
24784 KB |
Output is correct |
6 |
Correct |
136 ms |
23120 KB |
Output is correct |
7 |
Correct |
139 ms |
23348 KB |
Output is correct |
8 |
Correct |
144 ms |
23548 KB |
Output is correct |
9 |
Correct |
151 ms |
24092 KB |
Output is correct |
10 |
Correct |
142 ms |
23420 KB |
Output is correct |
11 |
Correct |
161 ms |
24348 KB |
Output is correct |
12 |
Correct |
151 ms |
23236 KB |
Output is correct |
13 |
Correct |
148 ms |
23140 KB |
Output is correct |
14 |
Correct |
165 ms |
24388 KB |
Output is correct |
15 |
Correct |
157 ms |
23900 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
143 ms |
24108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
972 KB |
Output is correct |
2 |
Correct |
6 ms |
972 KB |
Output is correct |
3 |
Correct |
6 ms |
1036 KB |
Output is correct |
4 |
Correct |
6 ms |
972 KB |
Output is correct |
5 |
Correct |
7 ms |
1020 KB |
Output is correct |
6 |
Correct |
5 ms |
1044 KB |
Output is correct |
7 |
Correct |
4 ms |
976 KB |
Output is correct |
8 |
Correct |
4 ms |
1056 KB |
Output is correct |
9 |
Correct |
4 ms |
972 KB |
Output is correct |
10 |
Correct |
4 ms |
972 KB |
Output is correct |
11 |
Correct |
5 ms |
972 KB |
Output is correct |
12 |
Correct |
5 ms |
912 KB |
Output is correct |
13 |
Correct |
5 ms |
972 KB |
Output is correct |
14 |
Correct |
4 ms |
980 KB |
Output is correct |
15 |
Correct |
6 ms |
980 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
972 KB |
Output is correct |
2 |
Correct |
6 ms |
972 KB |
Output is correct |
3 |
Correct |
6 ms |
1036 KB |
Output is correct |
4 |
Correct |
6 ms |
972 KB |
Output is correct |
5 |
Correct |
7 ms |
1020 KB |
Output is correct |
6 |
Correct |
5 ms |
1044 KB |
Output is correct |
7 |
Correct |
4 ms |
976 KB |
Output is correct |
8 |
Correct |
4 ms |
1056 KB |
Output is correct |
9 |
Correct |
4 ms |
972 KB |
Output is correct |
10 |
Correct |
4 ms |
972 KB |
Output is correct |
11 |
Correct |
5 ms |
972 KB |
Output is correct |
12 |
Correct |
5 ms |
912 KB |
Output is correct |
13 |
Correct |
5 ms |
972 KB |
Output is correct |
14 |
Correct |
4 ms |
980 KB |
Output is correct |
15 |
Correct |
6 ms |
980 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
972 KB |
Output is correct |
18 |
Correct |
81 ms |
1836 KB |
Output is correct |
19 |
Correct |
73 ms |
1740 KB |
Output is correct |
20 |
Correct |
81 ms |
1876 KB |
Output is correct |
21 |
Correct |
73 ms |
1732 KB |
Output is correct |
22 |
Correct |
81 ms |
1828 KB |
Output is correct |
23 |
Correct |
71 ms |
1808 KB |
Output is correct |
24 |
Correct |
75 ms |
1780 KB |
Output is correct |
25 |
Correct |
78 ms |
1952 KB |
Output is correct |
26 |
Correct |
73 ms |
1792 KB |
Output is correct |
27 |
Correct |
69 ms |
1732 KB |
Output is correct |
28 |
Correct |
56 ms |
1368 KB |
Output is correct |
29 |
Correct |
56 ms |
1368 KB |
Output is correct |
30 |
Correct |
57 ms |
1348 KB |
Output is correct |
31 |
Correct |
58 ms |
1432 KB |
Output is correct |
32 |
Correct |
58 ms |
1348 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
61 ms |
1948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
24328 KB |
Output is correct |
2 |
Correct |
233 ms |
24440 KB |
Output is correct |
3 |
Correct |
210 ms |
23112 KB |
Output is correct |
4 |
Correct |
203 ms |
22944 KB |
Output is correct |
5 |
Correct |
262 ms |
23420 KB |
Output is correct |
6 |
Correct |
133 ms |
23572 KB |
Output is correct |
7 |
Correct |
136 ms |
23196 KB |
Output is correct |
8 |
Correct |
137 ms |
23592 KB |
Output is correct |
9 |
Correct |
131 ms |
23372 KB |
Output is correct |
10 |
Correct |
143 ms |
24732 KB |
Output is correct |
11 |
Correct |
150 ms |
23104 KB |
Output is correct |
12 |
Correct |
162 ms |
24740 KB |
Output is correct |
13 |
Correct |
149 ms |
23072 KB |
Output is correct |
14 |
Correct |
151 ms |
23568 KB |
Output is correct |
15 |
Correct |
169 ms |
24680 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
137 ms |
23420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
23580 KB |
Output is correct |
2 |
Correct |
215 ms |
23720 KB |
Output is correct |
3 |
Correct |
213 ms |
23800 KB |
Output is correct |
4 |
Correct |
222 ms |
23908 KB |
Output is correct |
5 |
Correct |
227 ms |
24784 KB |
Output is correct |
6 |
Correct |
136 ms |
23120 KB |
Output is correct |
7 |
Correct |
139 ms |
23348 KB |
Output is correct |
8 |
Correct |
144 ms |
23548 KB |
Output is correct |
9 |
Correct |
151 ms |
24092 KB |
Output is correct |
10 |
Correct |
142 ms |
23420 KB |
Output is correct |
11 |
Correct |
161 ms |
24348 KB |
Output is correct |
12 |
Correct |
151 ms |
23236 KB |
Output is correct |
13 |
Correct |
148 ms |
23140 KB |
Output is correct |
14 |
Correct |
165 ms |
24388 KB |
Output is correct |
15 |
Correct |
157 ms |
23900 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
143 ms |
24108 KB |
Output is correct |
18 |
Correct |
6 ms |
972 KB |
Output is correct |
19 |
Correct |
6 ms |
972 KB |
Output is correct |
20 |
Correct |
6 ms |
1036 KB |
Output is correct |
21 |
Correct |
6 ms |
972 KB |
Output is correct |
22 |
Correct |
7 ms |
1020 KB |
Output is correct |
23 |
Correct |
5 ms |
1044 KB |
Output is correct |
24 |
Correct |
4 ms |
976 KB |
Output is correct |
25 |
Correct |
4 ms |
1056 KB |
Output is correct |
26 |
Correct |
4 ms |
972 KB |
Output is correct |
27 |
Correct |
4 ms |
972 KB |
Output is correct |
28 |
Correct |
5 ms |
972 KB |
Output is correct |
29 |
Correct |
5 ms |
912 KB |
Output is correct |
30 |
Correct |
5 ms |
972 KB |
Output is correct |
31 |
Correct |
4 ms |
980 KB |
Output is correct |
32 |
Correct |
6 ms |
980 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
4 ms |
972 KB |
Output is correct |
35 |
Correct |
81 ms |
1836 KB |
Output is correct |
36 |
Correct |
73 ms |
1740 KB |
Output is correct |
37 |
Correct |
81 ms |
1876 KB |
Output is correct |
38 |
Correct |
73 ms |
1732 KB |
Output is correct |
39 |
Correct |
81 ms |
1828 KB |
Output is correct |
40 |
Correct |
71 ms |
1808 KB |
Output is correct |
41 |
Correct |
75 ms |
1780 KB |
Output is correct |
42 |
Correct |
78 ms |
1952 KB |
Output is correct |
43 |
Correct |
73 ms |
1792 KB |
Output is correct |
44 |
Correct |
69 ms |
1732 KB |
Output is correct |
45 |
Correct |
56 ms |
1368 KB |
Output is correct |
46 |
Correct |
56 ms |
1368 KB |
Output is correct |
47 |
Correct |
57 ms |
1348 KB |
Output is correct |
48 |
Correct |
58 ms |
1432 KB |
Output is correct |
49 |
Correct |
58 ms |
1348 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
61 ms |
1948 KB |
Output is correct |
52 |
Correct |
228 ms |
24328 KB |
Output is correct |
53 |
Correct |
233 ms |
24440 KB |
Output is correct |
54 |
Correct |
210 ms |
23112 KB |
Output is correct |
55 |
Correct |
203 ms |
22944 KB |
Output is correct |
56 |
Correct |
262 ms |
23420 KB |
Output is correct |
57 |
Correct |
133 ms |
23572 KB |
Output is correct |
58 |
Correct |
136 ms |
23196 KB |
Output is correct |
59 |
Correct |
137 ms |
23592 KB |
Output is correct |
60 |
Correct |
131 ms |
23372 KB |
Output is correct |
61 |
Correct |
143 ms |
24732 KB |
Output is correct |
62 |
Correct |
150 ms |
23104 KB |
Output is correct |
63 |
Correct |
162 ms |
24740 KB |
Output is correct |
64 |
Correct |
149 ms |
23072 KB |
Output is correct |
65 |
Correct |
151 ms |
23568 KB |
Output is correct |
66 |
Correct |
169 ms |
24680 KB |
Output is correct |
67 |
Correct |
0 ms |
332 KB |
Output is correct |
68 |
Correct |
137 ms |
23420 KB |
Output is correct |
69 |
Correct |
442 ms |
24900 KB |
Output is correct |
70 |
Correct |
447 ms |
26504 KB |
Output is correct |
71 |
Correct |
422 ms |
26860 KB |
Output is correct |
72 |
Correct |
421 ms |
26564 KB |
Output is correct |
73 |
Correct |
423 ms |
25116 KB |
Output is correct |
74 |
Correct |
383 ms |
25928 KB |
Output is correct |
75 |
Correct |
346 ms |
26016 KB |
Output is correct |
76 |
Correct |
376 ms |
28952 KB |
Output is correct |
77 |
Correct |
344 ms |
25272 KB |
Output is correct |
78 |
Correct |
339 ms |
24668 KB |
Output is correct |
79 |
Correct |
256 ms |
25164 KB |
Output is correct |
80 |
Correct |
244 ms |
23848 KB |
Output is correct |
81 |
Correct |
250 ms |
23784 KB |
Output is correct |
82 |
Correct |
251 ms |
24600 KB |
Output is correct |
83 |
Correct |
242 ms |
23740 KB |
Output is correct |
84 |
Correct |
0 ms |
332 KB |
Output is correct |
85 |
Correct |
211 ms |
24824 KB |
Output is correct |