#include <bits/stdc++.h>
#define MAX 200010
#define LSB(A) (A&(-A))
#define LOG 20
int N,Q;
typedef std::pair<int,int> pii;
std::vector<pii> eventos;
std::vector<int> add[MAX];
std::vector<int> rem[MAX];
int ft[MAX];
typedef std::pair<int,int*> pipo;
int custos[101000];
int trava;
int armazena[MAX];
int valor[MAX];
int jump[MAX][LOG];
void gera_conexoes(){
std::deque<pii> st;
///Menor indice L em que o final esteja presente entre L e R
for(int i=MAX-1;i!=-1;--i){
///Adiciona o finalzinho
pii menor = {1e9+1,1e9+1};
for(auto&x:rem[i]){
pii conj = {eventos[x].first,x};
menor=std::min(menor,conj);
}
if(menor.first!=1e9+1){
while(st.size()&&st.front().first>=menor.first){
st.pop_front();
}
st.push_front(menor);
}
///Pega o valor
for(auto&x:add[i]){
int l=0,r=st.size()-1;
while(l<r){
int m = (l+r+1)/2;
if(eventos[st[m].second].second<=eventos[x].second){
l=m;
}else r=m-1;
}
jump[x][0]=st[l].second;
}
}
}
int jumps(int a,int b){
int pos = a;
for(int i=LOG-1;i!=-1;--i){
if(((1<<i)&b)&&jump[pos][i]!=-1){
pos=jump[pos][i];
}
}
return pos;
}
int tabp5[5005][5005];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin>>N>>Q;
for(int i=0;i!=N;++i){
int a,b;
std::cin>>a>>b;
eventos.push_back({a,b});
}
{
std::vector<pipo> comprime;
for(auto&x:eventos){
comprime.push_back({x.first,&x.first});
comprime.push_back({x.second,&x.second});
}
std::sort(comprime.begin(),comprime.end());
int last=-1,cord=1;
for(auto&x:comprime){
if(x.first!=last){
last=x.first;
++cord;
}
*x.second=cord;
}
trava=cord;
}
for(int i=0;i!=N;++i){
add[eventos[i].first].push_back(i);
rem[eventos[i].second].push_back(i);
}
gera_conexoes();
// for(int i=0;i!=N;++i)if(jump[i][0]==i)jump[i][0]=-1;
for(int j=1;j!=LOG;++j){
for(int i=0;i!=N;++i){
if(jump[i][j-1]==-1)jump[i][j]=-1;
else
jump[i][j]=jump[jump[i][j-1]][j-1];
}
}
for(int i=0;i!=Q;++i){
int s,t;
std::cin>>s>>t;
--s;--t;
if(s==t){
std::cout<<"0\n";
continue;
}
if(eventos[t].second<eventos[s].second){
std::cout<<"impossible\n";
continue;
}
int cord = eventos[s].second;
int l=0,r=1e9;
while(l<r){
int m = (l+r)/2;
int q=jumps(t,m);
if(eventos[q].first<=cord){
r=m;
}else l=m+1;
}
// std::cout<<"ve "<<l<<"\n";
int q=jumps(t,l);
if(q!=s){
++l;
}
// std::cout<<"Val R: "<<r<<"\n";
if(r==1e9){
std::cout<<"impossible\n";
continue;
}
std::cout<<l<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
203 ms |
25160 KB |
Output is correct |
3 |
Correct |
244 ms |
25160 KB |
Output is correct |
4 |
Correct |
398 ms |
25144 KB |
Output is correct |
5 |
Correct |
331 ms |
25212 KB |
Output is correct |
6 |
Correct |
253 ms |
25332 KB |
Output is correct |
7 |
Correct |
257 ms |
25304 KB |
Output is correct |
8 |
Correct |
195 ms |
25640 KB |
Output is correct |
9 |
Correct |
219 ms |
28576 KB |
Output is correct |
10 |
Correct |
232 ms |
28532 KB |
Output is correct |
11 |
Correct |
224 ms |
28752 KB |
Output is correct |
12 |
Correct |
155 ms |
24812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9704 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
8 ms |
9852 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9704 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
8 ms |
9852 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
8 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9712 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9860 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
6 ms |
9812 KB |
Output is correct |
16 |
Correct |
7 ms |
9912 KB |
Output is correct |
17 |
Correct |
6 ms |
9816 KB |
Output is correct |
18 |
Correct |
6 ms |
9816 KB |
Output is correct |
19 |
Correct |
119 ms |
11784 KB |
Output is correct |
20 |
Correct |
117 ms |
11888 KB |
Output is correct |
21 |
Correct |
107 ms |
12216 KB |
Output is correct |
22 |
Correct |
77 ms |
12056 KB |
Output is correct |
23 |
Correct |
69 ms |
12116 KB |
Output is correct |
24 |
Correct |
69 ms |
12084 KB |
Output is correct |
25 |
Correct |
129 ms |
11768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9704 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
8 ms |
9852 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9844 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
7 ms |
9876 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
6 ms |
9872 KB |
Output is correct |
19 |
Correct |
114 ms |
26484 KB |
Output is correct |
20 |
Correct |
100 ms |
25736 KB |
Output is correct |
21 |
Correct |
86 ms |
23160 KB |
Output is correct |
22 |
Correct |
95 ms |
26264 KB |
Output is correct |
23 |
Correct |
98 ms |
26460 KB |
Output is correct |
24 |
Correct |
131 ms |
26500 KB |
Output is correct |
25 |
Correct |
52 ms |
20904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
25132 KB |
Output is correct |
2 |
Correct |
250 ms |
25100 KB |
Output is correct |
3 |
Correct |
339 ms |
25144 KB |
Output is correct |
4 |
Correct |
195 ms |
25652 KB |
Output is correct |
5 |
Correct |
242 ms |
25500 KB |
Output is correct |
6 |
Correct |
259 ms |
25488 KB |
Output is correct |
7 |
Correct |
249 ms |
28248 KB |
Output is correct |
8 |
Correct |
194 ms |
28548 KB |
Output is correct |
9 |
Correct |
100 ms |
26360 KB |
Output is correct |
10 |
Correct |
248 ms |
28024 KB |
Output is correct |
11 |
Correct |
210 ms |
28024 KB |
Output is correct |
12 |
Correct |
282 ms |
27896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
203 ms |
25160 KB |
Output is correct |
3 |
Correct |
244 ms |
25160 KB |
Output is correct |
4 |
Correct |
398 ms |
25144 KB |
Output is correct |
5 |
Correct |
331 ms |
25212 KB |
Output is correct |
6 |
Correct |
253 ms |
25332 KB |
Output is correct |
7 |
Correct |
257 ms |
25304 KB |
Output is correct |
8 |
Correct |
195 ms |
25640 KB |
Output is correct |
9 |
Correct |
219 ms |
28576 KB |
Output is correct |
10 |
Correct |
232 ms |
28532 KB |
Output is correct |
11 |
Correct |
224 ms |
28752 KB |
Output is correct |
12 |
Correct |
155 ms |
24812 KB |
Output is correct |
13 |
Correct |
5 ms |
9704 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
8 ms |
9852 KB |
Output is correct |
19 |
Correct |
7 ms |
9812 KB |
Output is correct |
20 |
Correct |
7 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
8 ms |
9684 KB |
Output is correct |
23 |
Correct |
6 ms |
9712 KB |
Output is correct |
24 |
Correct |
7 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9860 KB |
Output is correct |
26 |
Correct |
8 ms |
9940 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
7 ms |
9912 KB |
Output is correct |
29 |
Correct |
6 ms |
9816 KB |
Output is correct |
30 |
Correct |
6 ms |
9816 KB |
Output is correct |
31 |
Correct |
119 ms |
11784 KB |
Output is correct |
32 |
Correct |
117 ms |
11888 KB |
Output is correct |
33 |
Correct |
107 ms |
12216 KB |
Output is correct |
34 |
Correct |
77 ms |
12056 KB |
Output is correct |
35 |
Correct |
69 ms |
12116 KB |
Output is correct |
36 |
Correct |
69 ms |
12084 KB |
Output is correct |
37 |
Correct |
129 ms |
11768 KB |
Output is correct |
38 |
Correct |
6 ms |
9684 KB |
Output is correct |
39 |
Correct |
6 ms |
9684 KB |
Output is correct |
40 |
Correct |
8 ms |
9844 KB |
Output is correct |
41 |
Correct |
6 ms |
9812 KB |
Output is correct |
42 |
Correct |
6 ms |
9812 KB |
Output is correct |
43 |
Correct |
7 ms |
9876 KB |
Output is correct |
44 |
Correct |
7 ms |
9812 KB |
Output is correct |
45 |
Correct |
7 ms |
9812 KB |
Output is correct |
46 |
Correct |
6 ms |
9872 KB |
Output is correct |
47 |
Correct |
114 ms |
26484 KB |
Output is correct |
48 |
Correct |
100 ms |
25736 KB |
Output is correct |
49 |
Correct |
86 ms |
23160 KB |
Output is correct |
50 |
Correct |
95 ms |
26264 KB |
Output is correct |
51 |
Correct |
98 ms |
26460 KB |
Output is correct |
52 |
Correct |
131 ms |
26500 KB |
Output is correct |
53 |
Correct |
52 ms |
20904 KB |
Output is correct |
54 |
Correct |
199 ms |
25132 KB |
Output is correct |
55 |
Correct |
250 ms |
25100 KB |
Output is correct |
56 |
Correct |
339 ms |
25144 KB |
Output is correct |
57 |
Correct |
195 ms |
25652 KB |
Output is correct |
58 |
Correct |
242 ms |
25500 KB |
Output is correct |
59 |
Correct |
259 ms |
25488 KB |
Output is correct |
60 |
Correct |
249 ms |
28248 KB |
Output is correct |
61 |
Correct |
194 ms |
28548 KB |
Output is correct |
62 |
Correct |
100 ms |
26360 KB |
Output is correct |
63 |
Correct |
248 ms |
28024 KB |
Output is correct |
64 |
Correct |
210 ms |
28024 KB |
Output is correct |
65 |
Correct |
282 ms |
27896 KB |
Output is correct |
66 |
Correct |
5 ms |
9736 KB |
Output is correct |
67 |
Correct |
203 ms |
28120 KB |
Output is correct |
68 |
Correct |
288 ms |
28332 KB |
Output is correct |
69 |
Correct |
317 ms |
28256 KB |
Output is correct |
70 |
Correct |
275 ms |
28444 KB |
Output is correct |
71 |
Correct |
285 ms |
28272 KB |
Output is correct |
72 |
Correct |
262 ms |
28304 KB |
Output is correct |
73 |
Correct |
211 ms |
28664 KB |
Output is correct |
74 |
Correct |
211 ms |
28760 KB |
Output is correct |
75 |
Correct |
227 ms |
28988 KB |
Output is correct |
76 |
Correct |
192 ms |
28656 KB |
Output is correct |
77 |
Correct |
165 ms |
24888 KB |
Output is correct |
78 |
Correct |
5 ms |
9728 KB |
Output is correct |
79 |
Correct |
7 ms |
9812 KB |
Output is correct |
80 |
Correct |
6 ms |
9812 KB |
Output is correct |
81 |
Correct |
6 ms |
9888 KB |
Output is correct |
82 |
Correct |
6 ms |
9812 KB |
Output is correct |
83 |
Correct |
7 ms |
9876 KB |
Output is correct |
84 |
Correct |
6 ms |
9812 KB |
Output is correct |
85 |
Correct |
6 ms |
9800 KB |
Output is correct |
86 |
Correct |
121 ms |
11744 KB |
Output is correct |
87 |
Correct |
123 ms |
11932 KB |
Output is correct |
88 |
Correct |
93 ms |
12192 KB |
Output is correct |
89 |
Correct |
74 ms |
12112 KB |
Output is correct |
90 |
Correct |
71 ms |
12072 KB |
Output is correct |
91 |
Correct |
71 ms |
12056 KB |
Output is correct |
92 |
Correct |
112 ms |
11724 KB |
Output is correct |
93 |
Correct |
102 ms |
26512 KB |
Output is correct |
94 |
Correct |
86 ms |
25704 KB |
Output is correct |
95 |
Correct |
84 ms |
23344 KB |
Output is correct |
96 |
Correct |
100 ms |
26512 KB |
Output is correct |
97 |
Correct |
110 ms |
26492 KB |
Output is correct |
98 |
Correct |
106 ms |
26492 KB |
Output is correct |
99 |
Correct |
49 ms |
20860 KB |
Output is correct |
100 |
Correct |
259 ms |
28264 KB |
Output is correct |
101 |
Correct |
256 ms |
28280 KB |
Output is correct |
102 |
Correct |
186 ms |
28416 KB |
Output is correct |
103 |
Correct |
267 ms |
27956 KB |
Output is correct |
104 |
Correct |
209 ms |
27684 KB |
Output is correct |
105 |
Correct |
263 ms |
27956 KB |
Output is correct |
106 |
Correct |
218 ms |
26232 KB |
Output is correct |
107 |
Correct |
338 ms |
27728 KB |
Output is correct |
108 |
Correct |
153 ms |
25292 KB |
Output is correct |
109 |
Correct |
159 ms |
25192 KB |
Output is correct |
110 |
Correct |
180 ms |
28284 KB |
Output is correct |
111 |
Correct |
176 ms |
28288 KB |
Output is correct |