#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);
int mn = n + 1;
for(int i = n; i >= 1; i--)
{
int val = get(1, 1, 2 * n, l[i], r[i]);
nxt[i] = val;
mn = min(mn, nxt[i]);
nxt[i] = mn;
up[i][0] = nxt[i];
upd(1, 1, 2 * n, l[i], r[i], 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 |
382 ms |
28756 KB |
Output is correct |
2 |
Correct |
362 ms |
28552 KB |
Output is correct |
3 |
Correct |
428 ms |
28532 KB |
Output is correct |
4 |
Correct |
374 ms |
28808 KB |
Output is correct |
5 |
Correct |
398 ms |
29652 KB |
Output is correct |
6 |
Correct |
139 ms |
27792 KB |
Output is correct |
7 |
Correct |
145 ms |
28044 KB |
Output is correct |
8 |
Correct |
156 ms |
28264 KB |
Output is correct |
9 |
Correct |
159 ms |
28728 KB |
Output is correct |
10 |
Correct |
171 ms |
28140 KB |
Output is correct |
11 |
Correct |
262 ms |
29328 KB |
Output is correct |
12 |
Correct |
289 ms |
28036 KB |
Output is correct |
13 |
Correct |
279 ms |
28008 KB |
Output is correct |
14 |
Correct |
290 ms |
29372 KB |
Output is correct |
15 |
Correct |
282 ms |
28724 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
148 ms |
29104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1228 KB |
Output is correct |
2 |
Correct |
11 ms |
1112 KB |
Output is correct |
3 |
Correct |
10 ms |
1100 KB |
Output is correct |
4 |
Correct |
8 ms |
1100 KB |
Output is correct |
5 |
Correct |
10 ms |
1188 KB |
Output is correct |
6 |
Correct |
5 ms |
1228 KB |
Output is correct |
7 |
Correct |
6 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1100 KB |
Output is correct |
9 |
Correct |
8 ms |
1188 KB |
Output is correct |
10 |
Correct |
4 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 |
1144 KB |
Output is correct |
14 |
Correct |
6 ms |
1168 KB |
Output is correct |
15 |
Correct |
6 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1228 KB |
Output is correct |
2 |
Correct |
11 ms |
1112 KB |
Output is correct |
3 |
Correct |
10 ms |
1100 KB |
Output is correct |
4 |
Correct |
8 ms |
1100 KB |
Output is correct |
5 |
Correct |
10 ms |
1188 KB |
Output is correct |
6 |
Correct |
5 ms |
1228 KB |
Output is correct |
7 |
Correct |
6 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1100 KB |
Output is correct |
9 |
Correct |
8 ms |
1188 KB |
Output is correct |
10 |
Correct |
4 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 |
1144 KB |
Output is correct |
14 |
Correct |
6 ms |
1168 KB |
Output is correct |
15 |
Correct |
6 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
18 |
Correct |
90 ms |
2068 KB |
Output is correct |
19 |
Correct |
68 ms |
1856 KB |
Output is correct |
20 |
Correct |
73 ms |
2040 KB |
Output is correct |
21 |
Correct |
68 ms |
1928 KB |
Output is correct |
22 |
Correct |
73 ms |
1988 KB |
Output is correct |
23 |
Correct |
70 ms |
2008 KB |
Output is correct |
24 |
Correct |
74 ms |
1932 KB |
Output is correct |
25 |
Correct |
63 ms |
1928 KB |
Output is correct |
26 |
Correct |
62 ms |
1860 KB |
Output is correct |
27 |
Correct |
59 ms |
1876 KB |
Output is correct |
28 |
Correct |
50 ms |
1476 KB |
Output is correct |
29 |
Correct |
58 ms |
1488 KB |
Output is correct |
30 |
Correct |
68 ms |
1524 KB |
Output is correct |
31 |
Correct |
50 ms |
1504 KB |
Output is correct |
32 |
Correct |
49 ms |
1556 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
51 ms |
2092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
449 ms |
29752 KB |
Output is correct |
2 |
Correct |
397 ms |
29312 KB |
Output is correct |
3 |
Correct |
481 ms |
27908 KB |
Output is correct |
4 |
Correct |
342 ms |
27676 KB |
Output is correct |
5 |
Correct |
383 ms |
28232 KB |
Output is correct |
6 |
Correct |
144 ms |
27960 KB |
Output is correct |
7 |
Correct |
150 ms |
27764 KB |
Output is correct |
8 |
Correct |
158 ms |
27964 KB |
Output is correct |
9 |
Correct |
150 ms |
27736 KB |
Output is correct |
10 |
Correct |
170 ms |
29264 KB |
Output is correct |
11 |
Correct |
269 ms |
27808 KB |
Output is correct |
12 |
Correct |
293 ms |
29660 KB |
Output is correct |
13 |
Correct |
273 ms |
27860 KB |
Output is correct |
14 |
Correct |
262 ms |
28432 KB |
Output is correct |
15 |
Correct |
270 ms |
29552 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
153 ms |
28212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
382 ms |
28756 KB |
Output is correct |
2 |
Correct |
362 ms |
28552 KB |
Output is correct |
3 |
Correct |
428 ms |
28532 KB |
Output is correct |
4 |
Correct |
374 ms |
28808 KB |
Output is correct |
5 |
Correct |
398 ms |
29652 KB |
Output is correct |
6 |
Correct |
139 ms |
27792 KB |
Output is correct |
7 |
Correct |
145 ms |
28044 KB |
Output is correct |
8 |
Correct |
156 ms |
28264 KB |
Output is correct |
9 |
Correct |
159 ms |
28728 KB |
Output is correct |
10 |
Correct |
171 ms |
28140 KB |
Output is correct |
11 |
Correct |
262 ms |
29328 KB |
Output is correct |
12 |
Correct |
289 ms |
28036 KB |
Output is correct |
13 |
Correct |
279 ms |
28008 KB |
Output is correct |
14 |
Correct |
290 ms |
29372 KB |
Output is correct |
15 |
Correct |
282 ms |
28724 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
148 ms |
29104 KB |
Output is correct |
18 |
Correct |
8 ms |
1228 KB |
Output is correct |
19 |
Correct |
11 ms |
1112 KB |
Output is correct |
20 |
Correct |
10 ms |
1100 KB |
Output is correct |
21 |
Correct |
8 ms |
1100 KB |
Output is correct |
22 |
Correct |
10 ms |
1188 KB |
Output is correct |
23 |
Correct |
5 ms |
1228 KB |
Output is correct |
24 |
Correct |
6 ms |
1100 KB |
Output is correct |
25 |
Correct |
5 ms |
1100 KB |
Output is correct |
26 |
Correct |
8 ms |
1188 KB |
Output is correct |
27 |
Correct |
4 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 |
1144 KB |
Output is correct |
31 |
Correct |
6 ms |
1168 KB |
Output is correct |
32 |
Correct |
6 ms |
1100 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
4 ms |
1100 KB |
Output is correct |
35 |
Correct |
90 ms |
2068 KB |
Output is correct |
36 |
Correct |
68 ms |
1856 KB |
Output is correct |
37 |
Correct |
73 ms |
2040 KB |
Output is correct |
38 |
Correct |
68 ms |
1928 KB |
Output is correct |
39 |
Correct |
73 ms |
1988 KB |
Output is correct |
40 |
Correct |
70 ms |
2008 KB |
Output is correct |
41 |
Correct |
74 ms |
1932 KB |
Output is correct |
42 |
Correct |
63 ms |
1928 KB |
Output is correct |
43 |
Correct |
62 ms |
1860 KB |
Output is correct |
44 |
Correct |
59 ms |
1876 KB |
Output is correct |
45 |
Correct |
50 ms |
1476 KB |
Output is correct |
46 |
Correct |
58 ms |
1488 KB |
Output is correct |
47 |
Correct |
68 ms |
1524 KB |
Output is correct |
48 |
Correct |
50 ms |
1504 KB |
Output is correct |
49 |
Correct |
49 ms |
1556 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
51 ms |
2092 KB |
Output is correct |
52 |
Correct |
449 ms |
29752 KB |
Output is correct |
53 |
Correct |
397 ms |
29312 KB |
Output is correct |
54 |
Correct |
481 ms |
27908 KB |
Output is correct |
55 |
Correct |
342 ms |
27676 KB |
Output is correct |
56 |
Correct |
383 ms |
28232 KB |
Output is correct |
57 |
Correct |
144 ms |
27960 KB |
Output is correct |
58 |
Correct |
150 ms |
27764 KB |
Output is correct |
59 |
Correct |
158 ms |
27964 KB |
Output is correct |
60 |
Correct |
150 ms |
27736 KB |
Output is correct |
61 |
Correct |
170 ms |
29264 KB |
Output is correct |
62 |
Correct |
269 ms |
27808 KB |
Output is correct |
63 |
Correct |
293 ms |
29660 KB |
Output is correct |
64 |
Correct |
273 ms |
27860 KB |
Output is correct |
65 |
Correct |
262 ms |
28432 KB |
Output is correct |
66 |
Correct |
270 ms |
29552 KB |
Output is correct |
67 |
Correct |
1 ms |
332 KB |
Output is correct |
68 |
Correct |
153 ms |
28212 KB |
Output is correct |
69 |
Correct |
532 ms |
29368 KB |
Output is correct |
70 |
Correct |
553 ms |
30540 KB |
Output is correct |
71 |
Correct |
584 ms |
28856 KB |
Output is correct |
72 |
Correct |
579 ms |
29956 KB |
Output is correct |
73 |
Correct |
592 ms |
29556 KB |
Output is correct |
74 |
Correct |
358 ms |
30728 KB |
Output is correct |
75 |
Correct |
353 ms |
29112 KB |
Output is correct |
76 |
Correct |
375 ms |
30776 KB |
Output is correct |
77 |
Correct |
290 ms |
28984 KB |
Output is correct |
78 |
Correct |
353 ms |
29300 KB |
Output is correct |
79 |
Correct |
334 ms |
30032 KB |
Output is correct |
80 |
Correct |
375 ms |
28360 KB |
Output is correct |
81 |
Correct |
314 ms |
28264 KB |
Output is correct |
82 |
Correct |
344 ms |
29456 KB |
Output is correct |
83 |
Correct |
326 ms |
28424 KB |
Output is correct |
84 |
Correct |
1 ms |
332 KB |
Output is correct |
85 |
Correct |
252 ms |
29528 KB |
Output is correct |