#include<bits/stdc++.h>
#define MAXN 2000007
using namespace std;
const long long inf=1e15;
int n,q,x,minv;
int l[MAXN],r[MAXN];
vector< pair<int,long long> > g[MAXN];
long long dist[MAXN],cost[MAXN];
bool li[MAXN];
priority_queue< pair<long long,int> , vector< pair<long long,int> > , greater< pair<long long,int> > > pq;
void dijkstra(int x){
for(int i=1;i<=5*n+10;i++){
li[i]=false; dist[i]=inf;
}
pq.push({0,x});
dist[x]=0;
while(!pq.empty()){
minv=pq.top().second;
pq.pop();
if(li[minv])continue;
li[minv]=true;
for(int i=0;i<g[minv].size();i++){
if(li[g[minv][i].first] or dist[g[minv][i].first]<=dist[minv]+g[minv][i].second)continue;
dist[g[minv][i].first]=dist[minv]+g[minv][i].second;
pq.push({dist[g[minv][i].first],g[minv][i].first});
}
}
}
void build(int t,int l,int r){
if(l==r){
g[l+4*n].push_back({t,0});
}else{
int tt=(l+r)/2;
g[2*t].push_back({t,0});
build(2*t,l,tt);
g[2*t+1].push_back({t,0});
build(2*t+1,tt+1,r);
}
}
void update(int t,int l,int r,int ll,int rr,int root){
if(ll>rr)return;
if(l==ll and r==rr){
g[t].push_back({root,1});
}else{
int tt=(l+r)/2;
update(2*t,l,tt,ll,min(tt,rr),root);
update(2*t+1,tt+1,r,max(tt+1,ll),rr,root);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>l[i]>>r[i];
}
build(1,1,n);
for(int i=1;i<=n;i++){
update(1,1,n,l[i],r[i],i+4*n);
}
dijkstra(4*n+1); dist[4*n+1]=1;
for(int i=1;i<=n;i++){
cost[i]+=dist[4*n+i];
}
dijkstra(4*n+n); dist[4*n+n]=1;
for(int i=1;i<=n;i++){
cost[i]+=dist[4*n+i];
}
for(int i=1;i<=n;i++){
g[5*n+1].push_back({i+4*n,cost[i]});
}
dijkstra(5*n+1);
cin>>q;
for(int i=1;i<=q;i++){
cin>>x;
if(dist[4*n+x]==inf)cout<<"-1\n";
else cout<<dist[4*n+x]-1<<"\n";
}
return 0;
}
Compilation message
passport.cpp: In function 'void dijkstra(int)':
passport.cpp:31:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i=0;i<g[minv].size();i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
55644 KB |
Output is correct |
2 |
Correct |
14 ms |
55644 KB |
Output is correct |
3 |
Correct |
12 ms |
55640 KB |
Output is correct |
4 |
Correct |
761 ms |
155152 KB |
Output is correct |
5 |
Correct |
372 ms |
118980 KB |
Output is correct |
6 |
Correct |
219 ms |
110784 KB |
Output is correct |
7 |
Correct |
235 ms |
100580 KB |
Output is correct |
8 |
Correct |
145 ms |
109748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
55644 KB |
Output is correct |
2 |
Correct |
11 ms |
55640 KB |
Output is correct |
3 |
Correct |
11 ms |
55856 KB |
Output is correct |
4 |
Correct |
11 ms |
55644 KB |
Output is correct |
5 |
Correct |
11 ms |
55788 KB |
Output is correct |
6 |
Correct |
11 ms |
55644 KB |
Output is correct |
7 |
Correct |
12 ms |
55808 KB |
Output is correct |
8 |
Correct |
11 ms |
55864 KB |
Output is correct |
9 |
Correct |
11 ms |
55740 KB |
Output is correct |
10 |
Correct |
11 ms |
55644 KB |
Output is correct |
11 |
Correct |
13 ms |
55900 KB |
Output is correct |
12 |
Correct |
13 ms |
55956 KB |
Output is correct |
13 |
Correct |
12 ms |
55896 KB |
Output is correct |
14 |
Correct |
12 ms |
55900 KB |
Output is correct |
15 |
Correct |
12 ms |
55900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
55644 KB |
Output is correct |
2 |
Correct |
11 ms |
55640 KB |
Output is correct |
3 |
Correct |
11 ms |
55856 KB |
Output is correct |
4 |
Correct |
11 ms |
55644 KB |
Output is correct |
5 |
Correct |
11 ms |
55788 KB |
Output is correct |
6 |
Correct |
11 ms |
55644 KB |
Output is correct |
7 |
Correct |
12 ms |
55808 KB |
Output is correct |
8 |
Correct |
11 ms |
55864 KB |
Output is correct |
9 |
Correct |
11 ms |
55740 KB |
Output is correct |
10 |
Correct |
11 ms |
55644 KB |
Output is correct |
11 |
Correct |
13 ms |
55900 KB |
Output is correct |
12 |
Correct |
13 ms |
55956 KB |
Output is correct |
13 |
Correct |
12 ms |
55896 KB |
Output is correct |
14 |
Correct |
12 ms |
55900 KB |
Output is correct |
15 |
Correct |
12 ms |
55900 KB |
Output is correct |
16 |
Correct |
16 ms |
56668 KB |
Output is correct |
17 |
Correct |
16 ms |
56412 KB |
Output is correct |
18 |
Correct |
16 ms |
56924 KB |
Output is correct |
19 |
Correct |
17 ms |
56892 KB |
Output is correct |
20 |
Correct |
14 ms |
56432 KB |
Output is correct |
21 |
Correct |
13 ms |
56412 KB |
Output is correct |
22 |
Correct |
13 ms |
56288 KB |
Output is correct |
23 |
Correct |
15 ms |
56624 KB |
Output is correct |
24 |
Correct |
16 ms |
56404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
55644 KB |
Output is correct |
2 |
Correct |
11 ms |
55640 KB |
Output is correct |
3 |
Correct |
11 ms |
55856 KB |
Output is correct |
4 |
Correct |
11 ms |
55644 KB |
Output is correct |
5 |
Correct |
11 ms |
55788 KB |
Output is correct |
6 |
Correct |
11 ms |
55644 KB |
Output is correct |
7 |
Correct |
12 ms |
55808 KB |
Output is correct |
8 |
Correct |
11 ms |
55864 KB |
Output is correct |
9 |
Correct |
11 ms |
55740 KB |
Output is correct |
10 |
Correct |
11 ms |
55644 KB |
Output is correct |
11 |
Correct |
13 ms |
55900 KB |
Output is correct |
12 |
Correct |
13 ms |
55956 KB |
Output is correct |
13 |
Correct |
12 ms |
55896 KB |
Output is correct |
14 |
Correct |
12 ms |
55900 KB |
Output is correct |
15 |
Correct |
12 ms |
55900 KB |
Output is correct |
16 |
Correct |
16 ms |
56668 KB |
Output is correct |
17 |
Correct |
16 ms |
56412 KB |
Output is correct |
18 |
Correct |
16 ms |
56924 KB |
Output is correct |
19 |
Correct |
17 ms |
56892 KB |
Output is correct |
20 |
Correct |
14 ms |
56432 KB |
Output is correct |
21 |
Correct |
13 ms |
56412 KB |
Output is correct |
22 |
Correct |
13 ms |
56288 KB |
Output is correct |
23 |
Correct |
15 ms |
56624 KB |
Output is correct |
24 |
Correct |
16 ms |
56404 KB |
Output is correct |
25 |
Correct |
11 ms |
55640 KB |
Output is correct |
26 |
Correct |
11 ms |
55644 KB |
Output is correct |
27 |
Correct |
16 ms |
56756 KB |
Output is correct |
28 |
Correct |
16 ms |
56412 KB |
Output is correct |
29 |
Correct |
15 ms |
56412 KB |
Output is correct |
30 |
Correct |
14 ms |
56408 KB |
Output is correct |
31 |
Correct |
15 ms |
56408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
55644 KB |
Output is correct |
2 |
Correct |
14 ms |
55644 KB |
Output is correct |
3 |
Correct |
12 ms |
55640 KB |
Output is correct |
4 |
Correct |
761 ms |
155152 KB |
Output is correct |
5 |
Correct |
372 ms |
118980 KB |
Output is correct |
6 |
Correct |
219 ms |
110784 KB |
Output is correct |
7 |
Correct |
235 ms |
100580 KB |
Output is correct |
8 |
Correct |
145 ms |
109748 KB |
Output is correct |
9 |
Correct |
11 ms |
55644 KB |
Output is correct |
10 |
Correct |
11 ms |
55640 KB |
Output is correct |
11 |
Correct |
11 ms |
55856 KB |
Output is correct |
12 |
Correct |
11 ms |
55644 KB |
Output is correct |
13 |
Correct |
11 ms |
55788 KB |
Output is correct |
14 |
Correct |
11 ms |
55644 KB |
Output is correct |
15 |
Correct |
12 ms |
55808 KB |
Output is correct |
16 |
Correct |
11 ms |
55864 KB |
Output is correct |
17 |
Correct |
11 ms |
55740 KB |
Output is correct |
18 |
Correct |
11 ms |
55644 KB |
Output is correct |
19 |
Correct |
13 ms |
55900 KB |
Output is correct |
20 |
Correct |
13 ms |
55956 KB |
Output is correct |
21 |
Correct |
12 ms |
55896 KB |
Output is correct |
22 |
Correct |
12 ms |
55900 KB |
Output is correct |
23 |
Correct |
12 ms |
55900 KB |
Output is correct |
24 |
Correct |
16 ms |
56668 KB |
Output is correct |
25 |
Correct |
16 ms |
56412 KB |
Output is correct |
26 |
Correct |
16 ms |
56924 KB |
Output is correct |
27 |
Correct |
17 ms |
56892 KB |
Output is correct |
28 |
Correct |
14 ms |
56432 KB |
Output is correct |
29 |
Correct |
13 ms |
56412 KB |
Output is correct |
30 |
Correct |
13 ms |
56288 KB |
Output is correct |
31 |
Correct |
15 ms |
56624 KB |
Output is correct |
32 |
Correct |
16 ms |
56404 KB |
Output is correct |
33 |
Correct |
11 ms |
55640 KB |
Output is correct |
34 |
Correct |
11 ms |
55644 KB |
Output is correct |
35 |
Correct |
16 ms |
56756 KB |
Output is correct |
36 |
Correct |
16 ms |
56412 KB |
Output is correct |
37 |
Correct |
15 ms |
56412 KB |
Output is correct |
38 |
Correct |
14 ms |
56408 KB |
Output is correct |
39 |
Correct |
15 ms |
56408 KB |
Output is correct |
40 |
Correct |
814 ms |
158904 KB |
Output is correct |
41 |
Correct |
402 ms |
118328 KB |
Output is correct |
42 |
Correct |
538 ms |
166040 KB |
Output is correct |
43 |
Correct |
522 ms |
163588 KB |
Output is correct |
44 |
Correct |
246 ms |
110012 KB |
Output is correct |
45 |
Correct |
285 ms |
120056 KB |
Output is correct |
46 |
Correct |
177 ms |
78244 KB |
Output is correct |
47 |
Correct |
473 ms |
124976 KB |
Output is correct |
48 |
Correct |
334 ms |
133532 KB |
Output is correct |