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>
#define ll long long
#define ntr "\n"
#define mod (ll)(1e9+7)
#define taskname ""
#define frep freopen(taskname".inp","r",stdin); freopen(taskname".out","w",stdout);
using namespace std;
vector<pair<ll,ll>> adj[800001];
ll id[200001];
void build(ll pos,ll l,ll r){
if(l==r){
id[l]=pos;
}
else{
ll mid=(l+r)/2;
build(2*pos,l,mid);
build(2*pos+1,mid+1,r);
adj[pos].push_back({2*pos,0});
adj[pos].push_back({2*pos+1,0});
}
}
void add(ll pos,ll l,ll r,ll u,ll v,ll x){
if(v<l||u>r) return ;
else if(u<=l&&r<=v){
adj[id[x]].push_back({pos,1});
}
else{
ll mid=(l+r)/2;
add(2*pos,l,mid,u,v,x);
add(2*pos+1,mid+1,r,u,v,x);
}
}
ll n,q;
ll ans[800001];
ll dist[800001];
ll vis[800001];
void proc(ll st){
fill(dist+1,dist+4*n+1,1e18);
fill(vis+1,vis+4*n+1,0);
priority_queue<pair<ll,ll>> pq;
pq.push({0,id[st]});
dist[id[st]]=0;
while(!pq.empty()){
ll u=pq.top().first;
pq.pop();
if(vis[u]) continue;
for(auto [v,w]:adj[u]){
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
pq.push({-dist[v],v});
}
}
}
ll mx=0;
for(int i=1;i<=n;i++){
mx=max(mx,dist[id[i]]);
}
if(mx==1e18) ans[st]=-1;
else ans[st]=mx;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//frep;
cin>>n;
build(1,1,n);
for(int i=1;i<=n;i++){
ll l,r;
cin>>l>>r;
add(1,1,n,l,r,i);
}
for(int i=1;i<=n;i++){
proc(i);
}
cin>>q;
for(int i=1;i<=q;i++){
ll x;
cin>>x;
cout<<ans[x]<<ntr;
}
}
# | 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... |