#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
const int MAXN = 1e5+1;
int s[MAXN],e[MAXN];
//fuck binlift how
//p(k,i) = min s if we jump backwards 2^k times
//p(k-1,i) = min of p(k-1),i<= x <= i ,p(k-1,x)
//yeesh but this n log^2n
//should pass maybe?
//1e5 * 18 * 18 = 3e7
int p[19][MAXN];
typedef pair<int,int> pii;
#define fi first
#define se second
pii T[8*MAXN];
pii mn[2*MAXN];
void build(int v,int l,int r){
if(l==r){
T[v] = mn[l];
return;
}
int tm = (l+r)/2;
build(2*v,l,tm);
build(2*v+1,tm+1,r);
T[v] = min(T[2*v],T[2*v+1]);
}
pii query(int v,int l,int r,int tl,int tr){
if(l>r)return {1e9,-1};
if(l==tl && r==tr)return T[v];
int tm = (tl+tr)/2;
return min(query(2*v,l,min(r,tm),tl,tm),
query(2*v+1,max(l,tm+1),r,tm+1,tr));
}
int main()
{
owo
int n,q;
cin>>n>>q;
vector<int>crd;
for(int i=0;i<n;i++)cin>>s[i]>>e[i];
for(int i=0;i<n;i++){
crd.push_back(s[i]);
crd.push_back(e[i]);
}
sort(all(crd));
crd.resize(unique(all(crd)) - crd.begin());
for(int i=0;i<n;i++){
s[i] = lower_bound(all(crd),s[i]) - crd.begin()+1;
e[i] = lower_bound(all(crd),e[i]) - crd.begin()+1;
}
int m = crd.size();
for(int i=1;i<=m;i++)mn[i] = {i,-1};
for(int i=0;i<n;i++)mn[e[i]] = min(mn[e[i]],{s[i],i});
build(1,1,m);
for(int i=0;i<n;i++){
p[0][i] = i;
pii res = query(1,s[i],e[i],1,m);
if(res.se != -1)p[0][i] = res.se;
}
for(int i=1;i<=18;i++){
for(int j=0;j<n;j++){
p[i][j] = p[i-1][p[i-1][j]];
}
}
while(q--){
int l,r;
cin>>l>>r;
l--;
r--;
if(l==r){
cout<<0<<'\n';
continue;
}
if(e[l] > e[r]){
cout<<"impossible"<<'\n';
continue;
}
if(s[r] <= e[l]){
cout<<1<<'\n';
continue;
}
int ans = 1; //one for actually arrivinf at l
/*
while(s[r] > e[l]){
if(p[0][r] == r){
ans = -1;
break;
}
ans++;
r = p[0][r];
}*/
for(int i=18;i>=0;i--){
int v = p[i][r];
if(s[v] > e[l]){
r = v;
ans+=(1<<i);
}
}
ans++;
if(r == p[0][r])ans=-1;
if(ans==-1)cout<<"impossible"<<'\n';
else cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
124 ms |
13796 KB |
Output is correct |
3 |
Correct |
115 ms |
15960 KB |
Output is correct |
4 |
Correct |
161 ms |
15808 KB |
Output is correct |
5 |
Correct |
136 ms |
16056 KB |
Output is correct |
6 |
Correct |
137 ms |
16192 KB |
Output is correct |
7 |
Correct |
124 ms |
16188 KB |
Output is correct |
8 |
Correct |
136 ms |
19280 KB |
Output is correct |
9 |
Correct |
140 ms |
19212 KB |
Output is correct |
10 |
Correct |
130 ms |
16396 KB |
Output is correct |
11 |
Correct |
154 ms |
18716 KB |
Output is correct |
12 |
Correct |
76 ms |
15648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 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 |
1 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 |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 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 |
1 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 |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 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 |
30 ms |
1540 KB |
Output is correct |
20 |
Correct |
37 ms |
1588 KB |
Output is correct |
21 |
Correct |
33 ms |
1772 KB |
Output is correct |
22 |
Correct |
30 ms |
1888 KB |
Output is correct |
23 |
Correct |
32 ms |
1800 KB |
Output is correct |
24 |
Correct |
24 ms |
1876 KB |
Output is correct |
25 |
Correct |
28 ms |
1444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 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 |
1 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 |
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 |
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 |
88 ms |
12256 KB |
Output is correct |
20 |
Correct |
93 ms |
13404 KB |
Output is correct |
21 |
Correct |
94 ms |
13960 KB |
Output is correct |
22 |
Correct |
81 ms |
16284 KB |
Output is correct |
23 |
Correct |
123 ms |
16896 KB |
Output is correct |
24 |
Correct |
111 ms |
16984 KB |
Output is correct |
25 |
Correct |
26 ms |
10608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
13892 KB |
Output is correct |
2 |
Correct |
119 ms |
15992 KB |
Output is correct |
3 |
Correct |
156 ms |
15764 KB |
Output is correct |
4 |
Correct |
152 ms |
19012 KB |
Output is correct |
5 |
Correct |
120 ms |
16212 KB |
Output is correct |
6 |
Correct |
135 ms |
18872 KB |
Output is correct |
7 |
Correct |
154 ms |
18808 KB |
Output is correct |
8 |
Correct |
143 ms |
19012 KB |
Output is correct |
9 |
Correct |
103 ms |
16876 KB |
Output is correct |
10 |
Correct |
125 ms |
18468 KB |
Output is correct |
11 |
Correct |
170 ms |
18324 KB |
Output is correct |
12 |
Correct |
147 ms |
18500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
124 ms |
13796 KB |
Output is correct |
3 |
Correct |
115 ms |
15960 KB |
Output is correct |
4 |
Correct |
161 ms |
15808 KB |
Output is correct |
5 |
Correct |
136 ms |
16056 KB |
Output is correct |
6 |
Correct |
137 ms |
16192 KB |
Output is correct |
7 |
Correct |
124 ms |
16188 KB |
Output is correct |
8 |
Correct |
136 ms |
19280 KB |
Output is correct |
9 |
Correct |
140 ms |
19212 KB |
Output is correct |
10 |
Correct |
130 ms |
16396 KB |
Output is correct |
11 |
Correct |
154 ms |
18716 KB |
Output is correct |
12 |
Correct |
76 ms |
15648 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 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 |
1 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 |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 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 |
30 ms |
1540 KB |
Output is correct |
32 |
Correct |
37 ms |
1588 KB |
Output is correct |
33 |
Correct |
33 ms |
1772 KB |
Output is correct |
34 |
Correct |
30 ms |
1888 KB |
Output is correct |
35 |
Correct |
32 ms |
1800 KB |
Output is correct |
36 |
Correct |
24 ms |
1876 KB |
Output is correct |
37 |
Correct |
28 ms |
1444 KB |
Output is correct |
38 |
Correct |
0 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 |
1 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 |
88 ms |
12256 KB |
Output is correct |
48 |
Correct |
93 ms |
13404 KB |
Output is correct |
49 |
Correct |
94 ms |
13960 KB |
Output is correct |
50 |
Correct |
81 ms |
16284 KB |
Output is correct |
51 |
Correct |
123 ms |
16896 KB |
Output is correct |
52 |
Correct |
111 ms |
16984 KB |
Output is correct |
53 |
Correct |
26 ms |
10608 KB |
Output is correct |
54 |
Correct |
102 ms |
13892 KB |
Output is correct |
55 |
Correct |
119 ms |
15992 KB |
Output is correct |
56 |
Correct |
156 ms |
15764 KB |
Output is correct |
57 |
Correct |
152 ms |
19012 KB |
Output is correct |
58 |
Correct |
120 ms |
16212 KB |
Output is correct |
59 |
Correct |
135 ms |
18872 KB |
Output is correct |
60 |
Correct |
154 ms |
18808 KB |
Output is correct |
61 |
Correct |
143 ms |
19012 KB |
Output is correct |
62 |
Correct |
103 ms |
16876 KB |
Output is correct |
63 |
Correct |
125 ms |
18468 KB |
Output is correct |
64 |
Correct |
170 ms |
18324 KB |
Output is correct |
65 |
Correct |
147 ms |
18500 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
109 ms |
15852 KB |
Output is correct |
68 |
Correct |
129 ms |
15936 KB |
Output is correct |
69 |
Correct |
160 ms |
15892 KB |
Output is correct |
70 |
Correct |
122 ms |
16068 KB |
Output is correct |
71 |
Correct |
134 ms |
16108 KB |
Output is correct |
72 |
Correct |
168 ms |
16276 KB |
Output is correct |
73 |
Correct |
144 ms |
19116 KB |
Output is correct |
74 |
Correct |
155 ms |
19236 KB |
Output is correct |
75 |
Correct |
125 ms |
16336 KB |
Output is correct |
76 |
Correct |
133 ms |
18728 KB |
Output is correct |
77 |
Correct |
76 ms |
15688 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
476 KB |
Output is correct |
86 |
Correct |
33 ms |
2568 KB |
Output is correct |
87 |
Correct |
33 ms |
2516 KB |
Output is correct |
88 |
Correct |
35 ms |
2772 KB |
Output is correct |
89 |
Correct |
34 ms |
2904 KB |
Output is correct |
90 |
Correct |
27 ms |
2892 KB |
Output is correct |
91 |
Correct |
25 ms |
2880 KB |
Output is correct |
92 |
Correct |
27 ms |
2480 KB |
Output is correct |
93 |
Correct |
77 ms |
14156 KB |
Output is correct |
94 |
Correct |
79 ms |
13348 KB |
Output is correct |
95 |
Correct |
98 ms |
14100 KB |
Output is correct |
96 |
Correct |
90 ms |
16456 KB |
Output is correct |
97 |
Correct |
101 ms |
16992 KB |
Output is correct |
98 |
Correct |
103 ms |
17012 KB |
Output is correct |
99 |
Correct |
27 ms |
10656 KB |
Output is correct |
100 |
Correct |
169 ms |
18856 KB |
Output is correct |
101 |
Correct |
135 ms |
18868 KB |
Output is correct |
102 |
Correct |
127 ms |
18988 KB |
Output is correct |
103 |
Correct |
146 ms |
18516 KB |
Output is correct |
104 |
Correct |
130 ms |
18252 KB |
Output is correct |
105 |
Correct |
132 ms |
18480 KB |
Output is correct |
106 |
Correct |
128 ms |
15224 KB |
Output is correct |
107 |
Correct |
143 ms |
15448 KB |
Output is correct |
108 |
Correct |
130 ms |
15960 KB |
Output is correct |
109 |
Correct |
127 ms |
16004 KB |
Output is correct |
110 |
Correct |
139 ms |
18772 KB |
Output is correct |
111 |
Correct |
148 ms |
18768 KB |
Output is correct |