#include <bits/stdc++.h>
using namespace std;
int cnd;
vector<pair<int,int> > adj[500005];
struct node{
int s,e,m;
int nd;
node *l,*r;
node(int S, int E){
s=S; e=E; m=(s+e)/2;
nd=cnd;
cnd++;
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
adj[l->nd].push_back({nd,0});
adj[r->nd].push_back({nd,0});
}
else{
adj[s].push_back({nd,0});
}
}
void addedge(int S, int E, int N){
if(S<=s&&e<=E){
adj[nd].push_back({N,1});
return;
}
if(E<=m) l->addedge(S,E,N);
else if(S>m) r->addedge(S,E,N);
else{
l->addedge(S,m,N);
r->addedge(m+1,E,N);
}
}
} *root;
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
cnd=n+1;
root = new node(1,n);
priority_queue<pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > pq;
int dist[500005][3];
memset(dist,-1,sizeof(dist));
for(int i=0; i<n; i++){
int a,b;
cin >> a >> b;
root->addedge(a,b,i+1);
if(a==1) pq.push({1,{1,i+1}});
if(b==n) pq.push({1,{2,i+1}});
}
while(!pq.empty()){
int a=pq.top().first,b=pq.top().second.first,c=pq.top().second.second;
pq.pop();
if(dist[c][b]!=-1) continue;
dist[c][b]=a;
for(auto i:adj[c]){
if(dist[i.first][b]==-1) pq.push({a+i.second,{b,i.first}});
}
}
for(int i=1; i<=n; i++){
//cout << dist[i][1] << ' ' << dist[i][2] << '\n';
if(dist[i][1]!=-1&&dist[i][2]!=-1){
pq.push({dist[i][1]+dist[i][2]-1,{0,i}});
}
}
while(!pq.empty()){
int a=pq.top().first,b=pq.top().second.first,c=pq.top().second.second;
pq.pop();
if(dist[c][b]!=-1) continue;
dist[c][b]=a;
for(auto i:adj[c]){
if(dist[i.first][b]==-1) pq.push({a+i.second,{b,i.first}});
}
}
int q;
cin >> q;
for(int i=0; i<q; i++){
int x;
cin >> x;
cout << dist[x][0] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
17876 KB |
Output is correct |
2 |
Correct |
10 ms |
17876 KB |
Output is correct |
3 |
Correct |
11 ms |
17908 KB |
Output is correct |
4 |
Runtime error |
77 ms |
93984 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
17856 KB |
Output is correct |
2 |
Correct |
11 ms |
17876 KB |
Output is correct |
3 |
Correct |
11 ms |
17876 KB |
Output is correct |
4 |
Correct |
12 ms |
17876 KB |
Output is correct |
5 |
Correct |
12 ms |
17920 KB |
Output is correct |
6 |
Correct |
10 ms |
17876 KB |
Output is correct |
7 |
Correct |
10 ms |
17876 KB |
Output is correct |
8 |
Correct |
10 ms |
17876 KB |
Output is correct |
9 |
Correct |
10 ms |
17876 KB |
Output is correct |
10 |
Correct |
10 ms |
17876 KB |
Output is correct |
11 |
Correct |
10 ms |
18056 KB |
Output is correct |
12 |
Correct |
11 ms |
18004 KB |
Output is correct |
13 |
Correct |
11 ms |
17928 KB |
Output is correct |
14 |
Correct |
11 ms |
17964 KB |
Output is correct |
15 |
Correct |
11 ms |
17984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
17856 KB |
Output is correct |
2 |
Correct |
11 ms |
17876 KB |
Output is correct |
3 |
Correct |
11 ms |
17876 KB |
Output is correct |
4 |
Correct |
12 ms |
17876 KB |
Output is correct |
5 |
Correct |
12 ms |
17920 KB |
Output is correct |
6 |
Correct |
10 ms |
17876 KB |
Output is correct |
7 |
Correct |
10 ms |
17876 KB |
Output is correct |
8 |
Correct |
10 ms |
17876 KB |
Output is correct |
9 |
Correct |
10 ms |
17876 KB |
Output is correct |
10 |
Correct |
10 ms |
17876 KB |
Output is correct |
11 |
Correct |
10 ms |
18056 KB |
Output is correct |
12 |
Correct |
11 ms |
18004 KB |
Output is correct |
13 |
Correct |
11 ms |
17928 KB |
Output is correct |
14 |
Correct |
11 ms |
17964 KB |
Output is correct |
15 |
Correct |
11 ms |
17984 KB |
Output is correct |
16 |
Correct |
18 ms |
18844 KB |
Output is correct |
17 |
Correct |
15 ms |
18616 KB |
Output is correct |
18 |
Correct |
16 ms |
18900 KB |
Output is correct |
19 |
Correct |
15 ms |
18796 KB |
Output is correct |
20 |
Correct |
13 ms |
18516 KB |
Output is correct |
21 |
Correct |
13 ms |
18424 KB |
Output is correct |
22 |
Correct |
12 ms |
18444 KB |
Output is correct |
23 |
Correct |
16 ms |
18704 KB |
Output is correct |
24 |
Correct |
17 ms |
18596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
17856 KB |
Output is correct |
2 |
Correct |
11 ms |
17876 KB |
Output is correct |
3 |
Correct |
11 ms |
17876 KB |
Output is correct |
4 |
Correct |
12 ms |
17876 KB |
Output is correct |
5 |
Correct |
12 ms |
17920 KB |
Output is correct |
6 |
Correct |
10 ms |
17876 KB |
Output is correct |
7 |
Correct |
10 ms |
17876 KB |
Output is correct |
8 |
Correct |
10 ms |
17876 KB |
Output is correct |
9 |
Correct |
10 ms |
17876 KB |
Output is correct |
10 |
Correct |
10 ms |
17876 KB |
Output is correct |
11 |
Correct |
10 ms |
18056 KB |
Output is correct |
12 |
Correct |
11 ms |
18004 KB |
Output is correct |
13 |
Correct |
11 ms |
17928 KB |
Output is correct |
14 |
Correct |
11 ms |
17964 KB |
Output is correct |
15 |
Correct |
11 ms |
17984 KB |
Output is correct |
16 |
Correct |
18 ms |
18844 KB |
Output is correct |
17 |
Correct |
15 ms |
18616 KB |
Output is correct |
18 |
Correct |
16 ms |
18900 KB |
Output is correct |
19 |
Correct |
15 ms |
18796 KB |
Output is correct |
20 |
Correct |
13 ms |
18516 KB |
Output is correct |
21 |
Correct |
13 ms |
18424 KB |
Output is correct |
22 |
Correct |
12 ms |
18444 KB |
Output is correct |
23 |
Correct |
16 ms |
18704 KB |
Output is correct |
24 |
Correct |
17 ms |
18596 KB |
Output is correct |
25 |
Correct |
10 ms |
17896 KB |
Output is correct |
26 |
Correct |
11 ms |
17876 KB |
Output is correct |
27 |
Correct |
17 ms |
18900 KB |
Output is correct |
28 |
Correct |
15 ms |
18528 KB |
Output is correct |
29 |
Correct |
14 ms |
18516 KB |
Output is correct |
30 |
Correct |
18 ms |
18448 KB |
Output is correct |
31 |
Correct |
15 ms |
18588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
17876 KB |
Output is correct |
2 |
Correct |
10 ms |
17876 KB |
Output is correct |
3 |
Correct |
11 ms |
17908 KB |
Output is correct |
4 |
Runtime error |
77 ms |
93984 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |