#include <bits/stdc++.h>
using namespace std;
int cnd;
vector<pair<int,int> > adj[1000005];
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[1000005][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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35452 KB |
Output is correct |
3 |
Correct |
19 ms |
35540 KB |
Output is correct |
4 |
Correct |
1793 ms |
127668 KB |
Output is correct |
5 |
Correct |
592 ms |
86604 KB |
Output is correct |
6 |
Correct |
287 ms |
80424 KB |
Output is correct |
7 |
Correct |
399 ms |
80204 KB |
Output is correct |
8 |
Correct |
215 ms |
80236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35516 KB |
Output is correct |
3 |
Correct |
21 ms |
35480 KB |
Output is correct |
4 |
Correct |
21 ms |
35540 KB |
Output is correct |
5 |
Correct |
20 ms |
35540 KB |
Output is correct |
6 |
Correct |
20 ms |
35484 KB |
Output is correct |
7 |
Correct |
20 ms |
35540 KB |
Output is correct |
8 |
Correct |
19 ms |
35536 KB |
Output is correct |
9 |
Correct |
19 ms |
35472 KB |
Output is correct |
10 |
Correct |
20 ms |
35424 KB |
Output is correct |
11 |
Correct |
19 ms |
35600 KB |
Output is correct |
12 |
Correct |
19 ms |
35508 KB |
Output is correct |
13 |
Correct |
19 ms |
35524 KB |
Output is correct |
14 |
Correct |
19 ms |
35540 KB |
Output is correct |
15 |
Correct |
23 ms |
35560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35516 KB |
Output is correct |
3 |
Correct |
21 ms |
35480 KB |
Output is correct |
4 |
Correct |
21 ms |
35540 KB |
Output is correct |
5 |
Correct |
20 ms |
35540 KB |
Output is correct |
6 |
Correct |
20 ms |
35484 KB |
Output is correct |
7 |
Correct |
20 ms |
35540 KB |
Output is correct |
8 |
Correct |
19 ms |
35536 KB |
Output is correct |
9 |
Correct |
19 ms |
35472 KB |
Output is correct |
10 |
Correct |
20 ms |
35424 KB |
Output is correct |
11 |
Correct |
19 ms |
35600 KB |
Output is correct |
12 |
Correct |
19 ms |
35508 KB |
Output is correct |
13 |
Correct |
19 ms |
35524 KB |
Output is correct |
14 |
Correct |
19 ms |
35540 KB |
Output is correct |
15 |
Correct |
23 ms |
35560 KB |
Output is correct |
16 |
Correct |
29 ms |
36432 KB |
Output is correct |
17 |
Correct |
25 ms |
36144 KB |
Output is correct |
18 |
Correct |
25 ms |
36368 KB |
Output is correct |
19 |
Correct |
25 ms |
36356 KB |
Output is correct |
20 |
Correct |
22 ms |
36052 KB |
Output is correct |
21 |
Correct |
21 ms |
36052 KB |
Output is correct |
22 |
Correct |
24 ms |
36008 KB |
Output is correct |
23 |
Correct |
25 ms |
36268 KB |
Output is correct |
24 |
Correct |
25 ms |
36256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35516 KB |
Output is correct |
3 |
Correct |
21 ms |
35480 KB |
Output is correct |
4 |
Correct |
21 ms |
35540 KB |
Output is correct |
5 |
Correct |
20 ms |
35540 KB |
Output is correct |
6 |
Correct |
20 ms |
35484 KB |
Output is correct |
7 |
Correct |
20 ms |
35540 KB |
Output is correct |
8 |
Correct |
19 ms |
35536 KB |
Output is correct |
9 |
Correct |
19 ms |
35472 KB |
Output is correct |
10 |
Correct |
20 ms |
35424 KB |
Output is correct |
11 |
Correct |
19 ms |
35600 KB |
Output is correct |
12 |
Correct |
19 ms |
35508 KB |
Output is correct |
13 |
Correct |
19 ms |
35524 KB |
Output is correct |
14 |
Correct |
19 ms |
35540 KB |
Output is correct |
15 |
Correct |
23 ms |
35560 KB |
Output is correct |
16 |
Correct |
29 ms |
36432 KB |
Output is correct |
17 |
Correct |
25 ms |
36144 KB |
Output is correct |
18 |
Correct |
25 ms |
36368 KB |
Output is correct |
19 |
Correct |
25 ms |
36356 KB |
Output is correct |
20 |
Correct |
22 ms |
36052 KB |
Output is correct |
21 |
Correct |
21 ms |
36052 KB |
Output is correct |
22 |
Correct |
24 ms |
36008 KB |
Output is correct |
23 |
Correct |
25 ms |
36268 KB |
Output is correct |
24 |
Correct |
25 ms |
36256 KB |
Output is correct |
25 |
Correct |
23 ms |
35540 KB |
Output is correct |
26 |
Correct |
18 ms |
35412 KB |
Output is correct |
27 |
Correct |
28 ms |
36504 KB |
Output is correct |
28 |
Correct |
25 ms |
36172 KB |
Output is correct |
29 |
Correct |
21 ms |
36136 KB |
Output is correct |
30 |
Correct |
21 ms |
36108 KB |
Output is correct |
31 |
Correct |
24 ms |
36272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35452 KB |
Output is correct |
3 |
Correct |
19 ms |
35540 KB |
Output is correct |
4 |
Correct |
1793 ms |
127668 KB |
Output is correct |
5 |
Correct |
592 ms |
86604 KB |
Output is correct |
6 |
Correct |
287 ms |
80424 KB |
Output is correct |
7 |
Correct |
399 ms |
80204 KB |
Output is correct |
8 |
Correct |
215 ms |
80236 KB |
Output is correct |
9 |
Correct |
19 ms |
35540 KB |
Output is correct |
10 |
Correct |
19 ms |
35516 KB |
Output is correct |
11 |
Correct |
21 ms |
35480 KB |
Output is correct |
12 |
Correct |
21 ms |
35540 KB |
Output is correct |
13 |
Correct |
20 ms |
35540 KB |
Output is correct |
14 |
Correct |
20 ms |
35484 KB |
Output is correct |
15 |
Correct |
20 ms |
35540 KB |
Output is correct |
16 |
Correct |
19 ms |
35536 KB |
Output is correct |
17 |
Correct |
19 ms |
35472 KB |
Output is correct |
18 |
Correct |
20 ms |
35424 KB |
Output is correct |
19 |
Correct |
19 ms |
35600 KB |
Output is correct |
20 |
Correct |
19 ms |
35508 KB |
Output is correct |
21 |
Correct |
19 ms |
35524 KB |
Output is correct |
22 |
Correct |
19 ms |
35540 KB |
Output is correct |
23 |
Correct |
23 ms |
35560 KB |
Output is correct |
24 |
Correct |
29 ms |
36432 KB |
Output is correct |
25 |
Correct |
25 ms |
36144 KB |
Output is correct |
26 |
Correct |
25 ms |
36368 KB |
Output is correct |
27 |
Correct |
25 ms |
36356 KB |
Output is correct |
28 |
Correct |
22 ms |
36052 KB |
Output is correct |
29 |
Correct |
21 ms |
36052 KB |
Output is correct |
30 |
Correct |
24 ms |
36008 KB |
Output is correct |
31 |
Correct |
25 ms |
36268 KB |
Output is correct |
32 |
Correct |
25 ms |
36256 KB |
Output is correct |
33 |
Correct |
23 ms |
35540 KB |
Output is correct |
34 |
Correct |
18 ms |
35412 KB |
Output is correct |
35 |
Correct |
28 ms |
36504 KB |
Output is correct |
36 |
Correct |
25 ms |
36172 KB |
Output is correct |
37 |
Correct |
21 ms |
36136 KB |
Output is correct |
38 |
Correct |
21 ms |
36108 KB |
Output is correct |
39 |
Correct |
24 ms |
36272 KB |
Output is correct |
40 |
Execution timed out |
2052 ms |
152644 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |