#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, mod = 1e9 + 7, inf = 1e9 + 5;
int tree[N * 8], lazy[N * 8];
int n;
int cor = 0;
void prob(int l, int r, int node)
{
tree[node] = min(tree[node], lazy[node]);
if (l != r)
lazy[node * 2] = min(lazy[node * 2], lazy[node]), lazy[node * 2 + 1] = min(lazy[node * 2 + 1], lazy[node]);
}
void update(int lq, int rq, int val, int l = 1, int r = cor, int node = 1)
{
prob(l, r, node);
if (lq <= l && r <= rq)
{
lazy[node] = min(val, lazy[node]);
prob(l, r, node);
return;
}
if (r < lq || l > rq)
return;
int mid = (l + r) / 2;
update(lq, rq, val, l, mid, node * 2), update(lq, rq, val, mid + 1, r, node * 2 + 1);
tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}
int query(int lq, int rq, int l = 1, int r = cor, int node = 1)
{
prob(l, r, node);
if (lq <= l && r <= rq)
return tree[node];
if (r < lq || l > rq)
return inf;
int mid = (l + r) / 2;
return min(query(lq, rq, l, mid, node * 2), query(lq, rq, mid + 1, r, node * 2 + 1));
}
map<int, int> mp;
int main()
{
ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
memset(lazy, '?', sizeof lazy);
memset(tree, '?', sizeof tree);
cin >> n;
vector<pair<int, int>> arr(n + 1);
for (int i = 1; i <= n; i++)
cin >> arr[i].first >> arr[i].second, mp[arr[i].first] = 1, mp[arr[i].second] = 1;
for (auto &v : mp)
v.second = ++cor;
for (int i = 1; i <= n; i++)
arr[i].first = mp[arr[i].first], arr[i].second = mp[arr[i].second];
vector<vector<int>> par(n + 2, vector<int>(22, n + 1));
for (int i = n; i >= 1; i--)
{
par[i][0] = par[i + 1][0];
par[i][0] = min(query(arr[i].first, arr[i].second), par[i][0]);
update(arr[i].first, arr[i].second, i);
}
for (int j = 1; j <= 21; j++)
for (int i = 1; i <= n; i++)
par[i][j] = par[par[i][j - 1]][j - 1];
int q; // code pls unhate me
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r; // zengy
int ans = 1;
for (int i = 21; i >= 0; i--)
if (par[l][i] <= r)
l = par[l][i], ans += (1 << i);
// aaa the num of teleports is 2^i? YES
cout << ans << "\n";
}
}
/*
3
1
3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
554 ms |
58488 KB |
Output is correct |
2 |
Correct |
591 ms |
57792 KB |
Output is correct |
3 |
Correct |
592 ms |
57736 KB |
Output is correct |
4 |
Correct |
586 ms |
58532 KB |
Output is correct |
5 |
Correct |
718 ms |
60652 KB |
Output is correct |
6 |
Correct |
69 ms |
38992 KB |
Output is correct |
7 |
Correct |
87 ms |
39764 KB |
Output is correct |
8 |
Correct |
83 ms |
39764 KB |
Output is correct |
9 |
Correct |
90 ms |
40328 KB |
Output is correct |
10 |
Correct |
94 ms |
39524 KB |
Output is correct |
11 |
Correct |
441 ms |
59036 KB |
Output is correct |
12 |
Correct |
362 ms |
56232 KB |
Output is correct |
13 |
Correct |
386 ms |
55860 KB |
Output is correct |
14 |
Correct |
393 ms |
58960 KB |
Output is correct |
15 |
Correct |
389 ms |
57632 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
85 ms |
40728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14172 KB |
Output is correct |
2 |
Correct |
11 ms |
13916 KB |
Output is correct |
3 |
Correct |
14 ms |
14136 KB |
Output is correct |
4 |
Correct |
11 ms |
14172 KB |
Output is correct |
5 |
Correct |
10 ms |
14128 KB |
Output is correct |
6 |
Correct |
5 ms |
13656 KB |
Output is correct |
7 |
Correct |
5 ms |
13712 KB |
Output is correct |
8 |
Correct |
5 ms |
13660 KB |
Output is correct |
9 |
Correct |
6 ms |
13660 KB |
Output is correct |
10 |
Correct |
5 ms |
13656 KB |
Output is correct |
11 |
Correct |
9 ms |
14172 KB |
Output is correct |
12 |
Correct |
8 ms |
13916 KB |
Output is correct |
13 |
Correct |
8 ms |
13916 KB |
Output is correct |
14 |
Correct |
9 ms |
14172 KB |
Output is correct |
15 |
Correct |
9 ms |
14016 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
13656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14172 KB |
Output is correct |
2 |
Correct |
11 ms |
13916 KB |
Output is correct |
3 |
Correct |
14 ms |
14136 KB |
Output is correct |
4 |
Correct |
11 ms |
14172 KB |
Output is correct |
5 |
Correct |
10 ms |
14128 KB |
Output is correct |
6 |
Correct |
5 ms |
13656 KB |
Output is correct |
7 |
Correct |
5 ms |
13712 KB |
Output is correct |
8 |
Correct |
5 ms |
13660 KB |
Output is correct |
9 |
Correct |
6 ms |
13660 KB |
Output is correct |
10 |
Correct |
5 ms |
13656 KB |
Output is correct |
11 |
Correct |
9 ms |
14172 KB |
Output is correct |
12 |
Correct |
8 ms |
13916 KB |
Output is correct |
13 |
Correct |
8 ms |
13916 KB |
Output is correct |
14 |
Correct |
9 ms |
14172 KB |
Output is correct |
15 |
Correct |
9 ms |
14016 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
13656 KB |
Output is correct |
18 |
Correct |
84 ms |
16664 KB |
Output is correct |
19 |
Correct |
63 ms |
16468 KB |
Output is correct |
20 |
Correct |
71 ms |
16860 KB |
Output is correct |
21 |
Correct |
64 ms |
16464 KB |
Output is correct |
22 |
Correct |
67 ms |
16468 KB |
Output is correct |
23 |
Correct |
61 ms |
16116 KB |
Output is correct |
24 |
Correct |
65 ms |
16212 KB |
Output is correct |
25 |
Correct |
62 ms |
16208 KB |
Output is correct |
26 |
Correct |
64 ms |
16208 KB |
Output is correct |
27 |
Correct |
56 ms |
15940 KB |
Output is correct |
28 |
Correct |
41 ms |
16096 KB |
Output is correct |
29 |
Correct |
45 ms |
16028 KB |
Output is correct |
30 |
Correct |
44 ms |
16212 KB |
Output is correct |
31 |
Correct |
43 ms |
15940 KB |
Output is correct |
32 |
Correct |
45 ms |
16232 KB |
Output is correct |
33 |
Correct |
2 ms |
12888 KB |
Output is correct |
34 |
Correct |
44 ms |
16040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
616 ms |
60496 KB |
Output is correct |
2 |
Correct |
605 ms |
59668 KB |
Output is correct |
3 |
Correct |
548 ms |
56496 KB |
Output is correct |
4 |
Correct |
498 ms |
55892 KB |
Output is correct |
5 |
Correct |
615 ms |
57500 KB |
Output is correct |
6 |
Correct |
70 ms |
39248 KB |
Output is correct |
7 |
Correct |
79 ms |
38996 KB |
Output is correct |
8 |
Correct |
80 ms |
39248 KB |
Output is correct |
9 |
Correct |
84 ms |
39092 KB |
Output is correct |
10 |
Correct |
100 ms |
41296 KB |
Output is correct |
11 |
Correct |
399 ms |
55640 KB |
Output is correct |
12 |
Correct |
428 ms |
60288 KB |
Output is correct |
13 |
Correct |
353 ms |
55572 KB |
Output is correct |
14 |
Correct |
448 ms |
57120 KB |
Output is correct |
15 |
Correct |
410 ms |
59772 KB |
Output is correct |
16 |
Correct |
2 ms |
13144 KB |
Output is correct |
17 |
Correct |
68 ms |
39636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
554 ms |
58488 KB |
Output is correct |
2 |
Correct |
591 ms |
57792 KB |
Output is correct |
3 |
Correct |
592 ms |
57736 KB |
Output is correct |
4 |
Correct |
586 ms |
58532 KB |
Output is correct |
5 |
Correct |
718 ms |
60652 KB |
Output is correct |
6 |
Correct |
69 ms |
38992 KB |
Output is correct |
7 |
Correct |
87 ms |
39764 KB |
Output is correct |
8 |
Correct |
83 ms |
39764 KB |
Output is correct |
9 |
Correct |
90 ms |
40328 KB |
Output is correct |
10 |
Correct |
94 ms |
39524 KB |
Output is correct |
11 |
Correct |
441 ms |
59036 KB |
Output is correct |
12 |
Correct |
362 ms |
56232 KB |
Output is correct |
13 |
Correct |
386 ms |
55860 KB |
Output is correct |
14 |
Correct |
393 ms |
58960 KB |
Output is correct |
15 |
Correct |
389 ms |
57632 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
85 ms |
40728 KB |
Output is correct |
18 |
Correct |
11 ms |
14172 KB |
Output is correct |
19 |
Correct |
11 ms |
13916 KB |
Output is correct |
20 |
Correct |
14 ms |
14136 KB |
Output is correct |
21 |
Correct |
11 ms |
14172 KB |
Output is correct |
22 |
Correct |
10 ms |
14128 KB |
Output is correct |
23 |
Correct |
5 ms |
13656 KB |
Output is correct |
24 |
Correct |
5 ms |
13712 KB |
Output is correct |
25 |
Correct |
5 ms |
13660 KB |
Output is correct |
26 |
Correct |
6 ms |
13660 KB |
Output is correct |
27 |
Correct |
5 ms |
13656 KB |
Output is correct |
28 |
Correct |
9 ms |
14172 KB |
Output is correct |
29 |
Correct |
8 ms |
13916 KB |
Output is correct |
30 |
Correct |
8 ms |
13916 KB |
Output is correct |
31 |
Correct |
9 ms |
14172 KB |
Output is correct |
32 |
Correct |
9 ms |
14016 KB |
Output is correct |
33 |
Correct |
2 ms |
12892 KB |
Output is correct |
34 |
Correct |
4 ms |
13656 KB |
Output is correct |
35 |
Correct |
84 ms |
16664 KB |
Output is correct |
36 |
Correct |
63 ms |
16468 KB |
Output is correct |
37 |
Correct |
71 ms |
16860 KB |
Output is correct |
38 |
Correct |
64 ms |
16464 KB |
Output is correct |
39 |
Correct |
67 ms |
16468 KB |
Output is correct |
40 |
Correct |
61 ms |
16116 KB |
Output is correct |
41 |
Correct |
65 ms |
16212 KB |
Output is correct |
42 |
Correct |
62 ms |
16208 KB |
Output is correct |
43 |
Correct |
64 ms |
16208 KB |
Output is correct |
44 |
Correct |
56 ms |
15940 KB |
Output is correct |
45 |
Correct |
41 ms |
16096 KB |
Output is correct |
46 |
Correct |
45 ms |
16028 KB |
Output is correct |
47 |
Correct |
44 ms |
16212 KB |
Output is correct |
48 |
Correct |
43 ms |
15940 KB |
Output is correct |
49 |
Correct |
45 ms |
16232 KB |
Output is correct |
50 |
Correct |
2 ms |
12888 KB |
Output is correct |
51 |
Correct |
44 ms |
16040 KB |
Output is correct |
52 |
Correct |
616 ms |
60496 KB |
Output is correct |
53 |
Correct |
605 ms |
59668 KB |
Output is correct |
54 |
Correct |
548 ms |
56496 KB |
Output is correct |
55 |
Correct |
498 ms |
55892 KB |
Output is correct |
56 |
Correct |
615 ms |
57500 KB |
Output is correct |
57 |
Correct |
70 ms |
39248 KB |
Output is correct |
58 |
Correct |
79 ms |
38996 KB |
Output is correct |
59 |
Correct |
80 ms |
39248 KB |
Output is correct |
60 |
Correct |
84 ms |
39092 KB |
Output is correct |
61 |
Correct |
100 ms |
41296 KB |
Output is correct |
62 |
Correct |
399 ms |
55640 KB |
Output is correct |
63 |
Correct |
428 ms |
60288 KB |
Output is correct |
64 |
Correct |
353 ms |
55572 KB |
Output is correct |
65 |
Correct |
448 ms |
57120 KB |
Output is correct |
66 |
Correct |
410 ms |
59772 KB |
Output is correct |
67 |
Correct |
2 ms |
13144 KB |
Output is correct |
68 |
Correct |
68 ms |
39636 KB |
Output is correct |
69 |
Correct |
759 ms |
60704 KB |
Output is correct |
70 |
Correct |
800 ms |
63188 KB |
Output is correct |
71 |
Correct |
701 ms |
59376 KB |
Output is correct |
72 |
Correct |
878 ms |
61772 KB |
Output is correct |
73 |
Correct |
764 ms |
61224 KB |
Output is correct |
74 |
Correct |
299 ms |
45096 KB |
Output is correct |
75 |
Correct |
325 ms |
42808 KB |
Output is correct |
76 |
Correct |
274 ms |
45168 KB |
Output is correct |
77 |
Correct |
246 ms |
42580 KB |
Output is correct |
78 |
Correct |
252 ms |
43196 KB |
Output is correct |
79 |
Correct |
465 ms |
62544 KB |
Output is correct |
80 |
Correct |
442 ms |
58940 KB |
Output is correct |
81 |
Correct |
403 ms |
58460 KB |
Output is correct |
82 |
Correct |
494 ms |
61264 KB |
Output is correct |
83 |
Correct |
433 ms |
58960 KB |
Output is correct |
84 |
Correct |
2 ms |
12892 KB |
Output is correct |
85 |
Correct |
134 ms |
42824 KB |
Output is correct |