#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';
}
}
Compilation message
Main.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
11 | #pragma GCC optimization ("O3")
|
Main.cpp:12: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
12 | #pragma GCC optimization ("unroll-loops")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
28828 KB |
Output is correct |
2 |
Correct |
379 ms |
28596 KB |
Output is correct |
3 |
Correct |
374 ms |
28584 KB |
Output is correct |
4 |
Correct |
369 ms |
28796 KB |
Output is correct |
5 |
Correct |
384 ms |
29700 KB |
Output is correct |
6 |
Correct |
151 ms |
27704 KB |
Output is correct |
7 |
Correct |
145 ms |
28084 KB |
Output is correct |
8 |
Correct |
144 ms |
28276 KB |
Output is correct |
9 |
Correct |
153 ms |
28804 KB |
Output is correct |
10 |
Correct |
152 ms |
28124 KB |
Output is correct |
11 |
Correct |
311 ms |
29244 KB |
Output is correct |
12 |
Correct |
270 ms |
28016 KB |
Output is correct |
13 |
Correct |
280 ms |
28028 KB |
Output is correct |
14 |
Correct |
274 ms |
29336 KB |
Output is correct |
15 |
Correct |
254 ms |
28660 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
150 ms |
29136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1224 KB |
Output is correct |
2 |
Correct |
8 ms |
1160 KB |
Output is correct |
3 |
Correct |
7 ms |
1108 KB |
Output is correct |
4 |
Correct |
8 ms |
1160 KB |
Output is correct |
5 |
Correct |
7 ms |
1116 KB |
Output is correct |
6 |
Correct |
5 ms |
1228 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
6 ms |
1100 KB |
Output is correct |
9 |
Correct |
5 ms |
1112 KB |
Output is correct |
10 |
Correct |
5 ms |
1100 KB |
Output is correct |
11 |
Correct |
6 ms |
1100 KB |
Output is correct |
12 |
Correct |
8 ms |
1164 KB |
Output is correct |
13 |
Correct |
7 ms |
1076 KB |
Output is correct |
14 |
Correct |
6 ms |
1100 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 |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1224 KB |
Output is correct |
2 |
Correct |
8 ms |
1160 KB |
Output is correct |
3 |
Correct |
7 ms |
1108 KB |
Output is correct |
4 |
Correct |
8 ms |
1160 KB |
Output is correct |
5 |
Correct |
7 ms |
1116 KB |
Output is correct |
6 |
Correct |
5 ms |
1228 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
6 ms |
1100 KB |
Output is correct |
9 |
Correct |
5 ms |
1112 KB |
Output is correct |
10 |
Correct |
5 ms |
1100 KB |
Output is correct |
11 |
Correct |
6 ms |
1100 KB |
Output is correct |
12 |
Correct |
8 ms |
1164 KB |
Output is correct |
13 |
Correct |
7 ms |
1076 KB |
Output is correct |
14 |
Correct |
6 ms |
1100 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 |
1228 KB |
Output is correct |
18 |
Correct |
69 ms |
1984 KB |
Output is correct |
19 |
Correct |
64 ms |
1860 KB |
Output is correct |
20 |
Correct |
70 ms |
1964 KB |
Output is correct |
21 |
Correct |
64 ms |
1884 KB |
Output is correct |
22 |
Correct |
73 ms |
1976 KB |
Output is correct |
23 |
Correct |
62 ms |
1924 KB |
Output is correct |
24 |
Correct |
65 ms |
1936 KB |
Output is correct |
25 |
Correct |
65 ms |
1900 KB |
Output is correct |
26 |
Correct |
64 ms |
1848 KB |
Output is correct |
27 |
Correct |
68 ms |
1984 KB |
Output is correct |
28 |
Correct |
47 ms |
1476 KB |
Output is correct |
29 |
Correct |
49 ms |
1436 KB |
Output is correct |
30 |
Correct |
51 ms |
1444 KB |
Output is correct |
31 |
Correct |
66 ms |
1492 KB |
Output is correct |
32 |
Correct |
54 ms |
1536 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
51 ms |
2116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
29720 KB |
Output is correct |
2 |
Correct |
385 ms |
29424 KB |
Output is correct |
3 |
Correct |
336 ms |
27832 KB |
Output is correct |
4 |
Correct |
349 ms |
27728 KB |
Output is correct |
5 |
Correct |
337 ms |
28332 KB |
Output is correct |
6 |
Correct |
137 ms |
27860 KB |
Output is correct |
7 |
Correct |
151 ms |
27760 KB |
Output is correct |
8 |
Correct |
143 ms |
27960 KB |
Output is correct |
9 |
Correct |
144 ms |
27756 KB |
Output is correct |
10 |
Correct |
157 ms |
29316 KB |
Output is correct |
11 |
Correct |
259 ms |
27816 KB |
Output is correct |
12 |
Correct |
263 ms |
29712 KB |
Output is correct |
13 |
Correct |
247 ms |
27872 KB |
Output is correct |
14 |
Correct |
259 ms |
28348 KB |
Output is correct |
15 |
Correct |
266 ms |
29624 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
137 ms |
28184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
28828 KB |
Output is correct |
2 |
Correct |
379 ms |
28596 KB |
Output is correct |
3 |
Correct |
374 ms |
28584 KB |
Output is correct |
4 |
Correct |
369 ms |
28796 KB |
Output is correct |
5 |
Correct |
384 ms |
29700 KB |
Output is correct |
6 |
Correct |
151 ms |
27704 KB |
Output is correct |
7 |
Correct |
145 ms |
28084 KB |
Output is correct |
8 |
Correct |
144 ms |
28276 KB |
Output is correct |
9 |
Correct |
153 ms |
28804 KB |
Output is correct |
10 |
Correct |
152 ms |
28124 KB |
Output is correct |
11 |
Correct |
311 ms |
29244 KB |
Output is correct |
12 |
Correct |
270 ms |
28016 KB |
Output is correct |
13 |
Correct |
280 ms |
28028 KB |
Output is correct |
14 |
Correct |
274 ms |
29336 KB |
Output is correct |
15 |
Correct |
254 ms |
28660 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
150 ms |
29136 KB |
Output is correct |
18 |
Correct |
8 ms |
1224 KB |
Output is correct |
19 |
Correct |
8 ms |
1160 KB |
Output is correct |
20 |
Correct |
7 ms |
1108 KB |
Output is correct |
21 |
Correct |
8 ms |
1160 KB |
Output is correct |
22 |
Correct |
7 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1228 KB |
Output is correct |
24 |
Correct |
5 ms |
1100 KB |
Output is correct |
25 |
Correct |
6 ms |
1100 KB |
Output is correct |
26 |
Correct |
5 ms |
1112 KB |
Output is correct |
27 |
Correct |
5 ms |
1100 KB |
Output is correct |
28 |
Correct |
6 ms |
1100 KB |
Output is correct |
29 |
Correct |
8 ms |
1164 KB |
Output is correct |
30 |
Correct |
7 ms |
1076 KB |
Output is correct |
31 |
Correct |
6 ms |
1100 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 |
1228 KB |
Output is correct |
35 |
Correct |
69 ms |
1984 KB |
Output is correct |
36 |
Correct |
64 ms |
1860 KB |
Output is correct |
37 |
Correct |
70 ms |
1964 KB |
Output is correct |
38 |
Correct |
64 ms |
1884 KB |
Output is correct |
39 |
Correct |
73 ms |
1976 KB |
Output is correct |
40 |
Correct |
62 ms |
1924 KB |
Output is correct |
41 |
Correct |
65 ms |
1936 KB |
Output is correct |
42 |
Correct |
65 ms |
1900 KB |
Output is correct |
43 |
Correct |
64 ms |
1848 KB |
Output is correct |
44 |
Correct |
68 ms |
1984 KB |
Output is correct |
45 |
Correct |
47 ms |
1476 KB |
Output is correct |
46 |
Correct |
49 ms |
1436 KB |
Output is correct |
47 |
Correct |
51 ms |
1444 KB |
Output is correct |
48 |
Correct |
66 ms |
1492 KB |
Output is correct |
49 |
Correct |
54 ms |
1536 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
51 ms |
2116 KB |
Output is correct |
52 |
Correct |
393 ms |
29720 KB |
Output is correct |
53 |
Correct |
385 ms |
29424 KB |
Output is correct |
54 |
Correct |
336 ms |
27832 KB |
Output is correct |
55 |
Correct |
349 ms |
27728 KB |
Output is correct |
56 |
Correct |
337 ms |
28332 KB |
Output is correct |
57 |
Correct |
137 ms |
27860 KB |
Output is correct |
58 |
Correct |
151 ms |
27760 KB |
Output is correct |
59 |
Correct |
143 ms |
27960 KB |
Output is correct |
60 |
Correct |
144 ms |
27756 KB |
Output is correct |
61 |
Correct |
157 ms |
29316 KB |
Output is correct |
62 |
Correct |
259 ms |
27816 KB |
Output is correct |
63 |
Correct |
263 ms |
29712 KB |
Output is correct |
64 |
Correct |
247 ms |
27872 KB |
Output is correct |
65 |
Correct |
259 ms |
28348 KB |
Output is correct |
66 |
Correct |
266 ms |
29624 KB |
Output is correct |
67 |
Correct |
0 ms |
332 KB |
Output is correct |
68 |
Correct |
137 ms |
28184 KB |
Output is correct |
69 |
Correct |
514 ms |
29356 KB |
Output is correct |
70 |
Correct |
564 ms |
30560 KB |
Output is correct |
71 |
Correct |
486 ms |
28848 KB |
Output is correct |
72 |
Correct |
512 ms |
29940 KB |
Output is correct |
73 |
Correct |
519 ms |
29556 KB |
Output is correct |
74 |
Correct |
319 ms |
30688 KB |
Output is correct |
75 |
Correct |
314 ms |
29240 KB |
Output is correct |
76 |
Correct |
329 ms |
30776 KB |
Output is correct |
77 |
Correct |
322 ms |
29100 KB |
Output is correct |
78 |
Correct |
277 ms |
29244 KB |
Output is correct |
79 |
Correct |
333 ms |
30196 KB |
Output is correct |
80 |
Correct |
297 ms |
28344 KB |
Output is correct |
81 |
Correct |
292 ms |
28268 KB |
Output is correct |
82 |
Correct |
319 ms |
29496 KB |
Output is correct |
83 |
Correct |
307 ms |
28388 KB |
Output is correct |
84 |
Correct |
0 ms |
332 KB |
Output is correct |
85 |
Correct |
210 ms |
29472 KB |
Output is correct |