#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 4;
struct node{
int upd, ans;
} a[4*MAXN];
void range_add(int l,int r,int constl,int constr,int idx,int val){
if(l<=constl && constr<=r){
a[idx].upd += val;
a[idx].ans += (constr-constl+1) * val;
return;
}
int mid = (constl + constr) >> 1;
if(mid<l || r<constl) range_add(l, r, mid+1, constr, 2*idx+2, val);
else if(constr<l || r<mid+1) range_add(l, r, constl, mid, 2*idx+1, val);
else{
range_add(l, r, constl, mid, 2*idx+1, val);
range_add(l, r, mid+1, constr, 2*idx+2, val);
}
a[idx].ans = a[2*idx+1].ans + a[2*idx+2].ans + (constr-constl+1) * a[idx].upd;
}
int query_sum(int l,int r,int constl,int constr,int idx){
if(l<=constl && constr<=r) return a[idx].ans;
int mid = (constl + constr) >> 1;
int sz = min(r, constr) - max(l, constl) + 1;
if(mid<l || r<constl) return sz * a[idx].upd + query_sum(l, r, mid+1, constr, 2*idx+2);
else if(constr<l || r<mid+1) return sz * a[idx].upd + query_sum(l, r, constl, mid, 2*idx+1);
else{
return sz * a[idx].upd + query_sum(l, r, constl, mid, 2*idx+1) + query_sum(l, r, mid+1, constr, 2*idx+2);
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
int l[n+1], r[n+1];
for(int i=1; i<=n; i++) cin >> l[i] >> r[i];
int s[2*n];
unordered_map<int, int> is;
for(int i=1; i<=n; i++) {
s[2*i-2] = l[i];
s[2*i-1] = r[i];
}
sort(s, s+2*n);
int it=0;
for(int i=0; i<2*n; i++) {
if(s[i] != s[i-1] || i == 0) is[s[i]] = ++it;
}
for(int i=1; i<=n; i++) {
l[i] = is[l[i]];
r[i] = is[r[i]];
}
int d[n+1];
int lp = 1, rp = 1;
range_add(l[1], r[1], 0, MAXN-1, 0, 1);
while(!(lp == n && rp == n)) {
d[lp] = rp;
if(rp<n && query_sum(l[rp+1], r[rp+1], 0, MAXN-1, 0) == 0) {
range_add(l[rp+1], r[rp+1], 0, MAXN-1, 0, 1);
rp++;
}
else {
range_add(l[lp], r[lp], 0, MAXN-1, 0, -1);
lp++;
if(rp < lp) {
rp++;
range_add(l[rp], r[rp], 0, MAXN-1, 0, 1);
}
}
}
d[n] = n;
int st[18][n+2];
for(int i=1; i<=n; i++) st[0][i] = d[i] + 1;
st[0][n + 1] = n + 1;
for(int i=1; i<18; i++) {
for(int j=1; j<=n+1; j++) {
st[i][j] = st[i-1][st[i-1][j]];
}
}
int q0; cin >> q0;
while(q0--) {
int p, q; cin >> p >> q;
int k = 32 - __builtin_clz(q-p+1) - 1;
int ans = 0;
for(int i=k; i>=0; i--) {
if(st[i][p] <= q) {
p = st[i][p];
ans += (1<<i);
}
}
while(p <= q) {
ans++;
p = d[p] + 1;
}
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
46592 KB |
Output is correct |
2 |
Correct |
401 ms |
45960 KB |
Output is correct |
3 |
Correct |
366 ms |
46016 KB |
Output is correct |
4 |
Correct |
354 ms |
46524 KB |
Output is correct |
5 |
Correct |
419 ms |
48296 KB |
Output is correct |
6 |
Correct |
85 ms |
20124 KB |
Output is correct |
7 |
Correct |
92 ms |
20500 KB |
Output is correct |
8 |
Correct |
99 ms |
20568 KB |
Output is correct |
9 |
Correct |
125 ms |
21256 KB |
Output is correct |
10 |
Correct |
104 ms |
20420 KB |
Output is correct |
11 |
Correct |
243 ms |
46928 KB |
Output is correct |
12 |
Correct |
219 ms |
44468 KB |
Output is correct |
13 |
Correct |
214 ms |
44400 KB |
Output is correct |
14 |
Correct |
327 ms |
46900 KB |
Output is correct |
15 |
Correct |
262 ms |
45920 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
95 ms |
21528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1452 KB |
Output is correct |
2 |
Correct |
12 ms |
1492 KB |
Output is correct |
3 |
Correct |
12 ms |
1396 KB |
Output is correct |
4 |
Correct |
12 ms |
1492 KB |
Output is correct |
5 |
Correct |
14 ms |
1492 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
9 ms |
856 KB |
Output is correct |
8 |
Correct |
8 ms |
852 KB |
Output is correct |
9 |
Correct |
9 ms |
852 KB |
Output is correct |
10 |
Correct |
8 ms |
852 KB |
Output is correct |
11 |
Correct |
10 ms |
1520 KB |
Output is correct |
12 |
Correct |
10 ms |
1364 KB |
Output is correct |
13 |
Correct |
10 ms |
1364 KB |
Output is correct |
14 |
Correct |
12 ms |
1492 KB |
Output is correct |
15 |
Correct |
11 ms |
1492 KB |
Output is correct |
16 |
Correct |
0 ms |
328 KB |
Output is correct |
17 |
Correct |
8 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1452 KB |
Output is correct |
2 |
Correct |
12 ms |
1492 KB |
Output is correct |
3 |
Correct |
12 ms |
1396 KB |
Output is correct |
4 |
Correct |
12 ms |
1492 KB |
Output is correct |
5 |
Correct |
14 ms |
1492 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
9 ms |
856 KB |
Output is correct |
8 |
Correct |
8 ms |
852 KB |
Output is correct |
9 |
Correct |
9 ms |
852 KB |
Output is correct |
10 |
Correct |
8 ms |
852 KB |
Output is correct |
11 |
Correct |
10 ms |
1520 KB |
Output is correct |
12 |
Correct |
10 ms |
1364 KB |
Output is correct |
13 |
Correct |
10 ms |
1364 KB |
Output is correct |
14 |
Correct |
12 ms |
1492 KB |
Output is correct |
15 |
Correct |
11 ms |
1492 KB |
Output is correct |
16 |
Correct |
0 ms |
328 KB |
Output is correct |
17 |
Correct |
8 ms |
852 KB |
Output is correct |
18 |
Correct |
251 ms |
4184 KB |
Output is correct |
19 |
Correct |
223 ms |
3792 KB |
Output is correct |
20 |
Correct |
246 ms |
4204 KB |
Output is correct |
21 |
Correct |
238 ms |
3936 KB |
Output is correct |
22 |
Correct |
234 ms |
3916 KB |
Output is correct |
23 |
Correct |
225 ms |
3404 KB |
Output is correct |
24 |
Correct |
253 ms |
3524 KB |
Output is correct |
25 |
Correct |
245 ms |
3524 KB |
Output is correct |
26 |
Correct |
265 ms |
3400 KB |
Output is correct |
27 |
Correct |
230 ms |
3296 KB |
Output is correct |
28 |
Correct |
213 ms |
3408 KB |
Output is correct |
29 |
Correct |
225 ms |
3612 KB |
Output is correct |
30 |
Correct |
221 ms |
3612 KB |
Output is correct |
31 |
Correct |
228 ms |
3524 KB |
Output is correct |
32 |
Correct |
230 ms |
3580 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
228 ms |
3220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
364 ms |
48368 KB |
Output is correct |
2 |
Correct |
345 ms |
47504 KB |
Output is correct |
3 |
Correct |
338 ms |
44712 KB |
Output is correct |
4 |
Correct |
313 ms |
44308 KB |
Output is correct |
5 |
Correct |
362 ms |
45500 KB |
Output is correct |
6 |
Correct |
85 ms |
20304 KB |
Output is correct |
7 |
Correct |
102 ms |
20120 KB |
Output is correct |
8 |
Correct |
103 ms |
20204 KB |
Output is correct |
9 |
Correct |
94 ms |
20156 KB |
Output is correct |
10 |
Correct |
109 ms |
21672 KB |
Output is correct |
11 |
Correct |
217 ms |
44064 KB |
Output is correct |
12 |
Correct |
270 ms |
47804 KB |
Output is correct |
13 |
Correct |
223 ms |
44316 KB |
Output is correct |
14 |
Correct |
229 ms |
45280 KB |
Output is correct |
15 |
Correct |
318 ms |
47608 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
91 ms |
20540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
46592 KB |
Output is correct |
2 |
Correct |
401 ms |
45960 KB |
Output is correct |
3 |
Correct |
366 ms |
46016 KB |
Output is correct |
4 |
Correct |
354 ms |
46524 KB |
Output is correct |
5 |
Correct |
419 ms |
48296 KB |
Output is correct |
6 |
Correct |
85 ms |
20124 KB |
Output is correct |
7 |
Correct |
92 ms |
20500 KB |
Output is correct |
8 |
Correct |
99 ms |
20568 KB |
Output is correct |
9 |
Correct |
125 ms |
21256 KB |
Output is correct |
10 |
Correct |
104 ms |
20420 KB |
Output is correct |
11 |
Correct |
243 ms |
46928 KB |
Output is correct |
12 |
Correct |
219 ms |
44468 KB |
Output is correct |
13 |
Correct |
214 ms |
44400 KB |
Output is correct |
14 |
Correct |
327 ms |
46900 KB |
Output is correct |
15 |
Correct |
262 ms |
45920 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
95 ms |
21528 KB |
Output is correct |
18 |
Correct |
13 ms |
1452 KB |
Output is correct |
19 |
Correct |
12 ms |
1492 KB |
Output is correct |
20 |
Correct |
12 ms |
1396 KB |
Output is correct |
21 |
Correct |
12 ms |
1492 KB |
Output is correct |
22 |
Correct |
14 ms |
1492 KB |
Output is correct |
23 |
Correct |
10 ms |
976 KB |
Output is correct |
24 |
Correct |
9 ms |
856 KB |
Output is correct |
25 |
Correct |
8 ms |
852 KB |
Output is correct |
26 |
Correct |
9 ms |
852 KB |
Output is correct |
27 |
Correct |
8 ms |
852 KB |
Output is correct |
28 |
Correct |
10 ms |
1520 KB |
Output is correct |
29 |
Correct |
10 ms |
1364 KB |
Output is correct |
30 |
Correct |
10 ms |
1364 KB |
Output is correct |
31 |
Correct |
12 ms |
1492 KB |
Output is correct |
32 |
Correct |
11 ms |
1492 KB |
Output is correct |
33 |
Correct |
0 ms |
328 KB |
Output is correct |
34 |
Correct |
8 ms |
852 KB |
Output is correct |
35 |
Correct |
251 ms |
4184 KB |
Output is correct |
36 |
Correct |
223 ms |
3792 KB |
Output is correct |
37 |
Correct |
246 ms |
4204 KB |
Output is correct |
38 |
Correct |
238 ms |
3936 KB |
Output is correct |
39 |
Correct |
234 ms |
3916 KB |
Output is correct |
40 |
Correct |
225 ms |
3404 KB |
Output is correct |
41 |
Correct |
253 ms |
3524 KB |
Output is correct |
42 |
Correct |
245 ms |
3524 KB |
Output is correct |
43 |
Correct |
265 ms |
3400 KB |
Output is correct |
44 |
Correct |
230 ms |
3296 KB |
Output is correct |
45 |
Correct |
213 ms |
3408 KB |
Output is correct |
46 |
Correct |
225 ms |
3612 KB |
Output is correct |
47 |
Correct |
221 ms |
3612 KB |
Output is correct |
48 |
Correct |
228 ms |
3524 KB |
Output is correct |
49 |
Correct |
230 ms |
3580 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
228 ms |
3220 KB |
Output is correct |
52 |
Correct |
364 ms |
48368 KB |
Output is correct |
53 |
Correct |
345 ms |
47504 KB |
Output is correct |
54 |
Correct |
338 ms |
44712 KB |
Output is correct |
55 |
Correct |
313 ms |
44308 KB |
Output is correct |
56 |
Correct |
362 ms |
45500 KB |
Output is correct |
57 |
Correct |
85 ms |
20304 KB |
Output is correct |
58 |
Correct |
102 ms |
20120 KB |
Output is correct |
59 |
Correct |
103 ms |
20204 KB |
Output is correct |
60 |
Correct |
94 ms |
20156 KB |
Output is correct |
61 |
Correct |
109 ms |
21672 KB |
Output is correct |
62 |
Correct |
217 ms |
44064 KB |
Output is correct |
63 |
Correct |
270 ms |
47804 KB |
Output is correct |
64 |
Correct |
223 ms |
44316 KB |
Output is correct |
65 |
Correct |
229 ms |
45280 KB |
Output is correct |
66 |
Correct |
318 ms |
47608 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
91 ms |
20540 KB |
Output is correct |
69 |
Correct |
657 ms |
49024 KB |
Output is correct |
70 |
Correct |
757 ms |
51240 KB |
Output is correct |
71 |
Correct |
613 ms |
47808 KB |
Output is correct |
72 |
Correct |
686 ms |
49952 KB |
Output is correct |
73 |
Correct |
687 ms |
49444 KB |
Output is correct |
74 |
Correct |
405 ms |
25504 KB |
Output is correct |
75 |
Correct |
380 ms |
23884 KB |
Output is correct |
76 |
Correct |
424 ms |
25652 KB |
Output is correct |
77 |
Correct |
395 ms |
23640 KB |
Output is correct |
78 |
Correct |
408 ms |
24124 KB |
Output is correct |
79 |
Correct |
496 ms |
50464 KB |
Output is correct |
80 |
Correct |
466 ms |
47108 KB |
Output is correct |
81 |
Correct |
495 ms |
46900 KB |
Output is correct |
82 |
Correct |
479 ms |
49300 KB |
Output is correct |
83 |
Correct |
494 ms |
47208 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
323 ms |
23752 KB |
Output is correct |