#include <bits/stdc++.h>
using namespace std;
struct doan{
long long l, r;
};
long long n;
doan arr[1000005];
set <pair<long long, long long>> pi;
long long st[200005][18];
bool canInsert(long long pos) {
if (pi.empty()) return true;
auto it = pi.lower_bound({arr[pos].l, 1e18});
int nxt = it -> second;
if (arr[pos].l <= arr[nxt].l && arr[nxt].l <= arr[pos].r) return false;
if (it != pi.begin()) {
it--;
int prv = it -> second;
if (arr[prv].l <= arr[pos].l && arr[pos].l <= arr[prv].r) return false;
}
return true;
}
void preprocess() {
long long lim = 0;
for (int i = 1; i <= n; i++) {
if (lim < i) {
pi.clear();
lim = i;
pi.insert({arr[i].l, i});
}
while (lim + 1 <= n && canInsert(lim + 1)) {
lim++;
pi.insert({arr[lim].l, lim});
}
st[i][0] = lim + 1;
pi.erase({arr[i].l, i});
}
st[n + 1][0] = n + 1;
for (int i = 1; i < 18; i++) {
for (int j = 1; j <= n + 1; j++) {
st[j][i] = st[st[j][i - 1]][i - 1];
}
}
}
long long get(long long l, long long r) {
long long res = 1;
for (int i = 17; i >= 0; i--) {
if (st[l][i] <= r) {
l = st[l][i];
res += 1 << i;
}
}
return res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i].l >> arr[i].r;
}
long long t;
cin >> t;
preprocess();
while (t--) {
long long p, q;
cin >> p >> q;
cout << get(p, q) << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
36948 KB |
Output is correct |
2 |
Correct |
65 ms |
36864 KB |
Output is correct |
3 |
Correct |
74 ms |
36928 KB |
Output is correct |
4 |
Correct |
67 ms |
36948 KB |
Output is correct |
5 |
Correct |
70 ms |
37284 KB |
Output is correct |
6 |
Correct |
62 ms |
36992 KB |
Output is correct |
7 |
Correct |
61 ms |
36948 KB |
Output is correct |
8 |
Correct |
71 ms |
37064 KB |
Output is correct |
9 |
Correct |
68 ms |
37096 KB |
Output is correct |
10 |
Correct |
71 ms |
36692 KB |
Output is correct |
11 |
Correct |
180 ms |
48724 KB |
Output is correct |
12 |
Correct |
174 ms |
48044 KB |
Output is correct |
13 |
Correct |
167 ms |
47700 KB |
Output is correct |
14 |
Correct |
191 ms |
43604 KB |
Output is correct |
15 |
Correct |
148 ms |
41868 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
62 ms |
37236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4700 KB |
Output is correct |
2 |
Correct |
3 ms |
4700 KB |
Output is correct |
3 |
Correct |
3 ms |
4884 KB |
Output is correct |
4 |
Correct |
3 ms |
4700 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
4700 KB |
Output is correct |
8 |
Correct |
3 ms |
4700 KB |
Output is correct |
9 |
Correct |
5 ms |
4696 KB |
Output is correct |
10 |
Correct |
3 ms |
4700 KB |
Output is correct |
11 |
Correct |
4 ms |
4956 KB |
Output is correct |
12 |
Correct |
4 ms |
4956 KB |
Output is correct |
13 |
Correct |
4 ms |
4956 KB |
Output is correct |
14 |
Correct |
4 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4700 KB |
Output is correct |
2 |
Correct |
3 ms |
4700 KB |
Output is correct |
3 |
Correct |
3 ms |
4884 KB |
Output is correct |
4 |
Correct |
3 ms |
4700 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
4700 KB |
Output is correct |
8 |
Correct |
3 ms |
4700 KB |
Output is correct |
9 |
Correct |
5 ms |
4696 KB |
Output is correct |
10 |
Correct |
3 ms |
4700 KB |
Output is correct |
11 |
Correct |
4 ms |
4956 KB |
Output is correct |
12 |
Correct |
4 ms |
4956 KB |
Output is correct |
13 |
Correct |
4 ms |
4956 KB |
Output is correct |
14 |
Correct |
4 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
57 ms |
7504 KB |
Output is correct |
19 |
Correct |
51 ms |
7248 KB |
Output is correct |
20 |
Correct |
58 ms |
7372 KB |
Output is correct |
21 |
Correct |
66 ms |
7284 KB |
Output is correct |
22 |
Correct |
68 ms |
7508 KB |
Output is correct |
23 |
Correct |
54 ms |
7100 KB |
Output is correct |
24 |
Correct |
54 ms |
7252 KB |
Output is correct |
25 |
Correct |
59 ms |
7432 KB |
Output is correct |
26 |
Correct |
52 ms |
7396 KB |
Output is correct |
27 |
Correct |
60 ms |
7248 KB |
Output is correct |
28 |
Correct |
36 ms |
7004 KB |
Output is correct |
29 |
Correct |
38 ms |
7252 KB |
Output is correct |
30 |
Correct |
41 ms |
7284 KB |
Output is correct |
31 |
Correct |
40 ms |
6904 KB |
Output is correct |
32 |
Correct |
39 ms |
6996 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
39 ms |
7020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
37300 KB |
Output is correct |
2 |
Correct |
73 ms |
37180 KB |
Output is correct |
3 |
Correct |
65 ms |
36932 KB |
Output is correct |
4 |
Correct |
64 ms |
36916 KB |
Output is correct |
5 |
Correct |
63 ms |
37048 KB |
Output is correct |
6 |
Correct |
60 ms |
36924 KB |
Output is correct |
7 |
Correct |
60 ms |
36948 KB |
Output is correct |
8 |
Correct |
65 ms |
36908 KB |
Output is correct |
9 |
Correct |
69 ms |
36768 KB |
Output is correct |
10 |
Correct |
76 ms |
36948 KB |
Output is correct |
11 |
Correct |
178 ms |
47792 KB |
Output is correct |
12 |
Correct |
194 ms |
49216 KB |
Output is correct |
13 |
Correct |
176 ms |
47700 KB |
Output is correct |
14 |
Correct |
155 ms |
42208 KB |
Output is correct |
15 |
Correct |
172 ms |
43656 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
62 ms |
37072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
36948 KB |
Output is correct |
2 |
Correct |
65 ms |
36864 KB |
Output is correct |
3 |
Correct |
74 ms |
36928 KB |
Output is correct |
4 |
Correct |
67 ms |
36948 KB |
Output is correct |
5 |
Correct |
70 ms |
37284 KB |
Output is correct |
6 |
Correct |
62 ms |
36992 KB |
Output is correct |
7 |
Correct |
61 ms |
36948 KB |
Output is correct |
8 |
Correct |
71 ms |
37064 KB |
Output is correct |
9 |
Correct |
68 ms |
37096 KB |
Output is correct |
10 |
Correct |
71 ms |
36692 KB |
Output is correct |
11 |
Correct |
180 ms |
48724 KB |
Output is correct |
12 |
Correct |
174 ms |
48044 KB |
Output is correct |
13 |
Correct |
167 ms |
47700 KB |
Output is correct |
14 |
Correct |
191 ms |
43604 KB |
Output is correct |
15 |
Correct |
148 ms |
41868 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
62 ms |
37236 KB |
Output is correct |
18 |
Correct |
3 ms |
4700 KB |
Output is correct |
19 |
Correct |
3 ms |
4700 KB |
Output is correct |
20 |
Correct |
3 ms |
4884 KB |
Output is correct |
21 |
Correct |
3 ms |
4700 KB |
Output is correct |
22 |
Correct |
3 ms |
4700 KB |
Output is correct |
23 |
Correct |
3 ms |
4700 KB |
Output is correct |
24 |
Correct |
3 ms |
4700 KB |
Output is correct |
25 |
Correct |
3 ms |
4700 KB |
Output is correct |
26 |
Correct |
5 ms |
4696 KB |
Output is correct |
27 |
Correct |
3 ms |
4700 KB |
Output is correct |
28 |
Correct |
4 ms |
4956 KB |
Output is correct |
29 |
Correct |
4 ms |
4956 KB |
Output is correct |
30 |
Correct |
4 ms |
4956 KB |
Output is correct |
31 |
Correct |
4 ms |
4956 KB |
Output is correct |
32 |
Correct |
4 ms |
4956 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
3 ms |
4700 KB |
Output is correct |
35 |
Correct |
57 ms |
7504 KB |
Output is correct |
36 |
Correct |
51 ms |
7248 KB |
Output is correct |
37 |
Correct |
58 ms |
7372 KB |
Output is correct |
38 |
Correct |
66 ms |
7284 KB |
Output is correct |
39 |
Correct |
68 ms |
7508 KB |
Output is correct |
40 |
Correct |
54 ms |
7100 KB |
Output is correct |
41 |
Correct |
54 ms |
7252 KB |
Output is correct |
42 |
Correct |
59 ms |
7432 KB |
Output is correct |
43 |
Correct |
52 ms |
7396 KB |
Output is correct |
44 |
Correct |
60 ms |
7248 KB |
Output is correct |
45 |
Correct |
36 ms |
7004 KB |
Output is correct |
46 |
Correct |
38 ms |
7252 KB |
Output is correct |
47 |
Correct |
41 ms |
7284 KB |
Output is correct |
48 |
Correct |
40 ms |
6904 KB |
Output is correct |
49 |
Correct |
39 ms |
6996 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
39 ms |
7020 KB |
Output is correct |
52 |
Correct |
74 ms |
37300 KB |
Output is correct |
53 |
Correct |
73 ms |
37180 KB |
Output is correct |
54 |
Correct |
65 ms |
36932 KB |
Output is correct |
55 |
Correct |
64 ms |
36916 KB |
Output is correct |
56 |
Correct |
63 ms |
37048 KB |
Output is correct |
57 |
Correct |
60 ms |
36924 KB |
Output is correct |
58 |
Correct |
60 ms |
36948 KB |
Output is correct |
59 |
Correct |
65 ms |
36908 KB |
Output is correct |
60 |
Correct |
69 ms |
36768 KB |
Output is correct |
61 |
Correct |
76 ms |
36948 KB |
Output is correct |
62 |
Correct |
178 ms |
47792 KB |
Output is correct |
63 |
Correct |
194 ms |
49216 KB |
Output is correct |
64 |
Correct |
176 ms |
47700 KB |
Output is correct |
65 |
Correct |
155 ms |
42208 KB |
Output is correct |
66 |
Correct |
172 ms |
43656 KB |
Output is correct |
67 |
Correct |
1 ms |
2396 KB |
Output is correct |
68 |
Correct |
62 ms |
37072 KB |
Output is correct |
69 |
Correct |
283 ms |
40504 KB |
Output is correct |
70 |
Correct |
231 ms |
40544 KB |
Output is correct |
71 |
Correct |
212 ms |
40132 KB |
Output is correct |
72 |
Correct |
233 ms |
40348 KB |
Output is correct |
73 |
Correct |
212 ms |
40532 KB |
Output is correct |
74 |
Correct |
236 ms |
40832 KB |
Output is correct |
75 |
Correct |
274 ms |
40764 KB |
Output is correct |
76 |
Correct |
263 ms |
40812 KB |
Output is correct |
77 |
Correct |
207 ms |
40260 KB |
Output is correct |
78 |
Correct |
214 ms |
40568 KB |
Output is correct |
79 |
Correct |
271 ms |
51796 KB |
Output is correct |
80 |
Correct |
208 ms |
50512 KB |
Output is correct |
81 |
Correct |
220 ms |
50420 KB |
Output is correct |
82 |
Correct |
211 ms |
44884 KB |
Output is correct |
83 |
Correct |
204 ms |
44384 KB |
Output is correct |
84 |
Correct |
1 ms |
2396 KB |
Output is correct |
85 |
Correct |
116 ms |
40516 KB |
Output is correct |