#include <bits/stdc++.h>
#define MAX 200010
#define LSB(A) (A&(-A))
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];
void upd(int a,int b){a+=3;
// std::cout<<"Upd "<<a<<"\n";
while(a<MAX){
ft[a]=std::min(ft[a],b);
a+=LSB(a);
}
}
int query(int a){a+=3;
// std::cout<<"Upd "<<a<<"\n";
int r=1e9;
while(a>0){
r=std::min(r,ft[a]);
a-=LSB(a);
}
return r;
}
typedef std::pair<int,int*> pipo;
int custos[101000];
int trava;
int correntes[MAX],grupos[MAX];
int curgp=1;
int contr=1;
void simula(int x){
int base = N*2 + 5;
for(int i=0;i!=base;++i)ft[i]=1e9;
upd(base-eventos[x].second-5,1);
for(int i=0;i!=N;++i)custos[i]=1e9;
for(int i=eventos[x].second;i!=trava+1;++i){
int minimo=1e9;
for(auto&x:rem[i]){
int d = query(base-eventos[x].first-5);
custos[x]=d;
// std::cout<<x<<" "<<d<<" "<<MAX-eventos[x].first-5<<" "<<MAX-i-5<<"!\n";
minimo=std::min(minimo,d+1);
}
if(minimo==1e9){
curgp++;
}
for(auto&x:rem[i]){
grupos[x]=curgp;
correntes[x]=contr;
++contr;
}
// std::cout<<minimo<<"\n";
upd(base-i-5,minimo);
for(auto&x:rem[i]){
custos[x]=std::min(custos[x],minimo);
}
}
custos[x]=0;
}
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){
rem[eventos[i].second].push_back(i);
}
if(0&&N<=5005){
for(int i=0;i!=N;++i){
simula(i);
for(int j=0;j!=N;++j)
tabp5[i][j]=custos[j];
///!!!!
}
for(int i=0;i!=Q;++i){
int a,b;
std::cin>>a>>b;
--a;--b;
int d = tabp5[a][b];
if(d>1e6){
std::cout<<"impossible\n";
}else {
std::cout<<d<<"\n";
}
}
return 0;
}
if(0&&Q<=100){
for(int i=0;i!=Q;++i){
int a,b;
std::cin>>a>>b;
--a;--b;
simula(a);
int d = custos[b];
if(d>1e6){
std::cout<<"impossible\n";
}else {
std::cout<<d<<"\n";
}
}
return 0;
}else {///Caso 1
simula(0);
for(int i=0;i!=Q;++i){
int a,b;
std::cin>>a>>b;
--a;--b;
///Caso especial
if(eventos[a].second==eventos[b].second){
std::cout<<"1\n";
continue;
}
int d = correntes[b]-correntes[a];
if(grupos[a]!=grupos[b]||d<0){
std::cout<<"impossible\n";
}else {
std::cout<<d<<"\n";
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
71 ms |
16652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |