#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
struct Inter {
int rl;
int rr;
int id;
bool operator<(const Inter& other) const {
if (rr != other.rr)
return rr < other.rr;
if (rl != other.rl)
return rl < other.rl;
return id < other.id;
}
};
const int MX_N = 1e5 + 42;
const int TREE_SIZE = (int)1 << 19;
int rl[MX_N];
int rr[MX_N];
int jump[20][MX_N];
int tree[2 * TREE_SIZE + 42];
void insert(int idx, int val, bool force = false) {
idx += TREE_SIZE;
if ((force) || (rl[val] < rl[tree[idx]]))
tree[idx] = val;
for (int i = idx / 2; i > 0; i /= 2) {
if (rl[tree[2 * i]] < rl[tree[2 * i + 1]])
tree[i] = tree[2 * i];
else
tree[i] = tree[2 * i + 1];
}
}
int query(int left, int right) {
left += TREE_SIZE;
right += TREE_SIZE;
int mnVal = 1e9;
int id = -1;
while (left <= right) {
if (left % 2 == 1) {
int newId = tree[left];
if (rl[newId] < mnVal) {
mnVal = rl[newId];
id = newId;
}
left++;
}
if (right % 2 == 0) {
int newId = tree[right];
if (rl[newId] < mnVal) {
mnVal = rl[newId];
id = newId;
}
right--;
}
left /= 2;
right /= 2;
}
return id;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<Inter> ranges {};
vector<int> vals {};
for (int i = 0; i < n; i++) {
cin >> rl[i] >> rr[i];
vals.push_back(rl[i]);
vals.push_back(rr[i]);
}
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
for (int i = 0; i < n; i++) {
rl[i] = lower_bound(vals.begin(), vals.end(), rl[i]) - vals.begin();
rr[i] = lower_bound(vals.begin(), vals.end(), rr[i]) - vals.begin();
ranges.push_back({rl[i], rr[i], i});
}
rl[n] = rr[n] = 1e9 + 42;
for (int i = 0; i < (int)vals.size(); i++)
insert(i, n, true);
sort(ranges.begin(), ranges.end());
for (int i = 0; i < n; i++) {
//cout << "doing range " << ranges[i].id << ' ' << ranges[i].rl << ' ' << ranges[i].rr << '\n';
jump[0][ranges[i].id] = query(ranges[i].rl, ranges[i].rr);
//cout << "query is " << jump[0][ranges[i].id] << '\n';
if (jump[0][ranges[i].id] == -1)
jump[0][ranges[i].id] = ranges[i].id;
//cout << "inserting at " << ranges[i].rr << '\n';
insert(ranges[i].rr, ranges[i].id);
//cout << "val at pos " << query(ranges[i].rr, ranges[i].rr) << '\n';
}
for (int i = 1; i <= 19; i++)
for (int j = 0; j < n; j++)
jump[i][j] = jump[i - 1][jump[i - 1][j]];
for (int i = 0; i < q; i++) {
int s, e;
cin >> s >> e;
s--, e--;
if (s == e) {
cout << "0\n";
continue;
}
if (rl[e] <= rr[s] && rr[s] <= rr[e]) {
cout << "1\n";
continue;
}
if (rl[s] > rl[e]) {
cout << "impossible\n";
continue;
}
int ans = 0;
//cout << "start has " << rl[s] << ' ' << rr[s] << '\n';
//cout << "bef jump left is " << rl[e] << " right is " << rr[e] << '\n';
for (int bit = 19; bit >= 0; bit--) {
int nxt = jump[bit][e];
if (rl[nxt] > rr[s]) {
//cout << "nxt has " << rl[nxt] << ' ' << rr[nxt] << '\n';
//cout << "jumping of " << (1 << bit) << '\n';
ans += (1 << bit);
e = nxt;
}
}
//cout << "preans " << ans << '\n';
//cout << "bef end left is " << rl[e] << " right is " << rr[e] << '\n';
ans++;
e = jump[0][e];
//cout << "last jump left is " << rl[e] << " right is " << rr[e] << '\n';
if (rl[e] <= rr[s] && rr[s] <= rr[e]) {
cout << ans + 1 << '\n';
} else {
cout << "impossible\n";
}
}
}
/*
7 1
1 2
3 4
1 5
6 7
5 10
10 20
15 20
1 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
113 ms |
26476 KB |
Output is correct |
3 |
Correct |
134 ms |
26704 KB |
Output is correct |
4 |
Correct |
219 ms |
26896 KB |
Output is correct |
5 |
Correct |
145 ms |
26968 KB |
Output is correct |
6 |
Correct |
143 ms |
27080 KB |
Output is correct |
7 |
Correct |
155 ms |
27264 KB |
Output is correct |
8 |
Correct |
177 ms |
28936 KB |
Output is correct |
9 |
Correct |
156 ms |
28856 KB |
Output is correct |
10 |
Correct |
150 ms |
27180 KB |
Output is correct |
11 |
Correct |
189 ms |
27740 KB |
Output is correct |
12 |
Correct |
95 ms |
27928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
764 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
764 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
760 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
764 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
43 ms |
3332 KB |
Output is correct |
20 |
Correct |
40 ms |
3432 KB |
Output is correct |
21 |
Correct |
38 ms |
3532 KB |
Output is correct |
22 |
Correct |
31 ms |
3660 KB |
Output is correct |
23 |
Correct |
25 ms |
3564 KB |
Output is correct |
24 |
Correct |
21 ms |
3560 KB |
Output is correct |
25 |
Correct |
30 ms |
3148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
764 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
764 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
760 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
88 ms |
26488 KB |
Output is correct |
20 |
Correct |
84 ms |
25656 KB |
Output is correct |
21 |
Correct |
96 ms |
26192 KB |
Output is correct |
22 |
Correct |
111 ms |
26876 KB |
Output is correct |
23 |
Correct |
112 ms |
27916 KB |
Output is correct |
24 |
Correct |
111 ms |
28048 KB |
Output is correct |
25 |
Correct |
48 ms |
24192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
25208 KB |
Output is correct |
2 |
Correct |
140 ms |
25348 KB |
Output is correct |
3 |
Correct |
204 ms |
25216 KB |
Output is correct |
4 |
Correct |
158 ms |
27220 KB |
Output is correct |
5 |
Correct |
171 ms |
26828 KB |
Output is correct |
6 |
Correct |
184 ms |
28172 KB |
Output is correct |
7 |
Correct |
176 ms |
28432 KB |
Output is correct |
8 |
Correct |
162 ms |
28732 KB |
Output is correct |
9 |
Correct |
110 ms |
27648 KB |
Output is correct |
10 |
Correct |
157 ms |
28060 KB |
Output is correct |
11 |
Correct |
158 ms |
27892 KB |
Output is correct |
12 |
Correct |
152 ms |
28096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
113 ms |
26476 KB |
Output is correct |
3 |
Correct |
134 ms |
26704 KB |
Output is correct |
4 |
Correct |
219 ms |
26896 KB |
Output is correct |
5 |
Correct |
145 ms |
26968 KB |
Output is correct |
6 |
Correct |
143 ms |
27080 KB |
Output is correct |
7 |
Correct |
155 ms |
27264 KB |
Output is correct |
8 |
Correct |
177 ms |
28936 KB |
Output is correct |
9 |
Correct |
156 ms |
28856 KB |
Output is correct |
10 |
Correct |
150 ms |
27180 KB |
Output is correct |
11 |
Correct |
189 ms |
27740 KB |
Output is correct |
12 |
Correct |
95 ms |
27928 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
764 KB |
Output is correct |
16 |
Correct |
1 ms |
768 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
764 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
760 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
764 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
43 ms |
3332 KB |
Output is correct |
32 |
Correct |
40 ms |
3432 KB |
Output is correct |
33 |
Correct |
38 ms |
3532 KB |
Output is correct |
34 |
Correct |
31 ms |
3660 KB |
Output is correct |
35 |
Correct |
25 ms |
3564 KB |
Output is correct |
36 |
Correct |
21 ms |
3560 KB |
Output is correct |
37 |
Correct |
30 ms |
3148 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
764 KB |
Output is correct |
43 |
Correct |
1 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
760 KB |
Output is correct |
46 |
Correct |
1 ms |
724 KB |
Output is correct |
47 |
Correct |
88 ms |
26488 KB |
Output is correct |
48 |
Correct |
84 ms |
25656 KB |
Output is correct |
49 |
Correct |
96 ms |
26192 KB |
Output is correct |
50 |
Correct |
111 ms |
26876 KB |
Output is correct |
51 |
Correct |
112 ms |
27916 KB |
Output is correct |
52 |
Correct |
111 ms |
28048 KB |
Output is correct |
53 |
Correct |
48 ms |
24192 KB |
Output is correct |
54 |
Correct |
116 ms |
25208 KB |
Output is correct |
55 |
Correct |
140 ms |
25348 KB |
Output is correct |
56 |
Correct |
204 ms |
25216 KB |
Output is correct |
57 |
Correct |
158 ms |
27220 KB |
Output is correct |
58 |
Correct |
171 ms |
26828 KB |
Output is correct |
59 |
Correct |
184 ms |
28172 KB |
Output is correct |
60 |
Correct |
176 ms |
28432 KB |
Output is correct |
61 |
Correct |
162 ms |
28732 KB |
Output is correct |
62 |
Correct |
110 ms |
27648 KB |
Output is correct |
63 |
Correct |
157 ms |
28060 KB |
Output is correct |
64 |
Correct |
158 ms |
27892 KB |
Output is correct |
65 |
Correct |
152 ms |
28096 KB |
Output is correct |
66 |
Correct |
0 ms |
468 KB |
Output is correct |
67 |
Correct |
115 ms |
28240 KB |
Output is correct |
68 |
Correct |
148 ms |
28232 KB |
Output is correct |
69 |
Correct |
200 ms |
28200 KB |
Output is correct |
70 |
Correct |
146 ms |
28488 KB |
Output is correct |
71 |
Correct |
144 ms |
28596 KB |
Output is correct |
72 |
Correct |
178 ms |
28744 KB |
Output is correct |
73 |
Correct |
161 ms |
30224 KB |
Output is correct |
74 |
Correct |
164 ms |
30264 KB |
Output is correct |
75 |
Correct |
159 ms |
28628 KB |
Output is correct |
76 |
Correct |
152 ms |
29300 KB |
Output is correct |
77 |
Correct |
71 ms |
27936 KB |
Output is correct |
78 |
Correct |
0 ms |
468 KB |
Output is correct |
79 |
Correct |
1 ms |
724 KB |
Output is correct |
80 |
Correct |
1 ms |
760 KB |
Output is correct |
81 |
Correct |
1 ms |
724 KB |
Output is correct |
82 |
Correct |
1 ms |
724 KB |
Output is correct |
83 |
Correct |
2 ms |
724 KB |
Output is correct |
84 |
Correct |
1 ms |
724 KB |
Output is correct |
85 |
Correct |
1 ms |
724 KB |
Output is correct |
86 |
Correct |
31 ms |
3256 KB |
Output is correct |
87 |
Correct |
43 ms |
3276 KB |
Output is correct |
88 |
Correct |
36 ms |
3568 KB |
Output is correct |
89 |
Correct |
31 ms |
3676 KB |
Output is correct |
90 |
Correct |
25 ms |
3536 KB |
Output is correct |
91 |
Correct |
22 ms |
3532 KB |
Output is correct |
92 |
Correct |
33 ms |
3128 KB |
Output is correct |
93 |
Correct |
88 ms |
26440 KB |
Output is correct |
94 |
Correct |
89 ms |
25712 KB |
Output is correct |
95 |
Correct |
98 ms |
26396 KB |
Output is correct |
96 |
Correct |
92 ms |
27068 KB |
Output is correct |
97 |
Correct |
111 ms |
28020 KB |
Output is correct |
98 |
Correct |
116 ms |
28088 KB |
Output is correct |
99 |
Correct |
51 ms |
24236 KB |
Output is correct |
100 |
Correct |
168 ms |
29920 KB |
Output is correct |
101 |
Correct |
170 ms |
29992 KB |
Output is correct |
102 |
Correct |
162 ms |
30008 KB |
Output is correct |
103 |
Correct |
151 ms |
29496 KB |
Output is correct |
104 |
Correct |
147 ms |
29328 KB |
Output is correct |
105 |
Correct |
170 ms |
29616 KB |
Output is correct |
106 |
Correct |
185 ms |
27320 KB |
Output is correct |
107 |
Correct |
183 ms |
27748 KB |
Output is correct |
108 |
Correct |
175 ms |
28352 KB |
Output is correct |
109 |
Correct |
153 ms |
28320 KB |
Output is correct |
110 |
Correct |
160 ms |
29804 KB |
Output is correct |
111 |
Correct |
164 ms |
29884 KB |
Output is correct |