This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n,q,l[500005],r[500005];
int dis[500005];
int su[800005];
struct nod
{
int ll;
int rr;
int id;
}node[500005];
bool cmp(nod x,nod y)
{
if(x.rr == y.rr)return x.ll < y.ll;
else return x.rr < y.rr;
}
void pushup(int rt)
{
su[rt] = min(su[rt<<1],su[rt<<1|1]);
return;
}
void build(int l,int r,int rt)
{
if(l == r)
{
su[rt] = dis[l];
return;
}
int mid = (l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
return;
}
int query(int l,int r,int queryl,int queryr,int rt)
{
if(queryl <= l && r <= queryr)
{
return su[rt];
}
int maxn = 1e9;
int mid = (l+r)>>1;
if(mid >= queryl)maxn = min(maxn,query(l,mid,queryl,queryr,rt<<1));
if(mid+1 <= queryr)maxn = min(maxn,query(mid+1,r,queryl,queryr,rt<<1|1));
return maxn;
}
void change(int l,int r,int askl,int askr,int val,int rt)
{
if(askl <= l && r <= askr)
{
su[rt] = val;
return;
}
int mid = (l+r)>>1;
if(mid >= askl)change(l,mid,askl,askr,val,rt<<1);
if(mid+1 <= askr)change(mid+1,r,askl,askr,val,rt<<1|1);
pushup(rt);
return;
}
int main()
{
//freopen("std.out","wb",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>l[i]>>r[i];
nod temp = nod{l[i],r[i],i};
node[i] = temp;
}
sort(node+1,node+n+1,cmp);
while(q--)
{
for(int i=1;i<=n;i++)dis[i] = 1e9;
int init,target;
cin>>init>>target;
int num = 0;
for(int i=1;i<=n;i++)
{
if(node[i].ll == l[init] && node[i].rr == r[init])
{
num = i;
break;
}
}
dis[node[num].id] = 0;
build(1,n,1);
change(1,n,num,num,dis[node[num].id],1);
for(int i=num+1;i<=n;i++)
{
int l = 1;
int r = i-1;
int ans = -1;
while(l <= r)
{
int mid = (l+r)>>1;
if(node[mid].rr >= node[i].ll)
{
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
//即[ans,i-1]是滿足rr >= ll的;
//cout<<"yeah2"<<endl;
if(ans != -1)dis[node[i].id] = query(1,n,ans,i-1,1) + 1;
//cout<<"yeah3"<<endl;
change(1,n,i,i,dis[node[i].id],1);
//cout<<"yeah4"<<endl;
}
if(dis[target] >= 1e9)cout<<"impossible"<<endl;
else cout<<dis[target]<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |