#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_;
int aa[N];
void build(int n) {
int i;
#if 0
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;
#endif
for (i = 0; i < n * 2; i++)
aa[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(st[i << 1 | 0], lz[i]), put(st[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 i;
for (i = l; i <= r; i++)
aa[i] = x;
#if 0
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_);
#endif
}
int query(int l, int r) {
int x = INF, i;
for (i = l; i <= r; i++)
x = min(x, aa[i]);
#if 0
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--]);
}
#endif
return x;
}
int main() {
static int ii[N * 2], dd[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--;
memset(dd, 0, n * 2 * sizeof *dd);
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:128:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:130:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | scanf("%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1]), xx[i << 1 | 1]++;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:147:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
Main.c:151:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1084 ms |
5024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
836 KB |
Output is correct |
2 |
Correct |
27 ms |
912 KB |
Output is correct |
3 |
Correct |
27 ms |
804 KB |
Output is correct |
4 |
Correct |
31 ms |
948 KB |
Output is correct |
5 |
Correct |
27 ms |
836 KB |
Output is correct |
6 |
Correct |
8 ms |
972 KB |
Output is correct |
7 |
Correct |
8 ms |
940 KB |
Output is correct |
8 |
Correct |
8 ms |
844 KB |
Output is correct |
9 |
Correct |
8 ms |
844 KB |
Output is correct |
10 |
Correct |
8 ms |
804 KB |
Output is correct |
11 |
Correct |
9 ms |
924 KB |
Output is correct |
12 |
Correct |
8 ms |
940 KB |
Output is correct |
13 |
Correct |
8 ms |
804 KB |
Output is correct |
14 |
Correct |
9 ms |
924 KB |
Output is correct |
15 |
Correct |
8 ms |
940 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
7 ms |
836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
836 KB |
Output is correct |
2 |
Correct |
27 ms |
912 KB |
Output is correct |
3 |
Correct |
27 ms |
804 KB |
Output is correct |
4 |
Correct |
31 ms |
948 KB |
Output is correct |
5 |
Correct |
27 ms |
836 KB |
Output is correct |
6 |
Correct |
8 ms |
972 KB |
Output is correct |
7 |
Correct |
8 ms |
940 KB |
Output is correct |
8 |
Correct |
8 ms |
844 KB |
Output is correct |
9 |
Correct |
8 ms |
844 KB |
Output is correct |
10 |
Correct |
8 ms |
804 KB |
Output is correct |
11 |
Correct |
9 ms |
924 KB |
Output is correct |
12 |
Correct |
8 ms |
940 KB |
Output is correct |
13 |
Correct |
8 ms |
804 KB |
Output is correct |
14 |
Correct |
9 ms |
924 KB |
Output is correct |
15 |
Correct |
8 ms |
940 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
7 ms |
836 KB |
Output is correct |
18 |
Correct |
261 ms |
1828 KB |
Output is correct |
19 |
Correct |
232 ms |
1836 KB |
Output is correct |
20 |
Correct |
268 ms |
1864 KB |
Output is correct |
21 |
Correct |
248 ms |
1872 KB |
Output is correct |
22 |
Correct |
243 ms |
1860 KB |
Output is correct |
23 |
Correct |
220 ms |
1848 KB |
Output is correct |
24 |
Correct |
232 ms |
1772 KB |
Output is correct |
25 |
Correct |
235 ms |
1848 KB |
Output is correct |
26 |
Correct |
241 ms |
1800 KB |
Output is correct |
27 |
Correct |
227 ms |
1796 KB |
Output is correct |
28 |
Correct |
208 ms |
1344 KB |
Output is correct |
29 |
Correct |
218 ms |
1568 KB |
Output is correct |
30 |
Correct |
216 ms |
1352 KB |
Output is correct |
31 |
Correct |
211 ms |
1332 KB |
Output is correct |
32 |
Correct |
215 ms |
1464 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
237 ms |
1940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1094 ms |
4984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1084 ms |
5024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |