#include<bits/stdc++.h>
#define MAXN 1000007
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 |
6 ms |
31064 KB |
Output is correct |
2 |
Correct |
6 ms |
31068 KB |
Output is correct |
3 |
Correct |
6 ms |
31064 KB |
Output is correct |
4 |
Correct |
790 ms |
126228 KB |
Output is correct |
5 |
Correct |
369 ms |
89284 KB |
Output is correct |
6 |
Runtime error |
270 ms |
142324 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31068 KB |
Output is correct |
3 |
Correct |
6 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31068 KB |
Output is correct |
6 |
Correct |
6 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31064 KB |
Output is correct |
9 |
Correct |
6 ms |
31068 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31324 KB |
Output is correct |
12 |
Correct |
6 ms |
31192 KB |
Output is correct |
13 |
Correct |
7 ms |
31324 KB |
Output is correct |
14 |
Correct |
7 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31068 KB |
Output is correct |
3 |
Correct |
6 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31068 KB |
Output is correct |
6 |
Correct |
6 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31064 KB |
Output is correct |
9 |
Correct |
6 ms |
31068 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31324 KB |
Output is correct |
12 |
Correct |
6 ms |
31192 KB |
Output is correct |
13 |
Correct |
7 ms |
31324 KB |
Output is correct |
14 |
Correct |
7 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31316 KB |
Output is correct |
16 |
Correct |
10 ms |
32092 KB |
Output is correct |
17 |
Correct |
10 ms |
31832 KB |
Output is correct |
18 |
Correct |
10 ms |
32092 KB |
Output is correct |
19 |
Correct |
12 ms |
32276 KB |
Output is correct |
20 |
Correct |
9 ms |
31576 KB |
Output is correct |
21 |
Correct |
8 ms |
31864 KB |
Output is correct |
22 |
Correct |
9 ms |
31812 KB |
Output is correct |
23 |
Correct |
10 ms |
31836 KB |
Output is correct |
24 |
Correct |
8 ms |
31836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31068 KB |
Output is correct |
3 |
Correct |
6 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31068 KB |
Output is correct |
6 |
Correct |
6 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31064 KB |
Output is correct |
9 |
Correct |
6 ms |
31068 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31324 KB |
Output is correct |
12 |
Correct |
6 ms |
31192 KB |
Output is correct |
13 |
Correct |
7 ms |
31324 KB |
Output is correct |
14 |
Correct |
7 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31316 KB |
Output is correct |
16 |
Correct |
10 ms |
32092 KB |
Output is correct |
17 |
Correct |
10 ms |
31832 KB |
Output is correct |
18 |
Correct |
10 ms |
32092 KB |
Output is correct |
19 |
Correct |
12 ms |
32276 KB |
Output is correct |
20 |
Correct |
9 ms |
31576 KB |
Output is correct |
21 |
Correct |
8 ms |
31864 KB |
Output is correct |
22 |
Correct |
9 ms |
31812 KB |
Output is correct |
23 |
Correct |
10 ms |
31836 KB |
Output is correct |
24 |
Correct |
8 ms |
31836 KB |
Output is correct |
25 |
Correct |
6 ms |
31068 KB |
Output is correct |
26 |
Correct |
6 ms |
31068 KB |
Output is correct |
27 |
Correct |
11 ms |
32092 KB |
Output is correct |
28 |
Correct |
10 ms |
31836 KB |
Output is correct |
29 |
Correct |
9 ms |
31580 KB |
Output is correct |
30 |
Correct |
9 ms |
31748 KB |
Output is correct |
31 |
Correct |
10 ms |
31832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31064 KB |
Output is correct |
2 |
Correct |
6 ms |
31068 KB |
Output is correct |
3 |
Correct |
6 ms |
31064 KB |
Output is correct |
4 |
Correct |
790 ms |
126228 KB |
Output is correct |
5 |
Correct |
369 ms |
89284 KB |
Output is correct |
6 |
Runtime error |
270 ms |
142324 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |