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 int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=2e5+5;
vector <int> g[N];
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);
int n;
cin>>n;
vector <int> l(n),r(n);
for(int i=0;i<n;i++)cin>>l[i]>>r[i];
for(int i=0;i<n;i++){
for(int j=l[i];j<=r[i];j++){
g[i+1].pb(j);
}
}
int q;cin>>q;
set <pair <int,int> > st;
while(q--){
int x;cin>>x;
vector <int> d(n+1,1e18),p(n+1);
d[x]=0;
st.insert({0,x});
while(!st.empty()){
int v=st.begin()->second;
d[v]++;
st.erase(st.begin());
for(auto to : g[v]){
if(d[to]>d[v]){
p[to]=v;
st.erase({d[to],to});
d[to]=d[v];
st.insert({d[to],to});
}
}
}
int mx=0;
set <int> s;
for(int i=1;i<=n;i++){
mx=max(mx,d[i]);
if(d[i]==1e18)break;
int curv=i;
while(curv!=x){
curv=p[curv];
s.insert(curv);
//cout<<curv<<" ";
}
//cout<<endl;
}
if(mx==1e18)cout<<-1<<"\n";
else cout<<s.size()<<"\n";
}
}
# | 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... |