#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1e5+5, Log=17, pot=(1<<Log), inf=1e9;
struct tournament{
int t[pot*2];
tournament(){
for(int i=1; i<pot*2; i++){
t[i]=inf;
}
}
void update(int x, int val){
t[x]=min(t[x], val);
for(x/=2; x>0; x/=2){
t[x]=min(t[x*2], t[x*2+1]);
}
}
int query(int L, int D, int x, int l, int d){
if(L>=l && D<=d){
return t[x];
}
int lijeva=inf, desna=inf;
if((L+D)/2>=l){
lijeva=query(L, (L+D)/2, x*2, l, d);
}
if((L+D)/2+1<=d){
desna=query((L+D)/2+1, D, x*2+1, l, d);
}
return min(lijeva, desna);
}
int nadji(int L, int D, int x, int l, int d, int val){
if(L>=l && D<=d){
if(t[x]==val){
while(x<pot){
if(t[x*2]==val){
x*=2;
}
else{
x=x*2+1;
}
}
return x;
}
return -1;
}
int xx=-1;
if((L+D)/2>=l){
xx=nadji(L, (L+D)/2, x*2, l, d, val);
if(xx!=-1){
return xx;
}
}
if((L+D)/2+1<=d){
xx=nadji((L+D)/2+1, D, x*2+1, l, d, val);
if(xx!=-1){
return xx;
}
}
return -1;
}
};
tournament T;
pair < int, int > a[maxn];
set < int > s;
map < int, int > m;
int digni[maxn][Log];
int moj[maxn];
void precompute(){
for(int i=1; i<Log; i++){
for(int j=0; j<maxn; j++){
digni[j][i]=j;
}
}
for(int i=1; i<Log; i++){
for(int j=0; j<maxn; j++){
digni[j][i]=digni[digni[j][i-1]][i-1];
}
}
}
int provjeri(int x, int y){
if(x==y){
return 0;
}
if(a[y].first<=a[x].second && a[x].second<=a[y].second){
return 1;
}
if(a[x].second>a[y].second){
return -1;
}
int uk=1;
x=a[x].second;
y=moj[y];
if(T.t[y+pot]<=x){
return 2;
}
// cout << "aha " << x << ' ' << y << endl;
// cout << T.t[digni[y][0]+pot] << endl;
for(int i=Log-1; i>-1; i--){
if(T.t[digni[y][i]+pot]>x){
// cout << "dizem " << y << " na " << digni[y][i] << endl;
y=digni[y][i];
uk+=(1<<i);
}
}
if(uk>maxn){
return -1;
}
return uk+2;
}
int main(){
int n, q;
scanf("%d%d", &n, &q);
for(int i=1; i<=n; i++){
scanf("%d%d", &a[i].first, &a[i].second);
s.insert(a[i].second);
}
int br=0;
for(auto it=s.begin(); it!=s.end(); it++){
m[*it]=br;
br++;
}
set < int >::iterator it;
for(int i=1; i<=n; i++){
it=s.lower_bound(a[i].first);
a[i]={m[*it], m[a[i].second]};
// cout << a[i].first << ' ' << a[i].second << endl;
T.update(a[i].second+pot, a[i].first);
}
int val, pos;
for(int i=1; i<=n; i++){
val=T.query(0, pot-1, 1, a[i].first, a[i].second);
// cout << "vrijednost " << a[i].second << " je " << val << endl;
if(val==a[i].first){
moj[i]=a[i].second;
continue;
}
pos=T.nadji(0, pot-1, 1, a[i].first, a[i].second, val);
moj[i]=pos-pot;
}
int gran;
for(int i=0; i<br; i++){
gran=T.t[i+pot];
val=T.query(0, pot-1, 1, gran, i);
if(val==gran){
digni[i][0]=i;
continue;
}
pos=T.nadji(0, pot-1, 1, gran, i, val);
digni[i][0]=pos-pot;
}
precompute();
/* cout << "ovo sranje" << endl;
for(int i=1; i<=n; i++){
cout << moj[i] << ' ';
}
cout << endl;
for(int i=0; i<br; i++){
cout << T.t[i+pot] << ' ';
}
cout << endl;
for(int i=0; i<br; i++){
cout << digni[i][0] << ' ';
}
cout << endl;
for(int i=0; i<br; i++){
for(int j=0; j<3; j++){
cout << digni[i][j] << ' ';
}
cout << endl;
}*/
int x, y, z;
for(int i=0; i<q; i++){
scanf("%d%d", &x, &y);
z=provjeri(x, y);
if(z==-1){
printf("impossible\n");
}
else{
printf("%d\n", z);
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:129:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
events.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d%d", &a[i].first, &a[i].second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:191:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
191 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
7892 KB |
Output is correct |
2 |
Correct |
290 ms |
19168 KB |
Output is correct |
3 |
Correct |
284 ms |
19036 KB |
Output is correct |
4 |
Correct |
298 ms |
19096 KB |
Output is correct |
5 |
Correct |
262 ms |
19152 KB |
Output is correct |
6 |
Correct |
251 ms |
19236 KB |
Output is correct |
7 |
Correct |
308 ms |
19240 KB |
Output is correct |
8 |
Correct |
222 ms |
19548 KB |
Output is correct |
9 |
Correct |
227 ms |
19532 KB |
Output is correct |
10 |
Correct |
273 ms |
19424 KB |
Output is correct |
11 |
Correct |
240 ms |
19476 KB |
Output is correct |
12 |
Correct |
109 ms |
14900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7892 KB |
Output is correct |
2 |
Correct |
11 ms |
7892 KB |
Output is correct |
3 |
Correct |
12 ms |
8088 KB |
Output is correct |
4 |
Correct |
15 ms |
8148 KB |
Output is correct |
5 |
Correct |
14 ms |
8096 KB |
Output is correct |
6 |
Correct |
12 ms |
8044 KB |
Output is correct |
7 |
Correct |
13 ms |
8088 KB |
Output is correct |
8 |
Correct |
11 ms |
8020 KB |
Output is correct |
9 |
Correct |
14 ms |
7996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7892 KB |
Output is correct |
2 |
Correct |
11 ms |
7892 KB |
Output is correct |
3 |
Correct |
12 ms |
8088 KB |
Output is correct |
4 |
Correct |
15 ms |
8148 KB |
Output is correct |
5 |
Correct |
14 ms |
8096 KB |
Output is correct |
6 |
Correct |
12 ms |
8044 KB |
Output is correct |
7 |
Correct |
13 ms |
8088 KB |
Output is correct |
8 |
Correct |
11 ms |
8020 KB |
Output is correct |
9 |
Correct |
14 ms |
7996 KB |
Output is correct |
10 |
Correct |
11 ms |
7892 KB |
Output is correct |
11 |
Correct |
12 ms |
7988 KB |
Output is correct |
12 |
Correct |
14 ms |
8092 KB |
Output is correct |
13 |
Correct |
12 ms |
8092 KB |
Output is correct |
14 |
Correct |
13 ms |
8024 KB |
Output is correct |
15 |
Correct |
18 ms |
8044 KB |
Output is correct |
16 |
Correct |
12 ms |
8092 KB |
Output is correct |
17 |
Correct |
12 ms |
8020 KB |
Output is correct |
18 |
Correct |
13 ms |
8000 KB |
Output is correct |
19 |
Correct |
47 ms |
8960 KB |
Output is correct |
20 |
Correct |
46 ms |
8896 KB |
Output is correct |
21 |
Correct |
51 ms |
9256 KB |
Output is correct |
22 |
Correct |
39 ms |
9040 KB |
Output is correct |
23 |
Correct |
53 ms |
9072 KB |
Output is correct |
24 |
Correct |
41 ms |
9044 KB |
Output is correct |
25 |
Correct |
48 ms |
8772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7892 KB |
Output is correct |
2 |
Correct |
11 ms |
7892 KB |
Output is correct |
3 |
Correct |
12 ms |
8088 KB |
Output is correct |
4 |
Correct |
15 ms |
8148 KB |
Output is correct |
5 |
Correct |
14 ms |
8096 KB |
Output is correct |
6 |
Correct |
12 ms |
8044 KB |
Output is correct |
7 |
Correct |
13 ms |
8088 KB |
Output is correct |
8 |
Correct |
11 ms |
8020 KB |
Output is correct |
9 |
Correct |
14 ms |
7996 KB |
Output is correct |
10 |
Correct |
15 ms |
7892 KB |
Output is correct |
11 |
Correct |
13 ms |
7988 KB |
Output is correct |
12 |
Correct |
11 ms |
8092 KB |
Output is correct |
13 |
Correct |
14 ms |
8088 KB |
Output is correct |
14 |
Correct |
11 ms |
8020 KB |
Output is correct |
15 |
Correct |
11 ms |
8020 KB |
Output is correct |
16 |
Correct |
16 ms |
8020 KB |
Output is correct |
17 |
Correct |
16 ms |
8020 KB |
Output is correct |
18 |
Correct |
11 ms |
7892 KB |
Output is correct |
19 |
Correct |
243 ms |
18524 KB |
Output is correct |
20 |
Correct |
241 ms |
18432 KB |
Output is correct |
21 |
Correct |
181 ms |
13832 KB |
Output is correct |
22 |
Correct |
232 ms |
18436 KB |
Output is correct |
23 |
Correct |
156 ms |
18532 KB |
Output is correct |
24 |
Correct |
303 ms |
18524 KB |
Output is correct |
25 |
Correct |
35 ms |
9132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
19048 KB |
Output is correct |
2 |
Correct |
305 ms |
19020 KB |
Output is correct |
3 |
Correct |
273 ms |
19036 KB |
Output is correct |
4 |
Correct |
287 ms |
19544 KB |
Output is correct |
5 |
Correct |
310 ms |
19432 KB |
Output is correct |
6 |
Correct |
369 ms |
19160 KB |
Output is correct |
7 |
Correct |
306 ms |
19168 KB |
Output is correct |
8 |
Correct |
336 ms |
19420 KB |
Output is correct |
9 |
Correct |
144 ms |
18524 KB |
Output is correct |
10 |
Correct |
309 ms |
18952 KB |
Output is correct |
11 |
Correct |
258 ms |
18636 KB |
Output is correct |
12 |
Correct |
305 ms |
18816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
7892 KB |
Output is correct |
2 |
Correct |
290 ms |
19168 KB |
Output is correct |
3 |
Correct |
284 ms |
19036 KB |
Output is correct |
4 |
Correct |
298 ms |
19096 KB |
Output is correct |
5 |
Correct |
262 ms |
19152 KB |
Output is correct |
6 |
Correct |
251 ms |
19236 KB |
Output is correct |
7 |
Correct |
308 ms |
19240 KB |
Output is correct |
8 |
Correct |
222 ms |
19548 KB |
Output is correct |
9 |
Correct |
227 ms |
19532 KB |
Output is correct |
10 |
Correct |
273 ms |
19424 KB |
Output is correct |
11 |
Correct |
240 ms |
19476 KB |
Output is correct |
12 |
Correct |
109 ms |
14900 KB |
Output is correct |
13 |
Correct |
13 ms |
7892 KB |
Output is correct |
14 |
Correct |
11 ms |
7892 KB |
Output is correct |
15 |
Correct |
12 ms |
8088 KB |
Output is correct |
16 |
Correct |
15 ms |
8148 KB |
Output is correct |
17 |
Correct |
14 ms |
8096 KB |
Output is correct |
18 |
Correct |
12 ms |
8044 KB |
Output is correct |
19 |
Correct |
13 ms |
8088 KB |
Output is correct |
20 |
Correct |
11 ms |
8020 KB |
Output is correct |
21 |
Correct |
14 ms |
7996 KB |
Output is correct |
22 |
Correct |
11 ms |
7892 KB |
Output is correct |
23 |
Correct |
12 ms |
7988 KB |
Output is correct |
24 |
Correct |
14 ms |
8092 KB |
Output is correct |
25 |
Correct |
12 ms |
8092 KB |
Output is correct |
26 |
Correct |
13 ms |
8024 KB |
Output is correct |
27 |
Correct |
18 ms |
8044 KB |
Output is correct |
28 |
Correct |
12 ms |
8092 KB |
Output is correct |
29 |
Correct |
12 ms |
8020 KB |
Output is correct |
30 |
Correct |
13 ms |
8000 KB |
Output is correct |
31 |
Correct |
47 ms |
8960 KB |
Output is correct |
32 |
Correct |
46 ms |
8896 KB |
Output is correct |
33 |
Correct |
51 ms |
9256 KB |
Output is correct |
34 |
Correct |
39 ms |
9040 KB |
Output is correct |
35 |
Correct |
53 ms |
9072 KB |
Output is correct |
36 |
Correct |
41 ms |
9044 KB |
Output is correct |
37 |
Correct |
48 ms |
8772 KB |
Output is correct |
38 |
Correct |
15 ms |
7892 KB |
Output is correct |
39 |
Correct |
13 ms |
7988 KB |
Output is correct |
40 |
Correct |
11 ms |
8092 KB |
Output is correct |
41 |
Correct |
14 ms |
8088 KB |
Output is correct |
42 |
Correct |
11 ms |
8020 KB |
Output is correct |
43 |
Correct |
11 ms |
8020 KB |
Output is correct |
44 |
Correct |
16 ms |
8020 KB |
Output is correct |
45 |
Correct |
16 ms |
8020 KB |
Output is correct |
46 |
Correct |
11 ms |
7892 KB |
Output is correct |
47 |
Correct |
243 ms |
18524 KB |
Output is correct |
48 |
Correct |
241 ms |
18432 KB |
Output is correct |
49 |
Correct |
181 ms |
13832 KB |
Output is correct |
50 |
Correct |
232 ms |
18436 KB |
Output is correct |
51 |
Correct |
156 ms |
18532 KB |
Output is correct |
52 |
Correct |
303 ms |
18524 KB |
Output is correct |
53 |
Correct |
35 ms |
9132 KB |
Output is correct |
54 |
Correct |
320 ms |
19048 KB |
Output is correct |
55 |
Correct |
305 ms |
19020 KB |
Output is correct |
56 |
Correct |
273 ms |
19036 KB |
Output is correct |
57 |
Correct |
287 ms |
19544 KB |
Output is correct |
58 |
Correct |
310 ms |
19432 KB |
Output is correct |
59 |
Correct |
369 ms |
19160 KB |
Output is correct |
60 |
Correct |
306 ms |
19168 KB |
Output is correct |
61 |
Correct |
336 ms |
19420 KB |
Output is correct |
62 |
Correct |
144 ms |
18524 KB |
Output is correct |
63 |
Correct |
309 ms |
18952 KB |
Output is correct |
64 |
Correct |
258 ms |
18636 KB |
Output is correct |
65 |
Correct |
305 ms |
18816 KB |
Output is correct |
66 |
Correct |
10 ms |
7892 KB |
Output is correct |
67 |
Correct |
275 ms |
19020 KB |
Output is correct |
68 |
Correct |
297 ms |
19048 KB |
Output is correct |
69 |
Correct |
261 ms |
19080 KB |
Output is correct |
70 |
Correct |
263 ms |
19152 KB |
Output is correct |
71 |
Correct |
288 ms |
19288 KB |
Output is correct |
72 |
Correct |
254 ms |
19168 KB |
Output is correct |
73 |
Correct |
238 ms |
19612 KB |
Output is correct |
74 |
Correct |
225 ms |
19604 KB |
Output is correct |
75 |
Correct |
286 ms |
19420 KB |
Output is correct |
76 |
Correct |
280 ms |
19452 KB |
Output is correct |
77 |
Correct |
116 ms |
14900 KB |
Output is correct |
78 |
Correct |
10 ms |
7988 KB |
Output is correct |
79 |
Correct |
14 ms |
8088 KB |
Output is correct |
80 |
Correct |
18 ms |
8020 KB |
Output is correct |
81 |
Correct |
13 ms |
8020 KB |
Output is correct |
82 |
Correct |
17 ms |
8020 KB |
Output is correct |
83 |
Correct |
15 ms |
8092 KB |
Output is correct |
84 |
Correct |
11 ms |
8084 KB |
Output is correct |
85 |
Correct |
11 ms |
8000 KB |
Output is correct |
86 |
Correct |
51 ms |
8932 KB |
Output is correct |
87 |
Correct |
49 ms |
8988 KB |
Output is correct |
88 |
Correct |
54 ms |
9188 KB |
Output is correct |
89 |
Correct |
51 ms |
9048 KB |
Output is correct |
90 |
Correct |
40 ms |
9120 KB |
Output is correct |
91 |
Correct |
37 ms |
9132 KB |
Output is correct |
92 |
Correct |
49 ms |
8716 KB |
Output is correct |
93 |
Correct |
286 ms |
18528 KB |
Output is correct |
94 |
Correct |
230 ms |
18552 KB |
Output is correct |
95 |
Correct |
184 ms |
13804 KB |
Output is correct |
96 |
Correct |
272 ms |
18508 KB |
Output is correct |
97 |
Correct |
153 ms |
18476 KB |
Output is correct |
98 |
Correct |
266 ms |
18524 KB |
Output is correct |
99 |
Correct |
56 ms |
9068 KB |
Output is correct |
100 |
Correct |
315 ms |
19268 KB |
Output is correct |
101 |
Correct |
304 ms |
19220 KB |
Output is correct |
102 |
Correct |
300 ms |
19292 KB |
Output is correct |
103 |
Correct |
320 ms |
18816 KB |
Output is correct |
104 |
Correct |
262 ms |
18632 KB |
Output is correct |
105 |
Correct |
298 ms |
18908 KB |
Output is correct |
106 |
Correct |
224 ms |
16896 KB |
Output is correct |
107 |
Correct |
287 ms |
19256 KB |
Output is correct |
108 |
Correct |
247 ms |
14652 KB |
Output is correct |
109 |
Correct |
179 ms |
14596 KB |
Output is correct |
110 |
Correct |
306 ms |
19272 KB |
Output is correct |
111 |
Correct |
297 ms |
19168 KB |
Output is correct |