#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 >= 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], {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 && end[y] >= end[x]) {
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);
| ~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
110 ms |
10724 KB |
Output is correct |
3 |
Correct |
112 ms |
10836 KB |
Output is correct |
4 |
Correct |
122 ms |
11124 KB |
Output is correct |
5 |
Correct |
117 ms |
11348 KB |
Output is correct |
6 |
Correct |
123 ms |
11864 KB |
Output is correct |
7 |
Correct |
119 ms |
11888 KB |
Output is correct |
8 |
Correct |
111 ms |
18120 KB |
Output is correct |
9 |
Correct |
103 ms |
18180 KB |
Output is correct |
10 |
Correct |
109 ms |
12116 KB |
Output is correct |
11 |
Correct |
116 ms |
18320 KB |
Output is correct |
12 |
Correct |
70 ms |
13652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
24 ms |
1372 KB |
Output is correct |
20 |
Correct |
31 ms |
1424 KB |
Output is correct |
21 |
Correct |
23 ms |
1628 KB |
Output is correct |
22 |
Correct |
17 ms |
1624 KB |
Output is correct |
23 |
Correct |
18 ms |
1884 KB |
Output is correct |
24 |
Correct |
17 ms |
1884 KB |
Output is correct |
25 |
Correct |
23 ms |
1616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 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 |
520 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
78 ms |
10168 KB |
Output is correct |
20 |
Correct |
74 ms |
10324 KB |
Output is correct |
21 |
Correct |
87 ms |
10320 KB |
Output is correct |
22 |
Correct |
86 ms |
13904 KB |
Output is correct |
23 |
Correct |
91 ms |
16980 KB |
Output is correct |
24 |
Correct |
107 ms |
16980 KB |
Output is correct |
25 |
Correct |
15 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
10828 KB |
Output is correct |
2 |
Correct |
114 ms |
10832 KB |
Output is correct |
3 |
Correct |
113 ms |
10836 KB |
Output is correct |
4 |
Correct |
114 ms |
18000 KB |
Output is correct |
5 |
Correct |
112 ms |
12116 KB |
Output is correct |
6 |
Correct |
142 ms |
20724 KB |
Output is correct |
7 |
Correct |
136 ms |
20820 KB |
Output is correct |
8 |
Correct |
137 ms |
20980 KB |
Output is correct |
9 |
Correct |
97 ms |
18980 KB |
Output is correct |
10 |
Correct |
155 ms |
20564 KB |
Output is correct |
11 |
Correct |
133 ms |
20208 KB |
Output is correct |
12 |
Correct |
164 ms |
20560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
110 ms |
10724 KB |
Output is correct |
3 |
Correct |
112 ms |
10836 KB |
Output is correct |
4 |
Correct |
122 ms |
11124 KB |
Output is correct |
5 |
Correct |
117 ms |
11348 KB |
Output is correct |
6 |
Correct |
123 ms |
11864 KB |
Output is correct |
7 |
Correct |
119 ms |
11888 KB |
Output is correct |
8 |
Correct |
111 ms |
18120 KB |
Output is correct |
9 |
Correct |
103 ms |
18180 KB |
Output is correct |
10 |
Correct |
109 ms |
12116 KB |
Output is correct |
11 |
Correct |
116 ms |
18320 KB |
Output is correct |
12 |
Correct |
70 ms |
13652 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
24 ms |
1372 KB |
Output is correct |
32 |
Correct |
31 ms |
1424 KB |
Output is correct |
33 |
Correct |
23 ms |
1628 KB |
Output is correct |
34 |
Correct |
17 ms |
1624 KB |
Output is correct |
35 |
Correct |
18 ms |
1884 KB |
Output is correct |
36 |
Correct |
17 ms |
1884 KB |
Output is correct |
37 |
Correct |
23 ms |
1616 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
520 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
78 ms |
10168 KB |
Output is correct |
48 |
Correct |
74 ms |
10324 KB |
Output is correct |
49 |
Correct |
87 ms |
10320 KB |
Output is correct |
50 |
Correct |
86 ms |
13904 KB |
Output is correct |
51 |
Correct |
91 ms |
16980 KB |
Output is correct |
52 |
Correct |
107 ms |
16980 KB |
Output is correct |
53 |
Correct |
15 ms |
3420 KB |
Output is correct |
54 |
Correct |
120 ms |
10828 KB |
Output is correct |
55 |
Correct |
114 ms |
10832 KB |
Output is correct |
56 |
Correct |
113 ms |
10836 KB |
Output is correct |
57 |
Correct |
114 ms |
18000 KB |
Output is correct |
58 |
Correct |
112 ms |
12116 KB |
Output is correct |
59 |
Correct |
142 ms |
20724 KB |
Output is correct |
60 |
Correct |
136 ms |
20820 KB |
Output is correct |
61 |
Correct |
137 ms |
20980 KB |
Output is correct |
62 |
Correct |
97 ms |
18980 KB |
Output is correct |
63 |
Correct |
155 ms |
20564 KB |
Output is correct |
64 |
Correct |
133 ms |
20208 KB |
Output is correct |
65 |
Correct |
164 ms |
20560 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
115 ms |
13980 KB |
Output is correct |
68 |
Correct |
124 ms |
13904 KB |
Output is correct |
69 |
Correct |
116 ms |
13904 KB |
Output is correct |
70 |
Correct |
115 ms |
14416 KB |
Output is correct |
71 |
Correct |
122 ms |
14928 KB |
Output is correct |
72 |
Correct |
127 ms |
15184 KB |
Output is correct |
73 |
Correct |
110 ms |
21296 KB |
Output is correct |
74 |
Correct |
109 ms |
21132 KB |
Output is correct |
75 |
Correct |
120 ms |
14312 KB |
Output is correct |
76 |
Correct |
128 ms |
18212 KB |
Output is correct |
77 |
Correct |
70 ms |
13652 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
572 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
1 ms |
348 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
25 ms |
2376 KB |
Output is correct |
87 |
Correct |
27 ms |
2396 KB |
Output is correct |
88 |
Correct |
24 ms |
2620 KB |
Output is correct |
89 |
Correct |
18 ms |
2764 KB |
Output is correct |
90 |
Correct |
20 ms |
2908 KB |
Output is correct |
91 |
Correct |
20 ms |
2908 KB |
Output is correct |
92 |
Correct |
24 ms |
2524 KB |
Output is correct |
93 |
Correct |
82 ms |
12072 KB |
Output is correct |
94 |
Correct |
80 ms |
11348 KB |
Output is correct |
95 |
Correct |
89 ms |
12112 KB |
Output is correct |
96 |
Correct |
89 ms |
15956 KB |
Output is correct |
97 |
Correct |
92 ms |
19028 KB |
Output is correct |
98 |
Correct |
108 ms |
19024 KB |
Output is correct |
99 |
Correct |
17 ms |
4696 KB |
Output is correct |
100 |
Correct |
142 ms |
20880 KB |
Output is correct |
101 |
Correct |
144 ms |
20768 KB |
Output is correct |
102 |
Correct |
131 ms |
20924 KB |
Output is correct |
103 |
Correct |
158 ms |
20564 KB |
Output is correct |
104 |
Correct |
129 ms |
20176 KB |
Output is correct |
105 |
Correct |
166 ms |
20412 KB |
Output is correct |
106 |
Correct |
105 ms |
12084 KB |
Output is correct |
107 |
Correct |
119 ms |
13460 KB |
Output is correct |
108 |
Correct |
113 ms |
14160 KB |
Output is correct |
109 |
Correct |
110 ms |
14164 KB |
Output is correct |
110 |
Correct |
144 ms |
20664 KB |
Output is correct |
111 |
Correct |
160 ms |
20804 KB |
Output is correct |