#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
constexpr int INF = 1e9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
vector<pair<pii, int>> events(n);
vector<int> start(n), end(n);
map<int, int> mp;
for(int i = 0; i < n; i++) {
cin >> start[i] >> end[i];
events[i].first = {end[i], start[i]};
mp[start[i]] = mp[end[i]] = 1;
events[i].second = i;
}
int t = 0;
for(auto& [x, cmp] : mp)
cmp = t++;
sort(events.begin(), events.end());
int N = 1 << 32 - __builtin_clz(t - 1);
vector<pii> st(2 * N, {INF, -1});
vector<int> parent(n), jump(n), depth(n);
for(int i = 0; i < n; i++) {
int e = events[i].first.first, s = events[i].first.second;
pii ans = {INF, -1};
for(int l = N + mp[s], r = 2 * N; l < r; l >>= 1, r >>= 1) {
if(l & 1) ans = min(ans, st[l++]);
if(r & 1) ans = min(ans, st[--r]);
}
int v = events[i].second;
if(ans.second == -1 || ans.first >= mp[s])
jump[v] = parent[v] = v;
else {
int p = parent[v] = ans.second;
depth[v] = depth[p] + 1;
if(depth[p] - depth[jump[p]] == depth[jump[p]] - depth[jump[jump[p]]])
jump[v] = jump[jump[p]];
else
jump[v] = p;
}
for(int j = N + mp[e]; j > 0; j >>= 1)
st[j] = min(st[j], {mp[s], v});
}
while(q--) {
int x, y;
cin >> x >> y;
x--;
y--;
int d = depth[y];
while(!(end[x] >= start[y] && end[x] <= end[y]) && x != y && parent[y] != y) {
if(start[jump[y]] > start[x])
y = jump[y];
else
y = parent[y];
}
if(end[x] >= start[y] && end[x] <= end[y])
cout << (x!=y) + d - depth[y] << '\n';
else
cout << "impossible\n";
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:22:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
22 | for(auto& [x, cmp] : mp)
| ^
events.cpp:25:21: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
25 | int N = 1 << 32 - __builtin_clz(t - 1);
| ~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
201 ms |
13908 KB |
Output is correct |
3 |
Correct |
158 ms |
13936 KB |
Output is correct |
4 |
Correct |
188 ms |
13904 KB |
Output is correct |
5 |
Correct |
182 ms |
14364 KB |
Output is correct |
6 |
Correct |
173 ms |
14944 KB |
Output is correct |
7 |
Correct |
177 ms |
15184 KB |
Output is correct |
8 |
Correct |
178 ms |
21072 KB |
Output is correct |
9 |
Correct |
148 ms |
21076 KB |
Output is correct |
10 |
Execution timed out |
1542 ms |
12516 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
612 KB |
Output is correct |
8 |
Correct |
1 ms |
616 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
612 KB |
Output is correct |
8 |
Correct |
1 ms |
616 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
360 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
360 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
2 ms |
360 KB |
Output is correct |
16 |
Correct |
1 ms |
612 KB |
Output is correct |
17 |
Correct |
1 ms |
612 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
26 ms |
2388 KB |
Output is correct |
20 |
Correct |
46 ms |
2320 KB |
Output is correct |
21 |
Correct |
391 ms |
2560 KB |
Output is correct |
22 |
Correct |
18 ms |
2656 KB |
Output is correct |
23 |
Correct |
21 ms |
2908 KB |
Output is correct |
24 |
Correct |
20 ms |
2908 KB |
Output is correct |
25 |
Correct |
26 ms |
2524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
612 KB |
Output is correct |
8 |
Correct |
1 ms |
616 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
500 KB |
Output is correct |
16 |
Correct |
1 ms |
472 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
122 ms |
12080 KB |
Output is correct |
20 |
Correct |
154 ms |
11484 KB |
Output is correct |
21 |
Correct |
141 ms |
12116 KB |
Output is correct |
22 |
Correct |
136 ms |
15948 KB |
Output is correct |
23 |
Correct |
139 ms |
18788 KB |
Output is correct |
24 |
Correct |
181 ms |
18772 KB |
Output is correct |
25 |
Correct |
23 ms |
4840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
13908 KB |
Output is correct |
2 |
Correct |
163 ms |
13904 KB |
Output is correct |
3 |
Correct |
148 ms |
13908 KB |
Output is correct |
4 |
Correct |
152 ms |
21432 KB |
Output is correct |
5 |
Execution timed out |
1536 ms |
12336 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
201 ms |
13908 KB |
Output is correct |
3 |
Correct |
158 ms |
13936 KB |
Output is correct |
4 |
Correct |
188 ms |
13904 KB |
Output is correct |
5 |
Correct |
182 ms |
14364 KB |
Output is correct |
6 |
Correct |
173 ms |
14944 KB |
Output is correct |
7 |
Correct |
177 ms |
15184 KB |
Output is correct |
8 |
Correct |
178 ms |
21072 KB |
Output is correct |
9 |
Correct |
148 ms |
21076 KB |
Output is correct |
10 |
Execution timed out |
1542 ms |
12516 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |