#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <map>
#define X first
#define Y second
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
const int LOG = 18;
const int OFF = 1 << LOG;
int n, t;
int S[OFF], E[OFF], up[OFF][LOG], T[2 * OFF];
vector<int> vri, sweep;
map<int, int> mp;
int merge(int a, int b) {
if(a == -1 || b == -1) return a > -1 ? a : b;
return S[a] < S[b] ? a : b;
}
void update(int a, int val) {
a += OFF;
T[a] = merge(T[a], val);
a /= 2;
while(a) {
T[a] = merge(T[2 * a], T[2 * a + 1]);
a /= 2;
}
}
int query(int l, int r, int lo = 0, int hi = OFF, int x = 1) {
if(r <= lo || hi <= l) return -1;
if(l <= lo && hi <= r) return T[x];
int mi = (lo + hi) / 2;
return merge(query(l, r, lo, mi, 2 * x), query(l, r, mi, hi, 2 * x + 1));
}
bool comp(int a, int b) {
return E[a] == E[b] ? S[a] < S[b] : E[a] < E[b];
}
pii climb(int a, int b) {
int res = 0;
for(int i = LOG - 1; i >= 0; i--) {
if(up[b][i] == -1) continue;
else if(E[a] < S[up[b][i]]) {
b = up[b][i];
res += 1 << i;
}
}
if(E[a] < S[b] && up[b][0] != -1) {
b = up[b][0];
res++;
}
return {b, res};
}
int main() {
memset(up, -1, sizeof(up));
memset(T, -1, sizeof(T));
scanf("%d%d", &n, &t);
for(int i = 0; i < n; i++) {
scanf("%d%d", S + i, E + i);
vri.pb(S[i]);
vri.pb(E[i]);
//vri.pb(E[i] + 1);
}
sort(vri.begin(), vri.end());
vri.erase(unique(vri.begin(), vri.end()), vri.end());
for(int i = 0; i < (int) vri.size(); i++) mp[vri[i]] = i;
for(int i = 0; i < n; i++) {
S[i] = mp[S[i]];
E[i] = mp[E[i]];
//printf("Si = %d Ei = %d\n", S[i] + 1, E[i] + 1);
sweep.pb(i);
}
sort(sweep.begin(), sweep.end(), comp);
for(int i : sweep) {
up[i][0] = query(S[i], E[i] + 1);
update(E[i], i);
if(S[i] <= S[up[i][0]]) up[i][0] = -1;
//printf("UP%d = %d\n", i, up[i][0]);
}
for(int j = 1; j < LOG; j++)
for(int i = 0; i < n; i++)
if(up[i][j - 1] != -1) up[i][j] = up[up[i][j - 1]][j - 1];
for(int i = 0; i < t; i++) {
int s, e;
scanf("%d%d", &s, &e);
//printf("%d %d\n", s, e);
s--;
e--;
// if(t >= 20) continue;
if(e == s) {
printf("0\n");
} else if(E[e] < E[s]) {
printf("impossible\n");
} else {
pii r = climb(s, e);
if(S[r.X] <= E[s] && E[s] <= E[r.X]) printf("%d\n", r.Y + 1);
else printf("impossible\n");
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d", &n, &t);
| ~~~~~^~~~~~~~~~~~~~~~
events.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d%d", S + i, E + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
events.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | scanf("%d%d", &s, &e);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
20820 KB |
Output is correct |
2 |
Correct |
179 ms |
31140 KB |
Output is correct |
3 |
Correct |
192 ms |
31704 KB |
Output is correct |
4 |
Correct |
227 ms |
31580 KB |
Output is correct |
5 |
Correct |
191 ms |
32180 KB |
Output is correct |
6 |
Correct |
208 ms |
32628 KB |
Output is correct |
7 |
Correct |
198 ms |
32764 KB |
Output is correct |
8 |
Correct |
199 ms |
36920 KB |
Output is correct |
9 |
Correct |
196 ms |
36824 KB |
Output is correct |
10 |
Correct |
190 ms |
31936 KB |
Output is correct |
11 |
Correct |
216 ms |
33908 KB |
Output is correct |
12 |
Correct |
135 ms |
31324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
20820 KB |
Output is correct |
2 |
Correct |
10 ms |
20812 KB |
Output is correct |
3 |
Correct |
9 ms |
20820 KB |
Output is correct |
4 |
Correct |
9 ms |
20832 KB |
Output is correct |
5 |
Correct |
10 ms |
20824 KB |
Output is correct |
6 |
Correct |
10 ms |
20816 KB |
Output is correct |
7 |
Correct |
12 ms |
20956 KB |
Output is correct |
8 |
Correct |
9 ms |
20948 KB |
Output is correct |
9 |
Correct |
9 ms |
20824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
20820 KB |
Output is correct |
2 |
Correct |
10 ms |
20812 KB |
Output is correct |
3 |
Correct |
9 ms |
20820 KB |
Output is correct |
4 |
Correct |
9 ms |
20832 KB |
Output is correct |
5 |
Correct |
10 ms |
20824 KB |
Output is correct |
6 |
Correct |
10 ms |
20816 KB |
Output is correct |
7 |
Correct |
12 ms |
20956 KB |
Output is correct |
8 |
Correct |
9 ms |
20948 KB |
Output is correct |
9 |
Correct |
9 ms |
20824 KB |
Output is correct |
10 |
Correct |
8 ms |
20820 KB |
Output is correct |
11 |
Correct |
9 ms |
20772 KB |
Output is correct |
12 |
Correct |
9 ms |
20864 KB |
Output is correct |
13 |
Correct |
10 ms |
20820 KB |
Output is correct |
14 |
Correct |
9 ms |
20840 KB |
Output is correct |
15 |
Correct |
9 ms |
20832 KB |
Output is correct |
16 |
Correct |
9 ms |
20888 KB |
Output is correct |
17 |
Correct |
10 ms |
20948 KB |
Output is correct |
18 |
Correct |
9 ms |
20820 KB |
Output is correct |
19 |
Correct |
53 ms |
22800 KB |
Output is correct |
20 |
Correct |
44 ms |
22704 KB |
Output is correct |
21 |
Correct |
53 ms |
22936 KB |
Output is correct |
22 |
Correct |
40 ms |
23020 KB |
Output is correct |
23 |
Correct |
42 ms |
23092 KB |
Output is correct |
24 |
Correct |
42 ms |
23060 KB |
Output is correct |
25 |
Correct |
45 ms |
22732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
20820 KB |
Output is correct |
2 |
Correct |
10 ms |
20812 KB |
Output is correct |
3 |
Correct |
9 ms |
20820 KB |
Output is correct |
4 |
Correct |
9 ms |
20832 KB |
Output is correct |
5 |
Correct |
10 ms |
20824 KB |
Output is correct |
6 |
Correct |
10 ms |
20816 KB |
Output is correct |
7 |
Correct |
12 ms |
20956 KB |
Output is correct |
8 |
Correct |
9 ms |
20948 KB |
Output is correct |
9 |
Correct |
9 ms |
20824 KB |
Output is correct |
10 |
Correct |
9 ms |
20820 KB |
Output is correct |
11 |
Correct |
9 ms |
20776 KB |
Output is correct |
12 |
Correct |
9 ms |
20820 KB |
Output is correct |
13 |
Correct |
12 ms |
20820 KB |
Output is correct |
14 |
Correct |
10 ms |
20820 KB |
Output is correct |
15 |
Correct |
10 ms |
20776 KB |
Output is correct |
16 |
Correct |
9 ms |
20960 KB |
Output is correct |
17 |
Correct |
12 ms |
20852 KB |
Output is correct |
18 |
Correct |
16 ms |
20820 KB |
Output is correct |
19 |
Correct |
163 ms |
29892 KB |
Output is correct |
20 |
Correct |
140 ms |
29128 KB |
Output is correct |
21 |
Correct |
163 ms |
29828 KB |
Output is correct |
22 |
Correct |
171 ms |
31700 KB |
Output is correct |
23 |
Correct |
232 ms |
34604 KB |
Output is correct |
24 |
Correct |
212 ms |
34600 KB |
Output is correct |
25 |
Correct |
56 ms |
24512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
231 ms |
31040 KB |
Output is correct |
2 |
Correct |
203 ms |
31644 KB |
Output is correct |
3 |
Correct |
205 ms |
31552 KB |
Output is correct |
4 |
Correct |
222 ms |
36864 KB |
Output is correct |
5 |
Correct |
207 ms |
31932 KB |
Output is correct |
6 |
Correct |
277 ms |
36412 KB |
Output is correct |
7 |
Correct |
249 ms |
36472 KB |
Output is correct |
8 |
Correct |
240 ms |
36612 KB |
Output is correct |
9 |
Correct |
209 ms |
34600 KB |
Output is correct |
10 |
Correct |
239 ms |
36048 KB |
Output is correct |
11 |
Correct |
232 ms |
35860 KB |
Output is correct |
12 |
Correct |
253 ms |
36080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
20820 KB |
Output is correct |
2 |
Correct |
179 ms |
31140 KB |
Output is correct |
3 |
Correct |
192 ms |
31704 KB |
Output is correct |
4 |
Correct |
227 ms |
31580 KB |
Output is correct |
5 |
Correct |
191 ms |
32180 KB |
Output is correct |
6 |
Correct |
208 ms |
32628 KB |
Output is correct |
7 |
Correct |
198 ms |
32764 KB |
Output is correct |
8 |
Correct |
199 ms |
36920 KB |
Output is correct |
9 |
Correct |
196 ms |
36824 KB |
Output is correct |
10 |
Correct |
190 ms |
31936 KB |
Output is correct |
11 |
Correct |
216 ms |
33908 KB |
Output is correct |
12 |
Correct |
135 ms |
31324 KB |
Output is correct |
13 |
Correct |
9 ms |
20820 KB |
Output is correct |
14 |
Correct |
10 ms |
20812 KB |
Output is correct |
15 |
Correct |
9 ms |
20820 KB |
Output is correct |
16 |
Correct |
9 ms |
20832 KB |
Output is correct |
17 |
Correct |
10 ms |
20824 KB |
Output is correct |
18 |
Correct |
10 ms |
20816 KB |
Output is correct |
19 |
Correct |
12 ms |
20956 KB |
Output is correct |
20 |
Correct |
9 ms |
20948 KB |
Output is correct |
21 |
Correct |
9 ms |
20824 KB |
Output is correct |
22 |
Correct |
8 ms |
20820 KB |
Output is correct |
23 |
Correct |
9 ms |
20772 KB |
Output is correct |
24 |
Correct |
9 ms |
20864 KB |
Output is correct |
25 |
Correct |
10 ms |
20820 KB |
Output is correct |
26 |
Correct |
9 ms |
20840 KB |
Output is correct |
27 |
Correct |
9 ms |
20832 KB |
Output is correct |
28 |
Correct |
9 ms |
20888 KB |
Output is correct |
29 |
Correct |
10 ms |
20948 KB |
Output is correct |
30 |
Correct |
9 ms |
20820 KB |
Output is correct |
31 |
Correct |
53 ms |
22800 KB |
Output is correct |
32 |
Correct |
44 ms |
22704 KB |
Output is correct |
33 |
Correct |
53 ms |
22936 KB |
Output is correct |
34 |
Correct |
40 ms |
23020 KB |
Output is correct |
35 |
Correct |
42 ms |
23092 KB |
Output is correct |
36 |
Correct |
42 ms |
23060 KB |
Output is correct |
37 |
Correct |
45 ms |
22732 KB |
Output is correct |
38 |
Correct |
9 ms |
20820 KB |
Output is correct |
39 |
Correct |
9 ms |
20776 KB |
Output is correct |
40 |
Correct |
9 ms |
20820 KB |
Output is correct |
41 |
Correct |
12 ms |
20820 KB |
Output is correct |
42 |
Correct |
10 ms |
20820 KB |
Output is correct |
43 |
Correct |
10 ms |
20776 KB |
Output is correct |
44 |
Correct |
9 ms |
20960 KB |
Output is correct |
45 |
Correct |
12 ms |
20852 KB |
Output is correct |
46 |
Correct |
16 ms |
20820 KB |
Output is correct |
47 |
Correct |
163 ms |
29892 KB |
Output is correct |
48 |
Correct |
140 ms |
29128 KB |
Output is correct |
49 |
Correct |
163 ms |
29828 KB |
Output is correct |
50 |
Correct |
171 ms |
31700 KB |
Output is correct |
51 |
Correct |
232 ms |
34604 KB |
Output is correct |
52 |
Correct |
212 ms |
34600 KB |
Output is correct |
53 |
Correct |
56 ms |
24512 KB |
Output is correct |
54 |
Correct |
231 ms |
31040 KB |
Output is correct |
55 |
Correct |
203 ms |
31644 KB |
Output is correct |
56 |
Correct |
205 ms |
31552 KB |
Output is correct |
57 |
Correct |
222 ms |
36864 KB |
Output is correct |
58 |
Correct |
207 ms |
31932 KB |
Output is correct |
59 |
Correct |
277 ms |
36412 KB |
Output is correct |
60 |
Correct |
249 ms |
36472 KB |
Output is correct |
61 |
Correct |
240 ms |
36612 KB |
Output is correct |
62 |
Correct |
209 ms |
34600 KB |
Output is correct |
63 |
Correct |
239 ms |
36048 KB |
Output is correct |
64 |
Correct |
232 ms |
35860 KB |
Output is correct |
65 |
Correct |
253 ms |
36080 KB |
Output is correct |
66 |
Correct |
9 ms |
20816 KB |
Output is correct |
67 |
Correct |
179 ms |
31604 KB |
Output is correct |
68 |
Correct |
192 ms |
31616 KB |
Output is correct |
69 |
Correct |
203 ms |
31568 KB |
Output is correct |
70 |
Correct |
193 ms |
32324 KB |
Output is correct |
71 |
Correct |
196 ms |
32536 KB |
Output is correct |
72 |
Correct |
189 ms |
32716 KB |
Output is correct |
73 |
Correct |
200 ms |
36924 KB |
Output is correct |
74 |
Correct |
201 ms |
36944 KB |
Output is correct |
75 |
Correct |
189 ms |
31992 KB |
Output is correct |
76 |
Correct |
202 ms |
33860 KB |
Output is correct |
77 |
Correct |
116 ms |
31404 KB |
Output is correct |
78 |
Correct |
9 ms |
20756 KB |
Output is correct |
79 |
Correct |
11 ms |
20872 KB |
Output is correct |
80 |
Correct |
9 ms |
20820 KB |
Output is correct |
81 |
Correct |
9 ms |
20820 KB |
Output is correct |
82 |
Correct |
9 ms |
20876 KB |
Output is correct |
83 |
Correct |
9 ms |
20948 KB |
Output is correct |
84 |
Correct |
9 ms |
20952 KB |
Output is correct |
85 |
Correct |
9 ms |
20828 KB |
Output is correct |
86 |
Correct |
49 ms |
22636 KB |
Output is correct |
87 |
Correct |
45 ms |
22600 KB |
Output is correct |
88 |
Correct |
45 ms |
22904 KB |
Output is correct |
89 |
Correct |
38 ms |
22992 KB |
Output is correct |
90 |
Correct |
44 ms |
23036 KB |
Output is correct |
91 |
Correct |
40 ms |
23004 KB |
Output is correct |
92 |
Correct |
44 ms |
22668 KB |
Output is correct |
93 |
Correct |
153 ms |
30016 KB |
Output is correct |
94 |
Correct |
140 ms |
29088 KB |
Output is correct |
95 |
Correct |
146 ms |
29744 KB |
Output is correct |
96 |
Correct |
163 ms |
31564 KB |
Output is correct |
97 |
Correct |
217 ms |
34680 KB |
Output is correct |
98 |
Correct |
209 ms |
34528 KB |
Output is correct |
99 |
Correct |
54 ms |
24516 KB |
Output is correct |
100 |
Correct |
252 ms |
36544 KB |
Output is correct |
101 |
Correct |
240 ms |
36416 KB |
Output is correct |
102 |
Correct |
231 ms |
36604 KB |
Output is correct |
103 |
Correct |
245 ms |
36112 KB |
Output is correct |
104 |
Correct |
238 ms |
35916 KB |
Output is correct |
105 |
Correct |
238 ms |
36032 KB |
Output is correct |
106 |
Correct |
184 ms |
30036 KB |
Output is correct |
107 |
Correct |
206 ms |
31076 KB |
Output is correct |
108 |
Correct |
179 ms |
31740 KB |
Output is correct |
109 |
Correct |
180 ms |
31700 KB |
Output is correct |
110 |
Correct |
241 ms |
36292 KB |
Output is correct |
111 |
Correct |
250 ms |
36548 KB |
Output is correct |