#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=2e5;
int n, q, l[mxN], r[mxN], anc[mxN][18];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i=0; i<n; ++i)
cin >> l[i] >> r[i];
set<ar<int, 2>> s;
for (int i=n-1, j=n-1; ~i; --i) {
while(1) {
if (s.empty()||r[i]<(*s.begin())[0]||l[i]>(*s.rbegin())[1]) {
s.insert({l[i], r[i]});
break;
}
auto it=s.lower_bound({l[i], -1});
if ((it==s.end()||r[i]<(*it)[0])&&(it==s.begin()||(*prev(it))[1]<l[i])) {
s.insert({l[i], r[i]});
break;
}
s.erase({l[j], r[j]});
--j;
}
anc[i][0]=j+1;
}
for (int j=1; j<18; ++j)
for (int i=0; i<n; ++i)
anc[i][j]=anc[i][j-1]==n?n:anc[anc[i][j-1]][j-1];
cin >> q;
while(q--) {
int a, b;
cin >> a >> b, --a, --b;
int ans=0;
for (int i=17; ~i; --i)
if (anc[a][i]<=b) {
ans+=1<<i;
a=anc[a][i];
}
cout << ans+1 << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
15928 KB |
Output is correct |
2 |
Correct |
81 ms |
15556 KB |
Output is correct |
3 |
Correct |
76 ms |
15796 KB |
Output is correct |
4 |
Correct |
78 ms |
15608 KB |
Output is correct |
5 |
Correct |
80 ms |
16604 KB |
Output is correct |
6 |
Correct |
68 ms |
15148 KB |
Output is correct |
7 |
Correct |
74 ms |
15384 KB |
Output is correct |
8 |
Correct |
87 ms |
15256 KB |
Output is correct |
9 |
Correct |
85 ms |
15816 KB |
Output is correct |
10 |
Correct |
87 ms |
15292 KB |
Output is correct |
11 |
Correct |
169 ms |
25232 KB |
Output is correct |
12 |
Correct |
190 ms |
23996 KB |
Output is correct |
13 |
Correct |
151 ms |
23712 KB |
Output is correct |
14 |
Correct |
179 ms |
21512 KB |
Output is correct |
15 |
Correct |
172 ms |
19700 KB |
Output is correct |
16 |
Correct |
0 ms |
316 KB |
Output is correct |
17 |
Correct |
73 ms |
15880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
696 KB |
Output is correct |
2 |
Correct |
4 ms |
748 KB |
Output is correct |
3 |
Correct |
3 ms |
668 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
3 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
800 KB |
Output is correct |
7 |
Correct |
3 ms |
744 KB |
Output is correct |
8 |
Correct |
3 ms |
764 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
4 ms |
988 KB |
Output is correct |
12 |
Correct |
4 ms |
972 KB |
Output is correct |
13 |
Correct |
4 ms |
844 KB |
Output is correct |
14 |
Correct |
4 ms |
744 KB |
Output is correct |
15 |
Correct |
4 ms |
716 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
17 |
Correct |
3 ms |
712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
696 KB |
Output is correct |
2 |
Correct |
4 ms |
748 KB |
Output is correct |
3 |
Correct |
3 ms |
668 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
3 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
800 KB |
Output is correct |
7 |
Correct |
3 ms |
744 KB |
Output is correct |
8 |
Correct |
3 ms |
764 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
4 ms |
988 KB |
Output is correct |
12 |
Correct |
4 ms |
972 KB |
Output is correct |
13 |
Correct |
4 ms |
844 KB |
Output is correct |
14 |
Correct |
4 ms |
744 KB |
Output is correct |
15 |
Correct |
4 ms |
716 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
17 |
Correct |
3 ms |
712 KB |
Output is correct |
18 |
Correct |
74 ms |
2060 KB |
Output is correct |
19 |
Correct |
59 ms |
1688 KB |
Output is correct |
20 |
Correct |
67 ms |
1728 KB |
Output is correct |
21 |
Correct |
61 ms |
1728 KB |
Output is correct |
22 |
Correct |
63 ms |
1716 KB |
Output is correct |
23 |
Correct |
60 ms |
1676 KB |
Output is correct |
24 |
Correct |
65 ms |
1740 KB |
Output is correct |
25 |
Correct |
63 ms |
1732 KB |
Output is correct |
26 |
Correct |
61 ms |
1760 KB |
Output is correct |
27 |
Correct |
63 ms |
1748 KB |
Output is correct |
28 |
Correct |
44 ms |
1648 KB |
Output is correct |
29 |
Correct |
47 ms |
1432 KB |
Output is correct |
30 |
Correct |
50 ms |
1360 KB |
Output is correct |
31 |
Correct |
48 ms |
1248 KB |
Output is correct |
32 |
Correct |
47 ms |
1328 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
54 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
16580 KB |
Output is correct |
2 |
Correct |
90 ms |
15948 KB |
Output is correct |
3 |
Correct |
76 ms |
15052 KB |
Output is correct |
4 |
Correct |
73 ms |
14840 KB |
Output is correct |
5 |
Correct |
74 ms |
15128 KB |
Output is correct |
6 |
Correct |
69 ms |
15796 KB |
Output is correct |
7 |
Correct |
87 ms |
15068 KB |
Output is correct |
8 |
Correct |
80 ms |
16452 KB |
Output is correct |
9 |
Correct |
78 ms |
16488 KB |
Output is correct |
10 |
Correct |
94 ms |
17468 KB |
Output is correct |
11 |
Correct |
153 ms |
23720 KB |
Output is correct |
12 |
Correct |
171 ms |
26012 KB |
Output is correct |
13 |
Correct |
159 ms |
23396 KB |
Output is correct |
14 |
Correct |
161 ms |
19800 KB |
Output is correct |
15 |
Correct |
188 ms |
21564 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
71 ms |
15460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
15928 KB |
Output is correct |
2 |
Correct |
81 ms |
15556 KB |
Output is correct |
3 |
Correct |
76 ms |
15796 KB |
Output is correct |
4 |
Correct |
78 ms |
15608 KB |
Output is correct |
5 |
Correct |
80 ms |
16604 KB |
Output is correct |
6 |
Correct |
68 ms |
15148 KB |
Output is correct |
7 |
Correct |
74 ms |
15384 KB |
Output is correct |
8 |
Correct |
87 ms |
15256 KB |
Output is correct |
9 |
Correct |
85 ms |
15816 KB |
Output is correct |
10 |
Correct |
87 ms |
15292 KB |
Output is correct |
11 |
Correct |
169 ms |
25232 KB |
Output is correct |
12 |
Correct |
190 ms |
23996 KB |
Output is correct |
13 |
Correct |
151 ms |
23712 KB |
Output is correct |
14 |
Correct |
179 ms |
21512 KB |
Output is correct |
15 |
Correct |
172 ms |
19700 KB |
Output is correct |
16 |
Correct |
0 ms |
316 KB |
Output is correct |
17 |
Correct |
73 ms |
15880 KB |
Output is correct |
18 |
Correct |
4 ms |
696 KB |
Output is correct |
19 |
Correct |
4 ms |
748 KB |
Output is correct |
20 |
Correct |
3 ms |
668 KB |
Output is correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Correct |
3 ms |
716 KB |
Output is correct |
23 |
Correct |
3 ms |
800 KB |
Output is correct |
24 |
Correct |
3 ms |
744 KB |
Output is correct |
25 |
Correct |
3 ms |
764 KB |
Output is correct |
26 |
Correct |
4 ms |
716 KB |
Output is correct |
27 |
Correct |
4 ms |
716 KB |
Output is correct |
28 |
Correct |
4 ms |
988 KB |
Output is correct |
29 |
Correct |
4 ms |
972 KB |
Output is correct |
30 |
Correct |
4 ms |
844 KB |
Output is correct |
31 |
Correct |
4 ms |
744 KB |
Output is correct |
32 |
Correct |
4 ms |
716 KB |
Output is correct |
33 |
Correct |
0 ms |
320 KB |
Output is correct |
34 |
Correct |
3 ms |
712 KB |
Output is correct |
35 |
Correct |
74 ms |
2060 KB |
Output is correct |
36 |
Correct |
59 ms |
1688 KB |
Output is correct |
37 |
Correct |
67 ms |
1728 KB |
Output is correct |
38 |
Correct |
61 ms |
1728 KB |
Output is correct |
39 |
Correct |
63 ms |
1716 KB |
Output is correct |
40 |
Correct |
60 ms |
1676 KB |
Output is correct |
41 |
Correct |
65 ms |
1740 KB |
Output is correct |
42 |
Correct |
63 ms |
1732 KB |
Output is correct |
43 |
Correct |
61 ms |
1760 KB |
Output is correct |
44 |
Correct |
63 ms |
1748 KB |
Output is correct |
45 |
Correct |
44 ms |
1648 KB |
Output is correct |
46 |
Correct |
47 ms |
1432 KB |
Output is correct |
47 |
Correct |
50 ms |
1360 KB |
Output is correct |
48 |
Correct |
48 ms |
1248 KB |
Output is correct |
49 |
Correct |
47 ms |
1328 KB |
Output is correct |
50 |
Correct |
0 ms |
332 KB |
Output is correct |
51 |
Correct |
54 ms |
1880 KB |
Output is correct |
52 |
Correct |
79 ms |
16580 KB |
Output is correct |
53 |
Correct |
90 ms |
15948 KB |
Output is correct |
54 |
Correct |
76 ms |
15052 KB |
Output is correct |
55 |
Correct |
73 ms |
14840 KB |
Output is correct |
56 |
Correct |
74 ms |
15128 KB |
Output is correct |
57 |
Correct |
69 ms |
15796 KB |
Output is correct |
58 |
Correct |
87 ms |
15068 KB |
Output is correct |
59 |
Correct |
80 ms |
16452 KB |
Output is correct |
60 |
Correct |
78 ms |
16488 KB |
Output is correct |
61 |
Correct |
94 ms |
17468 KB |
Output is correct |
62 |
Correct |
153 ms |
23720 KB |
Output is correct |
63 |
Correct |
171 ms |
26012 KB |
Output is correct |
64 |
Correct |
159 ms |
23396 KB |
Output is correct |
65 |
Correct |
161 ms |
19800 KB |
Output is correct |
66 |
Correct |
188 ms |
21564 KB |
Output is correct |
67 |
Correct |
0 ms |
204 KB |
Output is correct |
68 |
Correct |
71 ms |
15460 KB |
Output is correct |
69 |
Correct |
247 ms |
17312 KB |
Output is correct |
70 |
Correct |
275 ms |
19156 KB |
Output is correct |
71 |
Correct |
232 ms |
18876 KB |
Output is correct |
72 |
Correct |
237 ms |
18664 KB |
Output is correct |
73 |
Correct |
255 ms |
17176 KB |
Output is correct |
74 |
Correct |
256 ms |
17692 KB |
Output is correct |
75 |
Correct |
243 ms |
18112 KB |
Output is correct |
76 |
Correct |
252 ms |
20732 KB |
Output is correct |
77 |
Correct |
232 ms |
17344 KB |
Output is correct |
78 |
Correct |
240 ms |
16964 KB |
Output is correct |
79 |
Correct |
227 ms |
26808 KB |
Output is correct |
80 |
Correct |
217 ms |
24544 KB |
Output is correct |
81 |
Correct |
219 ms |
24392 KB |
Output is correct |
82 |
Correct |
241 ms |
20720 KB |
Output is correct |
83 |
Correct |
222 ms |
20492 KB |
Output is correct |
84 |
Correct |
1 ms |
308 KB |
Output is correct |
85 |
Correct |
142 ms |
17792 KB |
Output is correct |