#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(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 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;
push(l += n_), push(r += n_);
x = INF;
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], 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:112:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:114:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | scanf("%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1]), xx[i << 1 | 1]++;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:131:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
Main.c:135:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
275 ms |
25464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
1100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
1100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
275 ms |
26444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
275 ms |
25464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |