#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
const int K = 30;
struct Event {
int s, e, idx;
Event() {
s = INT_MAX;
e = INT_MAX;
idx = 0;
}
bool operator<(const Event &x) const {
return s < x.s;
}
};
struct Node {
Node *left, *right;
Event best; int tl, tr;
Node(int l_, int r_) : tl(l_), tr(r_), best() {}
void expand() {
if (left == nullptr) {
int tm = (tl + tr) / 2;
left = new Node(tl, tm);
right = new Node(tm+1, tr);
}
}
void upd(int pos, Event val) {
if (tl == tr) {
best = min(best, val);
} else {
expand();
int m = (tl + tr) / 2;
if (pos <= m) {
left->upd(pos, val);
} else {
right->upd(pos, val);
}
best = min(left->best, right->best);
}
}
Event qry(int l, int r) {
if (l > r) return Event();
if (l == tl && r == tr) {
return best;
} else {
expand();
int m = (tl + tr) / 2;
return min(left->qry(l, min(r, m)), right->qry(max(l, m+1), r));
}
}
};
int down[MAXN][K];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, q; cin >> n >> q;
Node *root = new Node(1, 2e9);
vector<Event> events(n+1);
for (int i = 1; i <= n; i++) {
cin >> events[i].s >> events[i].e; events[i].idx = i;
root->upd(events[i].e, events[i]);
}
for (int i = 1; i <= n; i++) {
down[i][0] = root->qry(events[i].s, events[i].e).idx;
}
for (int j = 1; j < K; j++) {
for (int i = 1; i <= n; i++) {
down[i][j] = down[down[i][j-1]][j-1];
}
}
while (q--) {
int a_, b_; cin >> a_ >> b_;
if (a_ == b_) {
cout << "0\n";
continue;
}
Event a = events[a_], b = events[b_];
if (a.e > b.e) {
cout << "impossible\n";
continue;
}
if (b.s <= a.e && a.e <= b.e) {
cout << "1\n";
continue;
}
int ans = 2;
for (int i = K-1; i >= 0; i--) {
int next = down[b.idx][i];
if (next && a.e < events[next].s) {
b = events[next];
ans += (1 << i);
}
}
b = events[down[b.idx][0]];
if (b.s > a.e || a.e > b.e) {
cout << "impossible\n";
continue;
}
cout << ans << "\n";
}
return 0;
}
Compilation message
events.cpp: In constructor 'Node::Node(int, int)':
events.cpp:23:25: warning: 'Node::tr' will be initialized after [-Wreorder]
23 | Event best; int tl, tr;
| ^~
events.cpp:23:11: warning: 'Event Node::best' [-Wreorder]
23 | Event best; int tl, tr;
| ^~~~
events.cpp:25:5: warning: when initialized here [-Wreorder]
25 | Node(int l_, int r_) : tl(l_), tr(r_), best() {}
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
374 ms |
131784 KB |
Output is correct |
3 |
Correct |
431 ms |
138992 KB |
Output is correct |
4 |
Correct |
454 ms |
141476 KB |
Output is correct |
5 |
Correct |
463 ms |
151404 KB |
Output is correct |
6 |
Correct |
429 ms |
158660 KB |
Output is correct |
7 |
Correct |
434 ms |
155008 KB |
Output is correct |
8 |
Correct |
443 ms |
208844 KB |
Output is correct |
9 |
Correct |
460 ms |
241500 KB |
Output is correct |
10 |
Correct |
414 ms |
132564 KB |
Output is correct |
11 |
Correct |
402 ms |
160136 KB |
Output is correct |
12 |
Correct |
152 ms |
33272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
4 ms |
2220 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
5 ms |
3924 KB |
Output is correct |
8 |
Correct |
6 ms |
3924 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
4 ms |
2220 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
5 ms |
3924 KB |
Output is correct |
8 |
Correct |
6 ms |
3924 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
2132 KB |
Output is correct |
13 |
Correct |
4 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
1364 KB |
Output is correct |
16 |
Correct |
5 ms |
3924 KB |
Output is correct |
17 |
Correct |
5 ms |
3832 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
40 ms |
2120 KB |
Output is correct |
20 |
Correct |
47 ms |
10120 KB |
Output is correct |
21 |
Correct |
37 ms |
3148 KB |
Output is correct |
22 |
Correct |
36 ms |
6728 KB |
Output is correct |
23 |
Correct |
42 ms |
17884 KB |
Output is correct |
24 |
Correct |
44 ms |
17888 KB |
Output is correct |
25 |
Correct |
74 ms |
17452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
4 ms |
2220 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
5 ms |
3924 KB |
Output is correct |
8 |
Correct |
6 ms |
3924 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
2132 KB |
Output is correct |
13 |
Correct |
3 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
1364 KB |
Output is correct |
16 |
Correct |
5 ms |
3924 KB |
Output is correct |
17 |
Correct |
5 ms |
3852 KB |
Output is correct |
18 |
Correct |
2 ms |
456 KB |
Output is correct |
19 |
Correct |
344 ms |
132384 KB |
Output is correct |
20 |
Correct |
145 ms |
23796 KB |
Output is correct |
21 |
Correct |
318 ms |
75292 KB |
Output is correct |
22 |
Correct |
344 ms |
147276 KB |
Output is correct |
23 |
Correct |
494 ms |
239152 KB |
Output is correct |
24 |
Correct |
477 ms |
238996 KB |
Output is correct |
25 |
Correct |
66 ms |
14408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
396 ms |
131660 KB |
Output is correct |
2 |
Correct |
424 ms |
138828 KB |
Output is correct |
3 |
Correct |
441 ms |
141480 KB |
Output is correct |
4 |
Correct |
466 ms |
241576 KB |
Output is correct |
5 |
Correct |
432 ms |
132476 KB |
Output is correct |
6 |
Correct |
539 ms |
241112 KB |
Output is correct |
7 |
Correct |
601 ms |
241124 KB |
Output is correct |
8 |
Correct |
511 ms |
241320 KB |
Output is correct |
9 |
Correct |
483 ms |
239192 KB |
Output is correct |
10 |
Correct |
546 ms |
240720 KB |
Output is correct |
11 |
Correct |
509 ms |
240588 KB |
Output is correct |
12 |
Correct |
549 ms |
240736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
374 ms |
131784 KB |
Output is correct |
3 |
Correct |
431 ms |
138992 KB |
Output is correct |
4 |
Correct |
454 ms |
141476 KB |
Output is correct |
5 |
Correct |
463 ms |
151404 KB |
Output is correct |
6 |
Correct |
429 ms |
158660 KB |
Output is correct |
7 |
Correct |
434 ms |
155008 KB |
Output is correct |
8 |
Correct |
443 ms |
208844 KB |
Output is correct |
9 |
Correct |
460 ms |
241500 KB |
Output is correct |
10 |
Correct |
414 ms |
132564 KB |
Output is correct |
11 |
Correct |
402 ms |
160136 KB |
Output is correct |
12 |
Correct |
152 ms |
33272 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
2136 KB |
Output is correct |
16 |
Correct |
4 ms |
2220 KB |
Output is correct |
17 |
Correct |
2 ms |
508 KB |
Output is correct |
18 |
Correct |
3 ms |
1364 KB |
Output is correct |
19 |
Correct |
5 ms |
3924 KB |
Output is correct |
20 |
Correct |
6 ms |
3924 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
4 ms |
2260 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
5 ms |
3924 KB |
Output is correct |
29 |
Correct |
5 ms |
3832 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
40 ms |
2120 KB |
Output is correct |
32 |
Correct |
47 ms |
10120 KB |
Output is correct |
33 |
Correct |
37 ms |
3148 KB |
Output is correct |
34 |
Correct |
36 ms |
6728 KB |
Output is correct |
35 |
Correct |
42 ms |
17884 KB |
Output is correct |
36 |
Correct |
44 ms |
17888 KB |
Output is correct |
37 |
Correct |
74 ms |
17452 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
3 ms |
2132 KB |
Output is correct |
41 |
Correct |
3 ms |
2260 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
1364 KB |
Output is correct |
44 |
Correct |
5 ms |
3924 KB |
Output is correct |
45 |
Correct |
5 ms |
3852 KB |
Output is correct |
46 |
Correct |
2 ms |
456 KB |
Output is correct |
47 |
Correct |
344 ms |
132384 KB |
Output is correct |
48 |
Correct |
145 ms |
23796 KB |
Output is correct |
49 |
Correct |
318 ms |
75292 KB |
Output is correct |
50 |
Correct |
344 ms |
147276 KB |
Output is correct |
51 |
Correct |
494 ms |
239152 KB |
Output is correct |
52 |
Correct |
477 ms |
238996 KB |
Output is correct |
53 |
Correct |
66 ms |
14408 KB |
Output is correct |
54 |
Correct |
396 ms |
131660 KB |
Output is correct |
55 |
Correct |
424 ms |
138828 KB |
Output is correct |
56 |
Correct |
441 ms |
141480 KB |
Output is correct |
57 |
Correct |
466 ms |
241576 KB |
Output is correct |
58 |
Correct |
432 ms |
132476 KB |
Output is correct |
59 |
Correct |
539 ms |
241112 KB |
Output is correct |
60 |
Correct |
601 ms |
241124 KB |
Output is correct |
61 |
Correct |
511 ms |
241320 KB |
Output is correct |
62 |
Correct |
483 ms |
239192 KB |
Output is correct |
63 |
Correct |
546 ms |
240720 KB |
Output is correct |
64 |
Correct |
509 ms |
240588 KB |
Output is correct |
65 |
Correct |
549 ms |
240736 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
436 ms |
134912 KB |
Output is correct |
68 |
Correct |
446 ms |
138884 KB |
Output is correct |
69 |
Correct |
466 ms |
141592 KB |
Output is correct |
70 |
Correct |
468 ms |
151412 KB |
Output is correct |
71 |
Correct |
444 ms |
158488 KB |
Output is correct |
72 |
Correct |
460 ms |
154868 KB |
Output is correct |
73 |
Correct |
444 ms |
208780 KB |
Output is correct |
74 |
Correct |
475 ms |
241412 KB |
Output is correct |
75 |
Correct |
390 ms |
132600 KB |
Output is correct |
76 |
Correct |
424 ms |
160056 KB |
Output is correct |
77 |
Correct |
146 ms |
33240 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
2256 KB |
Output is correct |
80 |
Correct |
3 ms |
2260 KB |
Output is correct |
81 |
Correct |
1 ms |
464 KB |
Output is correct |
82 |
Correct |
3 ms |
1364 KB |
Output is correct |
83 |
Correct |
5 ms |
3924 KB |
Output is correct |
84 |
Correct |
5 ms |
3888 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
39 ms |
3024 KB |
Output is correct |
87 |
Correct |
48 ms |
10128 KB |
Output is correct |
88 |
Correct |
37 ms |
3148 KB |
Output is correct |
89 |
Correct |
35 ms |
6732 KB |
Output is correct |
90 |
Correct |
41 ms |
17872 KB |
Output is correct |
91 |
Correct |
42 ms |
17892 KB |
Output is correct |
92 |
Correct |
51 ms |
17484 KB |
Output is correct |
93 |
Correct |
337 ms |
134272 KB |
Output is correct |
94 |
Correct |
157 ms |
23688 KB |
Output is correct |
95 |
Correct |
310 ms |
75292 KB |
Output is correct |
96 |
Correct |
348 ms |
147180 KB |
Output is correct |
97 |
Correct |
486 ms |
239232 KB |
Output is correct |
98 |
Correct |
453 ms |
239064 KB |
Output is correct |
99 |
Correct |
76 ms |
14408 KB |
Output is correct |
100 |
Correct |
552 ms |
241012 KB |
Output is correct |
101 |
Correct |
556 ms |
241228 KB |
Output is correct |
102 |
Correct |
504 ms |
241268 KB |
Output is correct |
103 |
Correct |
563 ms |
240808 KB |
Output is correct |
104 |
Correct |
511 ms |
240680 KB |
Output is correct |
105 |
Correct |
586 ms |
240728 KB |
Output is correct |
106 |
Correct |
216 ms |
23328 KB |
Output is correct |
107 |
Correct |
227 ms |
25680 KB |
Output is correct |
108 |
Correct |
341 ms |
77288 KB |
Output is correct |
109 |
Correct |
353 ms |
77388 KB |
Output is correct |
110 |
Correct |
540 ms |
240944 KB |
Output is correct |
111 |
Correct |
576 ms |
241096 KB |
Output is correct |