#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef pair <int, int> pi;
const int MAXN = 200005;
const int LOG = 18;
int n, q;
int lef[MAXN], rig[MAXN];
int par[MAXN][LOG];
set <pi> st;
bool can_insert (int ind) {
if (st.empty()) return 1;
auto it = st.lower_bound({lef[ind], 0});
int nxt = it -> second;
if (lef[ind] <= lef[nxt] && lef[nxt] <= rig[ind]) return 0;
if (it != st.begin()) {
it--;
int prv = it -> second;
if (lef[prv] <= lef[ind] && lef[ind] <= rig[prv]) return 0;
}
return 1;
}
void precompute () {
int lim = 0;
for (int i = 1; i <= n; i++) {
if (lim < i) {
st.clear();
lim = i;
st.insert({lef[i], i});
}
while (lim + 1 <= n && can_insert(lim + 1)) {
lim++;
st.insert({lef[lim], lim});
}
par[i][0] = lim + 1;
st.erase({lef[i], i});
}
par[n + 1][0] = n + 1;
for (int i = 1; i < LOG; i++) {
for (int j = 1; j <= n + 1; j++) {
par[j][i] = par[par[j][i - 1]][i - 1];
}
}
}
int upit (int a, int b) {
int res = 0;
for (int i = LOG - 1; i >= 0; i--) {
if (par[a][i] <= b) {
a = par[a][i];
res += 1 << i;
}
}
return res + 1;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> lef[i] >> rig[i];
}
precompute();
cin >> q;
for (int i = 1; i <= q; i++) {
int a, b;
cin >> a >> b;
cout << upit(a, b) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
15256 KB |
Output is correct |
2 |
Correct |
85 ms |
14956 KB |
Output is correct |
3 |
Correct |
76 ms |
15072 KB |
Output is correct |
4 |
Correct |
77 ms |
15248 KB |
Output is correct |
5 |
Correct |
82 ms |
15828 KB |
Output is correct |
6 |
Correct |
95 ms |
14364 KB |
Output is correct |
7 |
Correct |
78 ms |
14856 KB |
Output is correct |
8 |
Correct |
79 ms |
14884 KB |
Output is correct |
9 |
Correct |
104 ms |
15300 KB |
Output is correct |
10 |
Correct |
102 ms |
14776 KB |
Output is correct |
11 |
Correct |
247 ms |
24680 KB |
Output is correct |
12 |
Correct |
238 ms |
23308 KB |
Output is correct |
13 |
Correct |
227 ms |
23120 KB |
Output is correct |
14 |
Correct |
183 ms |
20740 KB |
Output is correct |
15 |
Correct |
189 ms |
19068 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
75 ms |
15508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
716 KB |
Output is correct |
2 |
Correct |
4 ms |
716 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
4 ms |
716 KB |
Output is correct |
6 |
Correct |
5 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
5 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
4 ms |
972 KB |
Output is correct |
12 |
Correct |
4 ms |
844 KB |
Output is correct |
13 |
Correct |
5 ms |
844 KB |
Output is correct |
14 |
Correct |
4 ms |
824 KB |
Output is correct |
15 |
Correct |
4 ms |
716 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
716 KB |
Output is correct |
2 |
Correct |
4 ms |
716 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
4 ms |
716 KB |
Output is correct |
6 |
Correct |
5 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
5 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
4 ms |
972 KB |
Output is correct |
12 |
Correct |
4 ms |
844 KB |
Output is correct |
13 |
Correct |
5 ms |
844 KB |
Output is correct |
14 |
Correct |
4 ms |
824 KB |
Output is correct |
15 |
Correct |
4 ms |
716 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
64 ms |
1496 KB |
Output is correct |
19 |
Correct |
58 ms |
1404 KB |
Output is correct |
20 |
Correct |
63 ms |
1496 KB |
Output is correct |
21 |
Correct |
57 ms |
1440 KB |
Output is correct |
22 |
Correct |
66 ms |
1524 KB |
Output is correct |
23 |
Correct |
66 ms |
1524 KB |
Output is correct |
24 |
Correct |
61 ms |
1468 KB |
Output is correct |
25 |
Correct |
63 ms |
1536 KB |
Output is correct |
26 |
Correct |
58 ms |
1452 KB |
Output is correct |
27 |
Correct |
68 ms |
1292 KB |
Output is correct |
28 |
Correct |
62 ms |
1272 KB |
Output is correct |
29 |
Correct |
50 ms |
1276 KB |
Output is correct |
30 |
Correct |
45 ms |
1224 KB |
Output is correct |
31 |
Correct |
50 ms |
1096 KB |
Output is correct |
32 |
Correct |
58 ms |
1136 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
71 ms |
1580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
15940 KB |
Output is correct |
2 |
Correct |
81 ms |
15556 KB |
Output is correct |
3 |
Correct |
77 ms |
14584 KB |
Output is correct |
4 |
Correct |
71 ms |
14332 KB |
Output is correct |
5 |
Correct |
80 ms |
14788 KB |
Output is correct |
6 |
Correct |
71 ms |
14512 KB |
Output is correct |
7 |
Correct |
76 ms |
14420 KB |
Output is correct |
8 |
Correct |
75 ms |
14628 KB |
Output is correct |
9 |
Correct |
82 ms |
14364 KB |
Output is correct |
10 |
Correct |
103 ms |
15648 KB |
Output is correct |
11 |
Correct |
215 ms |
23184 KB |
Output is correct |
12 |
Correct |
229 ms |
25244 KB |
Output is correct |
13 |
Correct |
217 ms |
22992 KB |
Output is correct |
14 |
Correct |
211 ms |
19256 KB |
Output is correct |
15 |
Correct |
189 ms |
20932 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
68 ms |
14692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
15256 KB |
Output is correct |
2 |
Correct |
85 ms |
14956 KB |
Output is correct |
3 |
Correct |
76 ms |
15072 KB |
Output is correct |
4 |
Correct |
77 ms |
15248 KB |
Output is correct |
5 |
Correct |
82 ms |
15828 KB |
Output is correct |
6 |
Correct |
95 ms |
14364 KB |
Output is correct |
7 |
Correct |
78 ms |
14856 KB |
Output is correct |
8 |
Correct |
79 ms |
14884 KB |
Output is correct |
9 |
Correct |
104 ms |
15300 KB |
Output is correct |
10 |
Correct |
102 ms |
14776 KB |
Output is correct |
11 |
Correct |
247 ms |
24680 KB |
Output is correct |
12 |
Correct |
238 ms |
23308 KB |
Output is correct |
13 |
Correct |
227 ms |
23120 KB |
Output is correct |
14 |
Correct |
183 ms |
20740 KB |
Output is correct |
15 |
Correct |
189 ms |
19068 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
75 ms |
15508 KB |
Output is correct |
18 |
Correct |
6 ms |
716 KB |
Output is correct |
19 |
Correct |
4 ms |
716 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Correct |
4 ms |
716 KB |
Output is correct |
23 |
Correct |
5 ms |
716 KB |
Output is correct |
24 |
Correct |
4 ms |
716 KB |
Output is correct |
25 |
Correct |
5 ms |
716 KB |
Output is correct |
26 |
Correct |
3 ms |
716 KB |
Output is correct |
27 |
Correct |
3 ms |
716 KB |
Output is correct |
28 |
Correct |
4 ms |
972 KB |
Output is correct |
29 |
Correct |
4 ms |
844 KB |
Output is correct |
30 |
Correct |
5 ms |
844 KB |
Output is correct |
31 |
Correct |
4 ms |
824 KB |
Output is correct |
32 |
Correct |
4 ms |
716 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
3 ms |
716 KB |
Output is correct |
35 |
Correct |
64 ms |
1496 KB |
Output is correct |
36 |
Correct |
58 ms |
1404 KB |
Output is correct |
37 |
Correct |
63 ms |
1496 KB |
Output is correct |
38 |
Correct |
57 ms |
1440 KB |
Output is correct |
39 |
Correct |
66 ms |
1524 KB |
Output is correct |
40 |
Correct |
66 ms |
1524 KB |
Output is correct |
41 |
Correct |
61 ms |
1468 KB |
Output is correct |
42 |
Correct |
63 ms |
1536 KB |
Output is correct |
43 |
Correct |
58 ms |
1452 KB |
Output is correct |
44 |
Correct |
68 ms |
1292 KB |
Output is correct |
45 |
Correct |
62 ms |
1272 KB |
Output is correct |
46 |
Correct |
50 ms |
1276 KB |
Output is correct |
47 |
Correct |
45 ms |
1224 KB |
Output is correct |
48 |
Correct |
50 ms |
1096 KB |
Output is correct |
49 |
Correct |
58 ms |
1136 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
71 ms |
1580 KB |
Output is correct |
52 |
Correct |
84 ms |
15940 KB |
Output is correct |
53 |
Correct |
81 ms |
15556 KB |
Output is correct |
54 |
Correct |
77 ms |
14584 KB |
Output is correct |
55 |
Correct |
71 ms |
14332 KB |
Output is correct |
56 |
Correct |
80 ms |
14788 KB |
Output is correct |
57 |
Correct |
71 ms |
14512 KB |
Output is correct |
58 |
Correct |
76 ms |
14420 KB |
Output is correct |
59 |
Correct |
75 ms |
14628 KB |
Output is correct |
60 |
Correct |
82 ms |
14364 KB |
Output is correct |
61 |
Correct |
103 ms |
15648 KB |
Output is correct |
62 |
Correct |
215 ms |
23184 KB |
Output is correct |
63 |
Correct |
229 ms |
25244 KB |
Output is correct |
64 |
Correct |
217 ms |
22992 KB |
Output is correct |
65 |
Correct |
211 ms |
19256 KB |
Output is correct |
66 |
Correct |
189 ms |
20932 KB |
Output is correct |
67 |
Correct |
0 ms |
344 KB |
Output is correct |
68 |
Correct |
68 ms |
14692 KB |
Output is correct |
69 |
Correct |
285 ms |
16036 KB |
Output is correct |
70 |
Correct |
258 ms |
16812 KB |
Output is correct |
71 |
Correct |
230 ms |
15496 KB |
Output is correct |
72 |
Correct |
316 ms |
16308 KB |
Output is correct |
73 |
Correct |
248 ms |
16028 KB |
Output is correct |
74 |
Correct |
272 ms |
16964 KB |
Output is correct |
75 |
Correct |
303 ms |
15696 KB |
Output is correct |
76 |
Correct |
298 ms |
16920 KB |
Output is correct |
77 |
Correct |
244 ms |
15652 KB |
Output is correct |
78 |
Correct |
256 ms |
15864 KB |
Output is correct |
79 |
Correct |
292 ms |
25464 KB |
Output is correct |
80 |
Correct |
274 ms |
23652 KB |
Output is correct |
81 |
Correct |
321 ms |
23412 KB |
Output is correct |
82 |
Correct |
258 ms |
19900 KB |
Output is correct |
83 |
Correct |
227 ms |
19136 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
134 ms |
15996 KB |
Output is correct |