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 F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=2e5+5;
int dis[N],L[N],R[N],n,q;
set<int> s;
void bfs(){
for (int i=2;i<=n;i++) dis[i]=INT_MAX;
queue<int> q;q.push(1);s.erase(1);
while (!q.empty()){
int x=q.front();q.pop();
while (true){
auto it=s.lower_bound(L[x]);
if (it==s.end()) break;
if (*it>R[x]) break;
int y=*it;
s.erase(it);
dis[y]=dis[x]+1;
q.push(y);
}
}return;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n;
for (int i=1;i<=n;i++) cin>>L[i]>>R[i],s.ep(i);
cin>>q;
while (q--){
int x;cin>>x;
bfs();
int mx=0;
for (int i=1;i<=n;i++) mx=max(mx,dis[i]);
if (mx>=INT_MAX) cout<<-1<<endl;
else cout<<mx<<endl;
}
return 0;
}
# | 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... |