#include <bits/stdc++.h>
#define ll long long
#define pb push_back
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) (int)x.size()
#define fi first
#define sd second
#define all(x) x.begin(), x.end()
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = (int)2e5 + 5;
int n, l[N], r[N], q, nxt[N], t[8 * N], mod[8 * N], up[N][22];
vector<int> vec;
void push(int v)
{
mod[v + v] = min(mod[v + v], mod[v]);
mod[v + v + 1] = min(mod[v + v + 1], mod[v]);
t[v + v] = min(t[v + v], mod[v]);
t[v + v + 1] = min(t[v + v + 1], mod[v]);
}
void build(int v, int tl, int tr)
{
t[v] = n + 1;
mod[v] = n + 1;
if (tl == tr)
return;
int m = (tl + tr) >> 1;
build(v + v, tl, m);
build(v + v + 1, m + 1, tr);
}
int get(int v, int tl, int tr, int l, int r)
{
if (tl > tr || tl > r || tr < l) return n + 1;
if (tl >= l && tr <= r)
return t[v];
push(v);
int m = (tl + tr) >> 1;
return min(get(v + v, tl, m, l, r), get(v + v + 1, m + 1, tr, l, r));
}
void upd(int v, int tl, int tr, int l, int r, int val)
{
if (tl > tr || tl > r || tr < l) return;
if (tl >= l && tr <= r)
{
t[v] = val;
mod[v] = val;
return;
}
int m = (tl + tr) >> 1;
push(v);
upd(v + v, tl, m, l, r, val);
upd(v + v + 1, m + 1, tr, l, r, val);
t[v] = min(t[v + v], t[v + v + 1]);
}
int get_ans(int a, int b)
{
int ans = 0;
for(int i = 19; i >= 0; i--)
{
if (up[a][i] <= b)
a = up[a][i], ans += (1 << i);
}
return ans + 1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
vec.pb(l[i]);
vec.pb(r[i]);
}
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
for(int i = 1; i <= n; i++)
{
l[i] = lower_bound(all(vec), l[i]) - vec.begin(); l[i]++;
r[i] = lower_bound(all(vec), r[i]) - vec.begin(); r[i]++;
}
build(1, 1, 2 * n);
for(int i = n; i >= 1; i--)
{
int val = get(1, 1, 2 * n, l[i], r[i]);
nxt[i] = val;
upd(1, 1, 2 * n, l[i], r[i], i);
}
int mn = n + 1;
for(int i = n; i >= 1; i--)
{
mn = min(mn, nxt[i]);
nxt[i] = mn;
up[i][0] = nxt[i];
}
for(int j = 0; j <= 19; j++)
up[n + 1][j] = n + 1;
for(int j = 1; j <= 19; j++)
{
for(int i = 1; i <= n; i++)
up[i][j] = up[up[i][j - 1]][j - 1];
}
cin >> q;
while(q--)
{
int le, ri;
cin >> le >> ri;
cout << get_ans(le, ri) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
32528 KB |
Output is correct |
2 |
Correct |
398 ms |
32168 KB |
Output is correct |
3 |
Correct |
391 ms |
32244 KB |
Output is correct |
4 |
Correct |
387 ms |
32480 KB |
Output is correct |
5 |
Correct |
383 ms |
33548 KB |
Output is correct |
6 |
Correct |
169 ms |
31264 KB |
Output is correct |
7 |
Correct |
155 ms |
31640 KB |
Output is correct |
8 |
Correct |
163 ms |
31800 KB |
Output is correct |
9 |
Correct |
183 ms |
32348 KB |
Output is correct |
10 |
Correct |
155 ms |
31516 KB |
Output is correct |
11 |
Correct |
286 ms |
32488 KB |
Output is correct |
12 |
Correct |
324 ms |
31044 KB |
Output is correct |
13 |
Correct |
244 ms |
30988 KB |
Output is correct |
14 |
Correct |
285 ms |
32456 KB |
Output is correct |
15 |
Correct |
265 ms |
31876 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
177 ms |
32844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1276 KB |
Output is correct |
2 |
Correct |
8 ms |
1216 KB |
Output is correct |
3 |
Correct |
8 ms |
1284 KB |
Output is correct |
4 |
Correct |
8 ms |
1284 KB |
Output is correct |
5 |
Correct |
7 ms |
1224 KB |
Output is correct |
6 |
Correct |
5 ms |
1356 KB |
Output is correct |
7 |
Correct |
5 ms |
1228 KB |
Output is correct |
8 |
Correct |
5 ms |
1332 KB |
Output is correct |
9 |
Correct |
6 ms |
1228 KB |
Output is correct |
10 |
Correct |
5 ms |
1228 KB |
Output is correct |
11 |
Correct |
7 ms |
1276 KB |
Output is correct |
12 |
Correct |
6 ms |
1228 KB |
Output is correct |
13 |
Correct |
7 ms |
1228 KB |
Output is correct |
14 |
Correct |
7 ms |
1228 KB |
Output is correct |
15 |
Correct |
9 ms |
1316 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1276 KB |
Output is correct |
2 |
Correct |
8 ms |
1216 KB |
Output is correct |
3 |
Correct |
8 ms |
1284 KB |
Output is correct |
4 |
Correct |
8 ms |
1284 KB |
Output is correct |
5 |
Correct |
7 ms |
1224 KB |
Output is correct |
6 |
Correct |
5 ms |
1356 KB |
Output is correct |
7 |
Correct |
5 ms |
1228 KB |
Output is correct |
8 |
Correct |
5 ms |
1332 KB |
Output is correct |
9 |
Correct |
6 ms |
1228 KB |
Output is correct |
10 |
Correct |
5 ms |
1228 KB |
Output is correct |
11 |
Correct |
7 ms |
1276 KB |
Output is correct |
12 |
Correct |
6 ms |
1228 KB |
Output is correct |
13 |
Correct |
7 ms |
1228 KB |
Output is correct |
14 |
Correct |
7 ms |
1228 KB |
Output is correct |
15 |
Correct |
9 ms |
1316 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1316 KB |
Output is correct |
18 |
Correct |
69 ms |
3912 KB |
Output is correct |
19 |
Correct |
63 ms |
3624 KB |
Output is correct |
20 |
Correct |
73 ms |
4020 KB |
Output is correct |
21 |
Correct |
73 ms |
3652 KB |
Output is correct |
22 |
Correct |
66 ms |
3792 KB |
Output is correct |
23 |
Correct |
61 ms |
3780 KB |
Output is correct |
24 |
Correct |
66 ms |
3828 KB |
Output is correct |
25 |
Correct |
84 ms |
3864 KB |
Output is correct |
26 |
Correct |
66 ms |
3832 KB |
Output is correct |
27 |
Correct |
59 ms |
3672 KB |
Output is correct |
28 |
Correct |
48 ms |
3340 KB |
Output is correct |
29 |
Correct |
57 ms |
3372 KB |
Output is correct |
30 |
Correct |
54 ms |
3360 KB |
Output is correct |
31 |
Correct |
48 ms |
3320 KB |
Output is correct |
32 |
Correct |
50 ms |
3332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
50 ms |
3652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
387 ms |
33720 KB |
Output is correct |
2 |
Correct |
433 ms |
33080 KB |
Output is correct |
3 |
Correct |
347 ms |
31416 KB |
Output is correct |
4 |
Correct |
359 ms |
31224 KB |
Output is correct |
5 |
Correct |
375 ms |
31872 KB |
Output is correct |
6 |
Correct |
148 ms |
31544 KB |
Output is correct |
7 |
Correct |
148 ms |
31232 KB |
Output is correct |
8 |
Correct |
146 ms |
31408 KB |
Output is correct |
9 |
Correct |
146 ms |
31260 KB |
Output is correct |
10 |
Correct |
188 ms |
32972 KB |
Output is correct |
11 |
Correct |
254 ms |
30840 KB |
Output is correct |
12 |
Correct |
304 ms |
33336 KB |
Output is correct |
13 |
Correct |
241 ms |
30776 KB |
Output is correct |
14 |
Correct |
297 ms |
31492 KB |
Output is correct |
15 |
Correct |
266 ms |
32852 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
158 ms |
31736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
32528 KB |
Output is correct |
2 |
Correct |
398 ms |
32168 KB |
Output is correct |
3 |
Correct |
391 ms |
32244 KB |
Output is correct |
4 |
Correct |
387 ms |
32480 KB |
Output is correct |
5 |
Correct |
383 ms |
33548 KB |
Output is correct |
6 |
Correct |
169 ms |
31264 KB |
Output is correct |
7 |
Correct |
155 ms |
31640 KB |
Output is correct |
8 |
Correct |
163 ms |
31800 KB |
Output is correct |
9 |
Correct |
183 ms |
32348 KB |
Output is correct |
10 |
Correct |
155 ms |
31516 KB |
Output is correct |
11 |
Correct |
286 ms |
32488 KB |
Output is correct |
12 |
Correct |
324 ms |
31044 KB |
Output is correct |
13 |
Correct |
244 ms |
30988 KB |
Output is correct |
14 |
Correct |
285 ms |
32456 KB |
Output is correct |
15 |
Correct |
265 ms |
31876 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
177 ms |
32844 KB |
Output is correct |
18 |
Correct |
8 ms |
1276 KB |
Output is correct |
19 |
Correct |
8 ms |
1216 KB |
Output is correct |
20 |
Correct |
8 ms |
1284 KB |
Output is correct |
21 |
Correct |
8 ms |
1284 KB |
Output is correct |
22 |
Correct |
7 ms |
1224 KB |
Output is correct |
23 |
Correct |
5 ms |
1356 KB |
Output is correct |
24 |
Correct |
5 ms |
1228 KB |
Output is correct |
25 |
Correct |
5 ms |
1332 KB |
Output is correct |
26 |
Correct |
6 ms |
1228 KB |
Output is correct |
27 |
Correct |
5 ms |
1228 KB |
Output is correct |
28 |
Correct |
7 ms |
1276 KB |
Output is correct |
29 |
Correct |
6 ms |
1228 KB |
Output is correct |
30 |
Correct |
7 ms |
1228 KB |
Output is correct |
31 |
Correct |
7 ms |
1228 KB |
Output is correct |
32 |
Correct |
9 ms |
1316 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
4 ms |
1316 KB |
Output is correct |
35 |
Correct |
69 ms |
3912 KB |
Output is correct |
36 |
Correct |
63 ms |
3624 KB |
Output is correct |
37 |
Correct |
73 ms |
4020 KB |
Output is correct |
38 |
Correct |
73 ms |
3652 KB |
Output is correct |
39 |
Correct |
66 ms |
3792 KB |
Output is correct |
40 |
Correct |
61 ms |
3780 KB |
Output is correct |
41 |
Correct |
66 ms |
3828 KB |
Output is correct |
42 |
Correct |
84 ms |
3864 KB |
Output is correct |
43 |
Correct |
66 ms |
3832 KB |
Output is correct |
44 |
Correct |
59 ms |
3672 KB |
Output is correct |
45 |
Correct |
48 ms |
3340 KB |
Output is correct |
46 |
Correct |
57 ms |
3372 KB |
Output is correct |
47 |
Correct |
54 ms |
3360 KB |
Output is correct |
48 |
Correct |
48 ms |
3320 KB |
Output is correct |
49 |
Correct |
50 ms |
3332 KB |
Output is correct |
50 |
Correct |
0 ms |
332 KB |
Output is correct |
51 |
Correct |
50 ms |
3652 KB |
Output is correct |
52 |
Correct |
387 ms |
33720 KB |
Output is correct |
53 |
Correct |
433 ms |
33080 KB |
Output is correct |
54 |
Correct |
347 ms |
31416 KB |
Output is correct |
55 |
Correct |
359 ms |
31224 KB |
Output is correct |
56 |
Correct |
375 ms |
31872 KB |
Output is correct |
57 |
Correct |
148 ms |
31544 KB |
Output is correct |
58 |
Correct |
148 ms |
31232 KB |
Output is correct |
59 |
Correct |
146 ms |
31408 KB |
Output is correct |
60 |
Correct |
146 ms |
31260 KB |
Output is correct |
61 |
Correct |
188 ms |
32972 KB |
Output is correct |
62 |
Correct |
254 ms |
30840 KB |
Output is correct |
63 |
Correct |
304 ms |
33336 KB |
Output is correct |
64 |
Correct |
241 ms |
30776 KB |
Output is correct |
65 |
Correct |
297 ms |
31492 KB |
Output is correct |
66 |
Correct |
266 ms |
32852 KB |
Output is correct |
67 |
Correct |
1 ms |
296 KB |
Output is correct |
68 |
Correct |
158 ms |
31736 KB |
Output is correct |
69 |
Correct |
563 ms |
35404 KB |
Output is correct |
70 |
Correct |
570 ms |
36660 KB |
Output is correct |
71 |
Correct |
560 ms |
34724 KB |
Output is correct |
72 |
Correct |
590 ms |
35932 KB |
Output is correct |
73 |
Correct |
557 ms |
35568 KB |
Output is correct |
74 |
Correct |
351 ms |
36948 KB |
Output is correct |
75 |
Correct |
324 ms |
35148 KB |
Output is correct |
76 |
Correct |
368 ms |
37136 KB |
Output is correct |
77 |
Correct |
308 ms |
34864 KB |
Output is correct |
78 |
Correct |
332 ms |
35400 KB |
Output is correct |
79 |
Correct |
356 ms |
35772 KB |
Output is correct |
80 |
Correct |
321 ms |
33764 KB |
Output is correct |
81 |
Correct |
351 ms |
33672 KB |
Output is correct |
82 |
Correct |
342 ms |
35088 KB |
Output is correct |
83 |
Correct |
347 ms |
33740 KB |
Output is correct |
84 |
Correct |
0 ms |
332 KB |
Output is correct |
85 |
Correct |
229 ms |
35008 KB |
Output is correct |