#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 5;
int DP[20][N];
void Solve()
{
int n;
cin >> n;
vector<int> max_left(n + 1, 0);
vector<int> l(n + 1), r(n + 1);
multiset<pair<int, int> > s;
for(int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
}
int left = n + 1;
for(int right = n; right > 0; right--)
{
while(left > 1)
{
s.insert(make_pair(l[left - 1], r[left - 1]));
auto idx = s.lower_bound(make_pair(l[left - 1], r[left - 1]));
// cout << "right = " << right << " no. of elements = " << s.size() << "\n";
// if(idx == s.begin())
// {
// cout << "in the beginning\n";
// }
// if(next(idx) == s.end())
// {
// cout << "in the end\n";
// }
if(idx != s.begin() && prev(idx)->second >= l[left - 1])
{
s.erase(s.find(make_pair(l[left - 1], r[left - 1])));
break;
}
if(next(idx) != s.end() && next(idx)->first <= r[left - 1])
{
s.erase(s.find(make_pair(l[left - 1], r[left - 1])));
break;
}
left--;
}
max_left[right] = left - 1;
s.erase(s.find(make_pair(l[right], r[right])));
}
for(int i = 1; i <= n; i++)
{
// cout << max_left[i] << "\n";
DP[0][i] = max_left[i];
}
// cout << "\n";
for(int i = 1; i < 20; i++)
{
for(int j = 1; j <= n; j++)
{
if(DP[i - 1][j] == 0)
{
DP[i][j] = 0;
}
else
{
DP[i][j] = DP[i - 1][DP[i - 1][j]];
}
}
}
int queries;
cin >> queries;
while(queries--)
{
int right;
cin >> left >> right;
int ans = 0;
int cur_idx = right;
for(int i = 19; i >= 0; i--)
{
if(DP[i][cur_idx] >= left)
{
cur_idx = DP[i][cur_idx];
ans += (1 << i);
}
}
cout << ans + 1 << "\n";
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
34768 KB |
Output is correct |
2 |
Correct |
75 ms |
34316 KB |
Output is correct |
3 |
Correct |
85 ms |
34336 KB |
Output is correct |
4 |
Correct |
71 ms |
34792 KB |
Output is correct |
5 |
Correct |
77 ms |
36256 KB |
Output is correct |
6 |
Correct |
65 ms |
33012 KB |
Output is correct |
7 |
Correct |
74 ms |
33484 KB |
Output is correct |
8 |
Correct |
92 ms |
37460 KB |
Output is correct |
9 |
Correct |
79 ms |
38348 KB |
Output is correct |
10 |
Correct |
104 ms |
37056 KB |
Output is correct |
11 |
Correct |
253 ms |
50984 KB |
Output is correct |
12 |
Correct |
201 ms |
47960 KB |
Output is correct |
13 |
Correct |
233 ms |
47820 KB |
Output is correct |
14 |
Correct |
189 ms |
45740 KB |
Output is correct |
15 |
Correct |
183 ms |
42988 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
78 ms |
38908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1364 KB |
Output is correct |
2 |
Correct |
4 ms |
1236 KB |
Output is correct |
3 |
Correct |
4 ms |
1236 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
4 ms |
1236 KB |
Output is correct |
6 |
Correct |
4 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1496 KB |
Output is correct |
9 |
Correct |
4 ms |
1372 KB |
Output is correct |
10 |
Correct |
4 ms |
1364 KB |
Output is correct |
11 |
Correct |
7 ms |
1748 KB |
Output is correct |
12 |
Correct |
7 ms |
1620 KB |
Output is correct |
13 |
Correct |
6 ms |
1624 KB |
Output is correct |
14 |
Correct |
5 ms |
1492 KB |
Output is correct |
15 |
Correct |
5 ms |
1588 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1364 KB |
Output is correct |
2 |
Correct |
4 ms |
1236 KB |
Output is correct |
3 |
Correct |
4 ms |
1236 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
4 ms |
1236 KB |
Output is correct |
6 |
Correct |
4 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1496 KB |
Output is correct |
9 |
Correct |
4 ms |
1372 KB |
Output is correct |
10 |
Correct |
4 ms |
1364 KB |
Output is correct |
11 |
Correct |
7 ms |
1748 KB |
Output is correct |
12 |
Correct |
7 ms |
1620 KB |
Output is correct |
13 |
Correct |
6 ms |
1624 KB |
Output is correct |
14 |
Correct |
5 ms |
1492 KB |
Output is correct |
15 |
Correct |
5 ms |
1588 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
1492 KB |
Output is correct |
18 |
Correct |
72 ms |
4120 KB |
Output is correct |
19 |
Correct |
67 ms |
3768 KB |
Output is correct |
20 |
Correct |
81 ms |
4172 KB |
Output is correct |
21 |
Correct |
65 ms |
3832 KB |
Output is correct |
22 |
Correct |
73 ms |
3892 KB |
Output is correct |
23 |
Correct |
89 ms |
3816 KB |
Output is correct |
24 |
Correct |
71 ms |
3964 KB |
Output is correct |
25 |
Correct |
74 ms |
4048 KB |
Output is correct |
26 |
Correct |
73 ms |
3932 KB |
Output is correct |
27 |
Correct |
70 ms |
3824 KB |
Output is correct |
28 |
Correct |
53 ms |
3720 KB |
Output is correct |
29 |
Correct |
50 ms |
3860 KB |
Output is correct |
30 |
Correct |
63 ms |
3788 KB |
Output is correct |
31 |
Correct |
58 ms |
3588 KB |
Output is correct |
32 |
Correct |
57 ms |
3680 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
51 ms |
3736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
36264 KB |
Output is correct |
2 |
Correct |
92 ms |
35516 KB |
Output is correct |
3 |
Correct |
75 ms |
33160 KB |
Output is correct |
4 |
Correct |
68 ms |
32868 KB |
Output is correct |
5 |
Correct |
71 ms |
33880 KB |
Output is correct |
6 |
Correct |
66 ms |
33160 KB |
Output is correct |
7 |
Correct |
72 ms |
32884 KB |
Output is correct |
8 |
Correct |
100 ms |
36752 KB |
Output is correct |
9 |
Correct |
74 ms |
36404 KB |
Output is correct |
10 |
Correct |
88 ms |
39352 KB |
Output is correct |
11 |
Correct |
222 ms |
47488 KB |
Output is correct |
12 |
Correct |
239 ms |
52044 KB |
Output is correct |
13 |
Correct |
208 ms |
47380 KB |
Output is correct |
14 |
Correct |
208 ms |
42992 KB |
Output is correct |
15 |
Correct |
191 ms |
46312 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
70 ms |
37324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
34768 KB |
Output is correct |
2 |
Correct |
75 ms |
34316 KB |
Output is correct |
3 |
Correct |
85 ms |
34336 KB |
Output is correct |
4 |
Correct |
71 ms |
34792 KB |
Output is correct |
5 |
Correct |
77 ms |
36256 KB |
Output is correct |
6 |
Correct |
65 ms |
33012 KB |
Output is correct |
7 |
Correct |
74 ms |
33484 KB |
Output is correct |
8 |
Correct |
92 ms |
37460 KB |
Output is correct |
9 |
Correct |
79 ms |
38348 KB |
Output is correct |
10 |
Correct |
104 ms |
37056 KB |
Output is correct |
11 |
Correct |
253 ms |
50984 KB |
Output is correct |
12 |
Correct |
201 ms |
47960 KB |
Output is correct |
13 |
Correct |
233 ms |
47820 KB |
Output is correct |
14 |
Correct |
189 ms |
45740 KB |
Output is correct |
15 |
Correct |
183 ms |
42988 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
78 ms |
38908 KB |
Output is correct |
18 |
Correct |
4 ms |
1364 KB |
Output is correct |
19 |
Correct |
4 ms |
1236 KB |
Output is correct |
20 |
Correct |
4 ms |
1236 KB |
Output is correct |
21 |
Correct |
4 ms |
1364 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1364 KB |
Output is correct |
24 |
Correct |
4 ms |
1236 KB |
Output is correct |
25 |
Correct |
5 ms |
1496 KB |
Output is correct |
26 |
Correct |
4 ms |
1372 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
7 ms |
1748 KB |
Output is correct |
29 |
Correct |
7 ms |
1620 KB |
Output is correct |
30 |
Correct |
6 ms |
1624 KB |
Output is correct |
31 |
Correct |
5 ms |
1492 KB |
Output is correct |
32 |
Correct |
5 ms |
1588 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
1492 KB |
Output is correct |
35 |
Correct |
72 ms |
4120 KB |
Output is correct |
36 |
Correct |
67 ms |
3768 KB |
Output is correct |
37 |
Correct |
81 ms |
4172 KB |
Output is correct |
38 |
Correct |
65 ms |
3832 KB |
Output is correct |
39 |
Correct |
73 ms |
3892 KB |
Output is correct |
40 |
Correct |
89 ms |
3816 KB |
Output is correct |
41 |
Correct |
71 ms |
3964 KB |
Output is correct |
42 |
Correct |
74 ms |
4048 KB |
Output is correct |
43 |
Correct |
73 ms |
3932 KB |
Output is correct |
44 |
Correct |
70 ms |
3824 KB |
Output is correct |
45 |
Correct |
53 ms |
3720 KB |
Output is correct |
46 |
Correct |
50 ms |
3860 KB |
Output is correct |
47 |
Correct |
63 ms |
3788 KB |
Output is correct |
48 |
Correct |
58 ms |
3588 KB |
Output is correct |
49 |
Correct |
57 ms |
3680 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
51 ms |
3736 KB |
Output is correct |
52 |
Correct |
73 ms |
36264 KB |
Output is correct |
53 |
Correct |
92 ms |
35516 KB |
Output is correct |
54 |
Correct |
75 ms |
33160 KB |
Output is correct |
55 |
Correct |
68 ms |
32868 KB |
Output is correct |
56 |
Correct |
71 ms |
33880 KB |
Output is correct |
57 |
Correct |
66 ms |
33160 KB |
Output is correct |
58 |
Correct |
72 ms |
32884 KB |
Output is correct |
59 |
Correct |
100 ms |
36752 KB |
Output is correct |
60 |
Correct |
74 ms |
36404 KB |
Output is correct |
61 |
Correct |
88 ms |
39352 KB |
Output is correct |
62 |
Correct |
222 ms |
47488 KB |
Output is correct |
63 |
Correct |
239 ms |
52044 KB |
Output is correct |
64 |
Correct |
208 ms |
47380 KB |
Output is correct |
65 |
Correct |
208 ms |
42992 KB |
Output is correct |
66 |
Correct |
191 ms |
46312 KB |
Output is correct |
67 |
Correct |
1 ms |
468 KB |
Output is correct |
68 |
Correct |
70 ms |
37324 KB |
Output is correct |
69 |
Correct |
403 ms |
41048 KB |
Output is correct |
70 |
Correct |
366 ms |
43060 KB |
Output is correct |
71 |
Correct |
352 ms |
39760 KB |
Output is correct |
72 |
Correct |
405 ms |
41876 KB |
Output is correct |
73 |
Correct |
372 ms |
41292 KB |
Output is correct |
74 |
Correct |
371 ms |
43388 KB |
Output is correct |
75 |
Correct |
396 ms |
40424 KB |
Output is correct |
76 |
Correct |
367 ms |
43672 KB |
Output is correct |
77 |
Correct |
371 ms |
40228 KB |
Output is correct |
78 |
Correct |
371 ms |
40908 KB |
Output is correct |
79 |
Correct |
331 ms |
54728 KB |
Output is correct |
80 |
Correct |
358 ms |
50580 KB |
Output is correct |
81 |
Correct |
321 ms |
50408 KB |
Output is correct |
82 |
Correct |
294 ms |
46604 KB |
Output is correct |
83 |
Correct |
283 ms |
44604 KB |
Output is correct |
84 |
Correct |
0 ms |
468 KB |
Output is correct |
85 |
Correct |
175 ms |
40452 KB |
Output is correct |