#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.resize(unique(all(vec)) - vec.begin());
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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
28856 KB |
Output is correct |
2 |
Correct |
350 ms |
28544 KB |
Output is correct |
3 |
Correct |
348 ms |
28604 KB |
Output is correct |
4 |
Correct |
355 ms |
28792 KB |
Output is correct |
5 |
Correct |
387 ms |
29616 KB |
Output is correct |
6 |
Correct |
143 ms |
27732 KB |
Output is correct |
7 |
Correct |
147 ms |
28120 KB |
Output is correct |
8 |
Correct |
155 ms |
28272 KB |
Output is correct |
9 |
Correct |
162 ms |
28796 KB |
Output is correct |
10 |
Correct |
155 ms |
28152 KB |
Output is correct |
11 |
Correct |
268 ms |
29244 KB |
Output is correct |
12 |
Correct |
243 ms |
28056 KB |
Output is correct |
13 |
Correct |
246 ms |
27960 KB |
Output is correct |
14 |
Correct |
285 ms |
29224 KB |
Output is correct |
15 |
Correct |
251 ms |
28696 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
150 ms |
29040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1228 KB |
Output is correct |
2 |
Correct |
8 ms |
1100 KB |
Output is correct |
3 |
Correct |
8 ms |
1100 KB |
Output is correct |
4 |
Correct |
9 ms |
1100 KB |
Output is correct |
5 |
Correct |
8 ms |
1136 KB |
Output is correct |
6 |
Correct |
5 ms |
1228 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1200 KB |
Output is correct |
9 |
Correct |
5 ms |
1100 KB |
Output is correct |
10 |
Correct |
5 ms |
1100 KB |
Output is correct |
11 |
Correct |
6 ms |
1100 KB |
Output is correct |
12 |
Correct |
6 ms |
1100 KB |
Output is correct |
13 |
Correct |
6 ms |
1180 KB |
Output is correct |
14 |
Correct |
6 ms |
1100 KB |
Output is correct |
15 |
Correct |
6 ms |
1100 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1228 KB |
Output is correct |
2 |
Correct |
8 ms |
1100 KB |
Output is correct |
3 |
Correct |
8 ms |
1100 KB |
Output is correct |
4 |
Correct |
9 ms |
1100 KB |
Output is correct |
5 |
Correct |
8 ms |
1136 KB |
Output is correct |
6 |
Correct |
5 ms |
1228 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1200 KB |
Output is correct |
9 |
Correct |
5 ms |
1100 KB |
Output is correct |
10 |
Correct |
5 ms |
1100 KB |
Output is correct |
11 |
Correct |
6 ms |
1100 KB |
Output is correct |
12 |
Correct |
6 ms |
1100 KB |
Output is correct |
13 |
Correct |
6 ms |
1180 KB |
Output is correct |
14 |
Correct |
6 ms |
1100 KB |
Output is correct |
15 |
Correct |
6 ms |
1100 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
18 |
Correct |
70 ms |
2008 KB |
Output is correct |
19 |
Correct |
76 ms |
1920 KB |
Output is correct |
20 |
Correct |
90 ms |
1972 KB |
Output is correct |
21 |
Correct |
66 ms |
1936 KB |
Output is correct |
22 |
Correct |
69 ms |
1948 KB |
Output is correct |
23 |
Correct |
63 ms |
1904 KB |
Output is correct |
24 |
Correct |
70 ms |
1988 KB |
Output is correct |
25 |
Correct |
63 ms |
2000 KB |
Output is correct |
26 |
Correct |
62 ms |
1848 KB |
Output is correct |
27 |
Correct |
61 ms |
1860 KB |
Output is correct |
28 |
Correct |
48 ms |
1448 KB |
Output is correct |
29 |
Correct |
51 ms |
1476 KB |
Output is correct |
30 |
Correct |
52 ms |
1544 KB |
Output is correct |
31 |
Correct |
50 ms |
1428 KB |
Output is correct |
32 |
Correct |
54 ms |
1504 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
51 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
377 ms |
29796 KB |
Output is correct |
2 |
Correct |
370 ms |
29244 KB |
Output is correct |
3 |
Correct |
367 ms |
27852 KB |
Output is correct |
4 |
Correct |
336 ms |
27632 KB |
Output is correct |
5 |
Correct |
361 ms |
28360 KB |
Output is correct |
6 |
Correct |
142 ms |
27888 KB |
Output is correct |
7 |
Correct |
145 ms |
27748 KB |
Output is correct |
8 |
Correct |
148 ms |
28064 KB |
Output is correct |
9 |
Correct |
150 ms |
27668 KB |
Output is correct |
10 |
Correct |
167 ms |
29384 KB |
Output is correct |
11 |
Correct |
246 ms |
27832 KB |
Output is correct |
12 |
Correct |
276 ms |
29896 KB |
Output is correct |
13 |
Correct |
240 ms |
27868 KB |
Output is correct |
14 |
Correct |
251 ms |
28404 KB |
Output is correct |
15 |
Correct |
285 ms |
29632 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
147 ms |
28216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
28856 KB |
Output is correct |
2 |
Correct |
350 ms |
28544 KB |
Output is correct |
3 |
Correct |
348 ms |
28604 KB |
Output is correct |
4 |
Correct |
355 ms |
28792 KB |
Output is correct |
5 |
Correct |
387 ms |
29616 KB |
Output is correct |
6 |
Correct |
143 ms |
27732 KB |
Output is correct |
7 |
Correct |
147 ms |
28120 KB |
Output is correct |
8 |
Correct |
155 ms |
28272 KB |
Output is correct |
9 |
Correct |
162 ms |
28796 KB |
Output is correct |
10 |
Correct |
155 ms |
28152 KB |
Output is correct |
11 |
Correct |
268 ms |
29244 KB |
Output is correct |
12 |
Correct |
243 ms |
28056 KB |
Output is correct |
13 |
Correct |
246 ms |
27960 KB |
Output is correct |
14 |
Correct |
285 ms |
29224 KB |
Output is correct |
15 |
Correct |
251 ms |
28696 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
150 ms |
29040 KB |
Output is correct |
18 |
Correct |
8 ms |
1228 KB |
Output is correct |
19 |
Correct |
8 ms |
1100 KB |
Output is correct |
20 |
Correct |
8 ms |
1100 KB |
Output is correct |
21 |
Correct |
9 ms |
1100 KB |
Output is correct |
22 |
Correct |
8 ms |
1136 KB |
Output is correct |
23 |
Correct |
5 ms |
1228 KB |
Output is correct |
24 |
Correct |
5 ms |
1100 KB |
Output is correct |
25 |
Correct |
5 ms |
1200 KB |
Output is correct |
26 |
Correct |
5 ms |
1100 KB |
Output is correct |
27 |
Correct |
5 ms |
1100 KB |
Output is correct |
28 |
Correct |
6 ms |
1100 KB |
Output is correct |
29 |
Correct |
6 ms |
1100 KB |
Output is correct |
30 |
Correct |
6 ms |
1180 KB |
Output is correct |
31 |
Correct |
6 ms |
1100 KB |
Output is correct |
32 |
Correct |
6 ms |
1100 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
4 ms |
1100 KB |
Output is correct |
35 |
Correct |
70 ms |
2008 KB |
Output is correct |
36 |
Correct |
76 ms |
1920 KB |
Output is correct |
37 |
Correct |
90 ms |
1972 KB |
Output is correct |
38 |
Correct |
66 ms |
1936 KB |
Output is correct |
39 |
Correct |
69 ms |
1948 KB |
Output is correct |
40 |
Correct |
63 ms |
1904 KB |
Output is correct |
41 |
Correct |
70 ms |
1988 KB |
Output is correct |
42 |
Correct |
63 ms |
2000 KB |
Output is correct |
43 |
Correct |
62 ms |
1848 KB |
Output is correct |
44 |
Correct |
61 ms |
1860 KB |
Output is correct |
45 |
Correct |
48 ms |
1448 KB |
Output is correct |
46 |
Correct |
51 ms |
1476 KB |
Output is correct |
47 |
Correct |
52 ms |
1544 KB |
Output is correct |
48 |
Correct |
50 ms |
1428 KB |
Output is correct |
49 |
Correct |
54 ms |
1504 KB |
Output is correct |
50 |
Correct |
0 ms |
332 KB |
Output is correct |
51 |
Correct |
51 ms |
2168 KB |
Output is correct |
52 |
Correct |
377 ms |
29796 KB |
Output is correct |
53 |
Correct |
370 ms |
29244 KB |
Output is correct |
54 |
Correct |
367 ms |
27852 KB |
Output is correct |
55 |
Correct |
336 ms |
27632 KB |
Output is correct |
56 |
Correct |
361 ms |
28360 KB |
Output is correct |
57 |
Correct |
142 ms |
27888 KB |
Output is correct |
58 |
Correct |
145 ms |
27748 KB |
Output is correct |
59 |
Correct |
148 ms |
28064 KB |
Output is correct |
60 |
Correct |
150 ms |
27668 KB |
Output is correct |
61 |
Correct |
167 ms |
29384 KB |
Output is correct |
62 |
Correct |
246 ms |
27832 KB |
Output is correct |
63 |
Correct |
276 ms |
29896 KB |
Output is correct |
64 |
Correct |
240 ms |
27868 KB |
Output is correct |
65 |
Correct |
251 ms |
28404 KB |
Output is correct |
66 |
Correct |
285 ms |
29632 KB |
Output is correct |
67 |
Correct |
0 ms |
332 KB |
Output is correct |
68 |
Correct |
147 ms |
28216 KB |
Output is correct |
69 |
Correct |
522 ms |
29480 KB |
Output is correct |
70 |
Correct |
541 ms |
30536 KB |
Output is correct |
71 |
Correct |
507 ms |
28772 KB |
Output is correct |
72 |
Correct |
529 ms |
30068 KB |
Output is correct |
73 |
Correct |
535 ms |
29516 KB |
Output is correct |
74 |
Correct |
337 ms |
30680 KB |
Output is correct |
75 |
Correct |
318 ms |
29084 KB |
Output is correct |
76 |
Correct |
332 ms |
30704 KB |
Output is correct |
77 |
Correct |
296 ms |
28980 KB |
Output is correct |
78 |
Correct |
308 ms |
29240 KB |
Output is correct |
79 |
Correct |
336 ms |
30008 KB |
Output is correct |
80 |
Correct |
328 ms |
28472 KB |
Output is correct |
81 |
Correct |
317 ms |
28264 KB |
Output is correct |
82 |
Correct |
323 ms |
29496 KB |
Output is correct |
83 |
Correct |
303 ms |
28420 KB |
Output is correct |
84 |
Correct |
0 ms |
332 KB |
Output is correct |
85 |
Correct |
217 ms |
29292 KB |
Output is correct |