#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct segTree{
pair<int, int> tree[800002];
void init(int i, int l, int r){
tree[i] = make_pair(2e9, 2e9);
if(l==r) return;
int m = (l+r)>>1;
init(i*2, l, m);
init(i*2+1, m+1, r);
}
void update(int i, int l, int r, int x, pair<int, int> v){
if(l==r){
tree[i] = min(tree[i], v);
return;
}
int m = (l+r)>>1;
if(x<=m) update(i*2, l, m, x, v);
else update(i*2+1, m+1, r, x, v);
tree[i] = min(tree[i*2], tree[i*2+1]);
}
pair<int, int> query(int i, int l, int r, int s, int e){
if(r<s || e<l) return make_pair(2e9, 2e9);
if(s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return min(query(i*2, l, m, s, e), query(i*2+1, m+1, r, s, e));
}
} tree;
struct Range{
int l, r, idx;
Range(){}
Range(int l, int r, int idx): l(l), r(r), idx(idx){}
bool operator<(const Range &nxt)const{
return r<nxt.r;
}
};
struct dat{
int x, v, idx;
dat(){}
dat(int x, int v, int idx): x(x), v(v), idx(idx){}
bool operator<(const dat &r)const{
if(x!=r.x) return x<r.x;
return v>r.v;
}
};
int n, q;
Range arr[100002];
void input();
void process();
void output();
int main(){
input();
process();
output();
}
void input(){
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++){
scanf("%d %d", &arr[i].l, &arr[i].r);
arr[i].idx = i;
}
vector<int> vec;
for(int i=1; i<=n; i++) vec.push_back(arr[i].l), vec.push_back(arr[i].r);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for(int i=1; i<=n; i++){
arr[i].l = lower_bound(vec.begin(), vec.end(), arr[i].l) - vec.begin() + 1;
arr[i].r = lower_bound(vec.begin(), vec.end(), arr[i].r) - vec.begin() + 1;
}
}
int nxt[100002];
int sps[100002][20];
int idx[100002];
void process(){
sort(arr+1, arr+n+1);
for(int i=1; i<=n; i++) idx[arr[i].idx] = i;
tree.init(1, 1, n+n);
for(int i=1; i<=n; i++) tree.update(1, 1, n+n, arr[i].r, make_pair(arr[i].l, i));
for(int i=1; i<=n; i++) nxt[i] = tree.query(1, 1, n+n, arr[i].l, arr[i].r).second;
for(int i=1; i<=n; i++) sps[i][0] = nxt[i];
for(int d=1; d<20; d++) for(int i=1; i<=n; i++) sps[i][d] = sps[sps[i][d-1]][d-1];
}
void output(){
while(q--){
int l, r;
scanf("%d %d", &l, &r);
l = idx[l], r = idx[r];
if(l==r) puts("0");
else if(arr[r].r < arr[l].r) puts("impossible");
else{
int ans = 0;
for(int d=19; d>=0; d--){
if(arr[sps[r][d]].l > arr[l].r) ans += (1<<d), r = sps[r][d];
}
if(ans > n) puts("impossible");
else printf("%d\n", ans+(arr[r].l <= arr[l].r && arr[l].r <= arr[r].r ? 1 : 2));
}
}
}
Compilation message
events.cpp: In function 'void input()':
events.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
events.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d %d", &arr[i].l, &arr[i].r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp: In function 'void output()':
events.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
165 ms |
14776 KB |
Output is correct |
3 |
Correct |
143 ms |
14880 KB |
Output is correct |
4 |
Correct |
149 ms |
14796 KB |
Output is correct |
5 |
Correct |
149 ms |
14848 KB |
Output is correct |
6 |
Correct |
146 ms |
14924 KB |
Output is correct |
7 |
Correct |
150 ms |
14900 KB |
Output is correct |
8 |
Correct |
135 ms |
15396 KB |
Output is correct |
9 |
Correct |
138 ms |
15304 KB |
Output is correct |
10 |
Correct |
165 ms |
15180 KB |
Output is correct |
11 |
Correct |
146 ms |
15264 KB |
Output is correct |
12 |
Correct |
100 ms |
15384 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 |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
43 ms |
1624 KB |
Output is correct |
20 |
Correct |
39 ms |
1600 KB |
Output is correct |
21 |
Correct |
38 ms |
1876 KB |
Output is correct |
22 |
Correct |
36 ms |
1964 KB |
Output is correct |
23 |
Correct |
37 ms |
1752 KB |
Output is correct |
24 |
Correct |
35 ms |
1744 KB |
Output is correct |
25 |
Correct |
38 ms |
1348 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 |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
98 ms |
14216 KB |
Output is correct |
20 |
Correct |
104 ms |
14612 KB |
Output is correct |
21 |
Correct |
125 ms |
14656 KB |
Output is correct |
22 |
Correct |
114 ms |
14588 KB |
Output is correct |
23 |
Correct |
113 ms |
14764 KB |
Output is correct |
24 |
Correct |
113 ms |
15152 KB |
Output is correct |
25 |
Correct |
57 ms |
14368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
14856 KB |
Output is correct |
2 |
Correct |
134 ms |
14920 KB |
Output is correct |
3 |
Correct |
139 ms |
14856 KB |
Output is correct |
4 |
Correct |
147 ms |
15436 KB |
Output is correct |
5 |
Correct |
173 ms |
15248 KB |
Output is correct |
6 |
Correct |
181 ms |
15012 KB |
Output is correct |
7 |
Correct |
218 ms |
14924 KB |
Output is correct |
8 |
Correct |
175 ms |
15108 KB |
Output is correct |
9 |
Correct |
111 ms |
14260 KB |
Output is correct |
10 |
Correct |
156 ms |
14664 KB |
Output is correct |
11 |
Correct |
156 ms |
14388 KB |
Output is correct |
12 |
Correct |
164 ms |
14632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
165 ms |
14776 KB |
Output is correct |
3 |
Correct |
143 ms |
14880 KB |
Output is correct |
4 |
Correct |
149 ms |
14796 KB |
Output is correct |
5 |
Correct |
149 ms |
14848 KB |
Output is correct |
6 |
Correct |
146 ms |
14924 KB |
Output is correct |
7 |
Correct |
150 ms |
14900 KB |
Output is correct |
8 |
Correct |
135 ms |
15396 KB |
Output is correct |
9 |
Correct |
138 ms |
15304 KB |
Output is correct |
10 |
Correct |
165 ms |
15180 KB |
Output is correct |
11 |
Correct |
146 ms |
15264 KB |
Output is correct |
12 |
Correct |
100 ms |
15384 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
43 ms |
1624 KB |
Output is correct |
32 |
Correct |
39 ms |
1600 KB |
Output is correct |
33 |
Correct |
38 ms |
1876 KB |
Output is correct |
34 |
Correct |
36 ms |
1964 KB |
Output is correct |
35 |
Correct |
37 ms |
1752 KB |
Output is correct |
36 |
Correct |
35 ms |
1744 KB |
Output is correct |
37 |
Correct |
38 ms |
1348 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
98 ms |
14216 KB |
Output is correct |
48 |
Correct |
104 ms |
14612 KB |
Output is correct |
49 |
Correct |
125 ms |
14656 KB |
Output is correct |
50 |
Correct |
114 ms |
14588 KB |
Output is correct |
51 |
Correct |
113 ms |
14764 KB |
Output is correct |
52 |
Correct |
113 ms |
15152 KB |
Output is correct |
53 |
Correct |
57 ms |
14368 KB |
Output is correct |
54 |
Correct |
163 ms |
14856 KB |
Output is correct |
55 |
Correct |
134 ms |
14920 KB |
Output is correct |
56 |
Correct |
139 ms |
14856 KB |
Output is correct |
57 |
Correct |
147 ms |
15436 KB |
Output is correct |
58 |
Correct |
173 ms |
15248 KB |
Output is correct |
59 |
Correct |
181 ms |
15012 KB |
Output is correct |
60 |
Correct |
218 ms |
14924 KB |
Output is correct |
61 |
Correct |
175 ms |
15108 KB |
Output is correct |
62 |
Correct |
111 ms |
14260 KB |
Output is correct |
63 |
Correct |
156 ms |
14664 KB |
Output is correct |
64 |
Correct |
156 ms |
14388 KB |
Output is correct |
65 |
Correct |
164 ms |
14632 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
136 ms |
15164 KB |
Output is correct |
68 |
Correct |
153 ms |
15484 KB |
Output is correct |
69 |
Correct |
153 ms |
15160 KB |
Output is correct |
70 |
Correct |
152 ms |
15680 KB |
Output is correct |
71 |
Correct |
161 ms |
15280 KB |
Output is correct |
72 |
Correct |
160 ms |
15864 KB |
Output is correct |
73 |
Correct |
137 ms |
15704 KB |
Output is correct |
74 |
Correct |
154 ms |
15696 KB |
Output is correct |
75 |
Correct |
161 ms |
15612 KB |
Output is correct |
76 |
Correct |
152 ms |
15520 KB |
Output is correct |
77 |
Correct |
113 ms |
15544 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
476 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
468 KB |
Output is correct |
84 |
Correct |
2 ms |
464 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
52 ms |
1768 KB |
Output is correct |
87 |
Correct |
49 ms |
1724 KB |
Output is correct |
88 |
Correct |
46 ms |
1880 KB |
Output is correct |
89 |
Correct |
36 ms |
2136 KB |
Output is correct |
90 |
Correct |
35 ms |
1904 KB |
Output is correct |
91 |
Correct |
33 ms |
1900 KB |
Output is correct |
92 |
Correct |
39 ms |
1428 KB |
Output is correct |
93 |
Correct |
103 ms |
14388 KB |
Output is correct |
94 |
Correct |
106 ms |
14368 KB |
Output is correct |
95 |
Correct |
111 ms |
14348 KB |
Output is correct |
96 |
Correct |
117 ms |
14432 KB |
Output is correct |
97 |
Correct |
121 ms |
14760 KB |
Output is correct |
98 |
Correct |
130 ms |
14632 KB |
Output is correct |
99 |
Correct |
73 ms |
14432 KB |
Output is correct |
100 |
Correct |
180 ms |
15336 KB |
Output is correct |
101 |
Correct |
205 ms |
15656 KB |
Output is correct |
102 |
Correct |
183 ms |
16752 KB |
Output is correct |
103 |
Correct |
151 ms |
16544 KB |
Output is correct |
104 |
Correct |
148 ms |
15724 KB |
Output is correct |
105 |
Correct |
175 ms |
16800 KB |
Output is correct |
106 |
Correct |
156 ms |
17064 KB |
Output is correct |
107 |
Correct |
178 ms |
17332 KB |
Output is correct |
108 |
Correct |
158 ms |
17004 KB |
Output is correct |
109 |
Correct |
146 ms |
16408 KB |
Output is correct |
110 |
Correct |
179 ms |
15628 KB |
Output is correct |
111 |
Correct |
180 ms |
15320 KB |
Output is correct |