#include <bits/stdc++.h>
using namespace std;
const int N = 1<<17;
const int INF = 0x3f3f3f3f;
int n, q;
pair<int, int> segs[N];
map<int, int> mp;
vector<int> stuff;
vector<pair<int, int>> bruh[N<<1];
int tree[N<<2];
int to[N][20];
void upd(int pos, int val, int tl = 0, int tr = (N<<1)-1, int v = 1) {
if (tr < pos || tl > pos) {
return;
}
if (tl == tr) {
tree[v] = min(tree[v], val);
return;
}
int tm = (tl+tr)>>1;
upd(pos, val, tl, tm, v<<1);
upd(pos, val, tm+1, tr, v<<1|1);
tree[v] = min(tree[v<<1], tree[v<<1|1]);
}
int query(int l, int r, int tl = 0, int tr = (N<<1)-1, int v = 1) {
if (tr < l || tl > r) {
return INF;
}
if (l <= tl && tr <= r) {
return tree[v];
}
int tm = (tl+tr)>>1;
return min(query(l, r, tl, tm, v<<1), query(l, r, tm+1, tr, v<<1|1));
}
int main() {
// freopen("a.in", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(tree, INF, sizeof tree);
memset(to, -1, sizeof to);
cin >> n >> q;
for (int i = 0; i < n; ++i) {
cin >> segs[i].first >> segs[i].second;
stuff.push_back(segs[i].first);
stuff.push_back(segs[i].second);
}
sort(stuff.begin(), stuff.end());
stuff.erase(unique(stuff.begin(), stuff.end()), stuff.end());
for (int i = 0; i < stuff.size(); ++i) {
mp[stuff[i]] = i;
}
for (int i = 0; i < n; ++i) {
segs[i].first = mp[segs[i].first];
segs[i].second = mp[segs[i].second];
upd(segs[i].second, segs[i].first);
bruh[segs[i].first].push_back({segs[i].second, i});
}
for (int i = 0; i < (N<<1); ++i) {
sort(bruh[i].begin(), bruh[i].end());
}
for (int i = 0; i < n; ++i) {
int val = query(segs[i].first, segs[i].second);
if (val < segs[i].first) {
to[i][0] = (*lower_bound(bruh[val].begin(), bruh[val].end(), pair<int, int>{segs[i].first, -1})).second;
}
}
for (int i = 1; i < 20; ++i) {
for (int j = 0; j < n; ++j) {
if (to[j][i-1] != -1) {
to[j][i] = to[to[j][i-1]][i-1];
}
}
}
while (q--) {
int s, e;
cin >> s >> e;
--s, --e;
if (s == e) {
cout << 0 << '\n';
continue;
}
if (segs[s].second > segs[e].second) {
cout << "impossible" << '\n';
continue;
}
if (segs[s].second >= segs[e].first) {
cout << 1 << '\n';
continue;
}
int ans = 0, cur = e;
for (int i = 19; i >= 0; --i) {
if (to[cur][i] != -1 && segs[s].second < segs[to[cur][i]].first) {
ans += (1<<i);
cur = to[cur][i];
}
}
if (to[cur][0] != -1) {
cout << ans+2 << '\n';
} else {
cout << "impossible" << '\n';
}
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < stuff.size(); ++i) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18772 KB |
Output is correct |
2 |
Correct |
204 ms |
29416 KB |
Output is correct |
3 |
Correct |
236 ms |
29216 KB |
Output is correct |
4 |
Correct |
284 ms |
29296 KB |
Output is correct |
5 |
Correct |
286 ms |
29904 KB |
Output is correct |
6 |
Correct |
230 ms |
30200 KB |
Output is correct |
7 |
Correct |
274 ms |
30364 KB |
Output is correct |
8 |
Correct |
249 ms |
34428 KB |
Output is correct |
9 |
Correct |
289 ms |
34444 KB |
Output is correct |
10 |
Correct |
294 ms |
29676 KB |
Output is correct |
11 |
Correct |
271 ms |
31552 KB |
Output is correct |
12 |
Correct |
131 ms |
28172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18796 KB |
Output is correct |
2 |
Correct |
12 ms |
18772 KB |
Output is correct |
3 |
Correct |
12 ms |
18888 KB |
Output is correct |
4 |
Correct |
11 ms |
18856 KB |
Output is correct |
5 |
Correct |
12 ms |
18900 KB |
Output is correct |
6 |
Correct |
12 ms |
18804 KB |
Output is correct |
7 |
Correct |
13 ms |
18900 KB |
Output is correct |
8 |
Correct |
11 ms |
18948 KB |
Output is correct |
9 |
Correct |
11 ms |
18748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18796 KB |
Output is correct |
2 |
Correct |
12 ms |
18772 KB |
Output is correct |
3 |
Correct |
12 ms |
18888 KB |
Output is correct |
4 |
Correct |
11 ms |
18856 KB |
Output is correct |
5 |
Correct |
12 ms |
18900 KB |
Output is correct |
6 |
Correct |
12 ms |
18804 KB |
Output is correct |
7 |
Correct |
13 ms |
18900 KB |
Output is correct |
8 |
Correct |
11 ms |
18948 KB |
Output is correct |
9 |
Correct |
11 ms |
18748 KB |
Output is correct |
10 |
Correct |
11 ms |
18800 KB |
Output is correct |
11 |
Correct |
10 ms |
18772 KB |
Output is correct |
12 |
Correct |
10 ms |
18900 KB |
Output is correct |
13 |
Correct |
13 ms |
18852 KB |
Output is correct |
14 |
Correct |
11 ms |
18892 KB |
Output is correct |
15 |
Correct |
11 ms |
18772 KB |
Output is correct |
16 |
Correct |
11 ms |
18864 KB |
Output is correct |
17 |
Correct |
13 ms |
18880 KB |
Output is correct |
18 |
Correct |
10 ms |
18772 KB |
Output is correct |
19 |
Correct |
47 ms |
20684 KB |
Output is correct |
20 |
Correct |
50 ms |
20744 KB |
Output is correct |
21 |
Correct |
48 ms |
20976 KB |
Output is correct |
22 |
Correct |
42 ms |
20968 KB |
Output is correct |
23 |
Correct |
39 ms |
21196 KB |
Output is correct |
24 |
Correct |
37 ms |
21104 KB |
Output is correct |
25 |
Correct |
45 ms |
20812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18796 KB |
Output is correct |
2 |
Correct |
12 ms |
18772 KB |
Output is correct |
3 |
Correct |
12 ms |
18888 KB |
Output is correct |
4 |
Correct |
11 ms |
18856 KB |
Output is correct |
5 |
Correct |
12 ms |
18900 KB |
Output is correct |
6 |
Correct |
12 ms |
18804 KB |
Output is correct |
7 |
Correct |
13 ms |
18900 KB |
Output is correct |
8 |
Correct |
11 ms |
18948 KB |
Output is correct |
9 |
Correct |
11 ms |
18748 KB |
Output is correct |
10 |
Correct |
10 ms |
18736 KB |
Output is correct |
11 |
Correct |
10 ms |
18772 KB |
Output is correct |
12 |
Correct |
10 ms |
18900 KB |
Output is correct |
13 |
Correct |
11 ms |
18900 KB |
Output is correct |
14 |
Correct |
10 ms |
18860 KB |
Output is correct |
15 |
Correct |
10 ms |
18772 KB |
Output is correct |
16 |
Correct |
11 ms |
18880 KB |
Output is correct |
17 |
Correct |
10 ms |
18900 KB |
Output is correct |
18 |
Correct |
10 ms |
18812 KB |
Output is correct |
19 |
Correct |
183 ms |
30120 KB |
Output is correct |
20 |
Correct |
204 ms |
29312 KB |
Output is correct |
21 |
Correct |
188 ms |
28716 KB |
Output is correct |
22 |
Correct |
232 ms |
31768 KB |
Output is correct |
23 |
Correct |
253 ms |
34788 KB |
Output is correct |
24 |
Correct |
317 ms |
34708 KB |
Output is correct |
25 |
Correct |
72 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
29312 KB |
Output is correct |
2 |
Correct |
244 ms |
29212 KB |
Output is correct |
3 |
Correct |
265 ms |
29412 KB |
Output is correct |
4 |
Correct |
274 ms |
34472 KB |
Output is correct |
5 |
Correct |
223 ms |
29644 KB |
Output is correct |
6 |
Correct |
309 ms |
34096 KB |
Output is correct |
7 |
Correct |
380 ms |
34160 KB |
Output is correct |
8 |
Correct |
307 ms |
34212 KB |
Output is correct |
9 |
Correct |
293 ms |
33140 KB |
Output is correct |
10 |
Correct |
305 ms |
33708 KB |
Output is correct |
11 |
Correct |
328 ms |
33636 KB |
Output is correct |
12 |
Correct |
289 ms |
33708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18772 KB |
Output is correct |
2 |
Correct |
204 ms |
29416 KB |
Output is correct |
3 |
Correct |
236 ms |
29216 KB |
Output is correct |
4 |
Correct |
284 ms |
29296 KB |
Output is correct |
5 |
Correct |
286 ms |
29904 KB |
Output is correct |
6 |
Correct |
230 ms |
30200 KB |
Output is correct |
7 |
Correct |
274 ms |
30364 KB |
Output is correct |
8 |
Correct |
249 ms |
34428 KB |
Output is correct |
9 |
Correct |
289 ms |
34444 KB |
Output is correct |
10 |
Correct |
294 ms |
29676 KB |
Output is correct |
11 |
Correct |
271 ms |
31552 KB |
Output is correct |
12 |
Correct |
131 ms |
28172 KB |
Output is correct |
13 |
Correct |
10 ms |
18796 KB |
Output is correct |
14 |
Correct |
12 ms |
18772 KB |
Output is correct |
15 |
Correct |
12 ms |
18888 KB |
Output is correct |
16 |
Correct |
11 ms |
18856 KB |
Output is correct |
17 |
Correct |
12 ms |
18900 KB |
Output is correct |
18 |
Correct |
12 ms |
18804 KB |
Output is correct |
19 |
Correct |
13 ms |
18900 KB |
Output is correct |
20 |
Correct |
11 ms |
18948 KB |
Output is correct |
21 |
Correct |
11 ms |
18748 KB |
Output is correct |
22 |
Correct |
11 ms |
18800 KB |
Output is correct |
23 |
Correct |
10 ms |
18772 KB |
Output is correct |
24 |
Correct |
10 ms |
18900 KB |
Output is correct |
25 |
Correct |
13 ms |
18852 KB |
Output is correct |
26 |
Correct |
11 ms |
18892 KB |
Output is correct |
27 |
Correct |
11 ms |
18772 KB |
Output is correct |
28 |
Correct |
11 ms |
18864 KB |
Output is correct |
29 |
Correct |
13 ms |
18880 KB |
Output is correct |
30 |
Correct |
10 ms |
18772 KB |
Output is correct |
31 |
Correct |
47 ms |
20684 KB |
Output is correct |
32 |
Correct |
50 ms |
20744 KB |
Output is correct |
33 |
Correct |
48 ms |
20976 KB |
Output is correct |
34 |
Correct |
42 ms |
20968 KB |
Output is correct |
35 |
Correct |
39 ms |
21196 KB |
Output is correct |
36 |
Correct |
37 ms |
21104 KB |
Output is correct |
37 |
Correct |
45 ms |
20812 KB |
Output is correct |
38 |
Correct |
10 ms |
18736 KB |
Output is correct |
39 |
Correct |
10 ms |
18772 KB |
Output is correct |
40 |
Correct |
10 ms |
18900 KB |
Output is correct |
41 |
Correct |
11 ms |
18900 KB |
Output is correct |
42 |
Correct |
10 ms |
18860 KB |
Output is correct |
43 |
Correct |
10 ms |
18772 KB |
Output is correct |
44 |
Correct |
11 ms |
18880 KB |
Output is correct |
45 |
Correct |
10 ms |
18900 KB |
Output is correct |
46 |
Correct |
10 ms |
18812 KB |
Output is correct |
47 |
Correct |
183 ms |
30120 KB |
Output is correct |
48 |
Correct |
204 ms |
29312 KB |
Output is correct |
49 |
Correct |
188 ms |
28716 KB |
Output is correct |
50 |
Correct |
232 ms |
31768 KB |
Output is correct |
51 |
Correct |
253 ms |
34788 KB |
Output is correct |
52 |
Correct |
317 ms |
34708 KB |
Output is correct |
53 |
Correct |
72 ms |
23132 KB |
Output is correct |
54 |
Correct |
212 ms |
29312 KB |
Output is correct |
55 |
Correct |
244 ms |
29212 KB |
Output is correct |
56 |
Correct |
265 ms |
29412 KB |
Output is correct |
57 |
Correct |
274 ms |
34472 KB |
Output is correct |
58 |
Correct |
223 ms |
29644 KB |
Output is correct |
59 |
Correct |
309 ms |
34096 KB |
Output is correct |
60 |
Correct |
380 ms |
34160 KB |
Output is correct |
61 |
Correct |
307 ms |
34212 KB |
Output is correct |
62 |
Correct |
293 ms |
33140 KB |
Output is correct |
63 |
Correct |
305 ms |
33708 KB |
Output is correct |
64 |
Correct |
328 ms |
33636 KB |
Output is correct |
65 |
Correct |
289 ms |
33708 KB |
Output is correct |
66 |
Correct |
11 ms |
18772 KB |
Output is correct |
67 |
Correct |
224 ms |
31116 KB |
Output is correct |
68 |
Correct |
216 ms |
31072 KB |
Output is correct |
69 |
Correct |
260 ms |
31016 KB |
Output is correct |
70 |
Correct |
233 ms |
31588 KB |
Output is correct |
71 |
Correct |
239 ms |
31968 KB |
Output is correct |
72 |
Correct |
284 ms |
32132 KB |
Output is correct |
73 |
Correct |
244 ms |
36168 KB |
Output is correct |
74 |
Correct |
227 ms |
36192 KB |
Output is correct |
75 |
Correct |
268 ms |
31376 KB |
Output is correct |
76 |
Correct |
267 ms |
33372 KB |
Output is correct |
77 |
Correct |
122 ms |
29996 KB |
Output is correct |
78 |
Correct |
10 ms |
18772 KB |
Output is correct |
79 |
Correct |
13 ms |
18848 KB |
Output is correct |
80 |
Correct |
14 ms |
18812 KB |
Output is correct |
81 |
Correct |
10 ms |
18900 KB |
Output is correct |
82 |
Correct |
10 ms |
18900 KB |
Output is correct |
83 |
Correct |
10 ms |
18964 KB |
Output is correct |
84 |
Correct |
10 ms |
18900 KB |
Output is correct |
85 |
Correct |
10 ms |
18768 KB |
Output is correct |
86 |
Correct |
55 ms |
20692 KB |
Output is correct |
87 |
Correct |
45 ms |
20760 KB |
Output is correct |
88 |
Correct |
48 ms |
20992 KB |
Output is correct |
89 |
Correct |
38 ms |
20944 KB |
Output is correct |
90 |
Correct |
42 ms |
21172 KB |
Output is correct |
91 |
Correct |
41 ms |
21092 KB |
Output is correct |
92 |
Correct |
47 ms |
20724 KB |
Output is correct |
93 |
Correct |
188 ms |
30076 KB |
Output is correct |
94 |
Correct |
240 ms |
29216 KB |
Output is correct |
95 |
Correct |
210 ms |
28772 KB |
Output is correct |
96 |
Correct |
202 ms |
31776 KB |
Output is correct |
97 |
Correct |
262 ms |
34732 KB |
Output is correct |
98 |
Correct |
271 ms |
34748 KB |
Output is correct |
99 |
Correct |
66 ms |
23128 KB |
Output is correct |
100 |
Correct |
366 ms |
35844 KB |
Output is correct |
101 |
Correct |
322 ms |
36016 KB |
Output is correct |
102 |
Correct |
300 ms |
36052 KB |
Output is correct |
103 |
Correct |
317 ms |
35584 KB |
Output is correct |
104 |
Correct |
324 ms |
35356 KB |
Output is correct |
105 |
Correct |
310 ms |
35656 KB |
Output is correct |
106 |
Correct |
209 ms |
29248 KB |
Output is correct |
107 |
Correct |
297 ms |
31304 KB |
Output is correct |
108 |
Correct |
225 ms |
29940 KB |
Output is correct |
109 |
Correct |
250 ms |
29948 KB |
Output is correct |
110 |
Correct |
319 ms |
35720 KB |
Output is correct |
111 |
Correct |
325 ms |
35784 KB |
Output is correct |