// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const int inf=1e18;
const int mod=998244353;
const int mod2=1e9+7;
const int maxn=200005;
const int bl=650;
const int maxs=650;
const int maxm=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=1000005;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
vector<int> edge[4*maxn];
int n,check[4*maxn],dist[4*maxn][4],pos[maxn];
namespace Segtree{
void build(int l,int r,int id){
if(l==r){
pos[l]=id;
check[id]=l;
return;
}
int mid=(l+r)>>1;
build(l,mid,id<<1);build(mid+1,r,id<<1|1);
edge[id<<1].push_back(id);
edge[id<<1|1].push_back(id);
}
void update(int l,int r,int id,int tl,int tr,int p){
if(tr<l || r<tl) return;
if(tl<=l && r<=tr){
edge[id].push_back(pos[p]);
return;
}
int mid=(l+r)>>1;
update(l,mid,id<<1,tl,tr,p);update(mid+1,r,id<<1|1,tl,tr,p);
}
}
priority_queue<piii,vector<piii>,greater<piii>> pq;
void add(int u,int d,int x){
if(dist[u][x]>d){
dist[u][x]=d;
pq.push({d,{u,x}});
}
}
void solve(){
cin >> n;
Segtree::build(1,n,1);
for(int i=1;i<=4*n;i++){
for(int j=0;j<=3;j++) dist[i][j]=n+1;
}
for(int i=1;i<=n;i++){
int l,r;cin >> l >> r;
Segtree::update(1,n,1,l,r,i);
}
dist[pos[1]][1]=0;
dist[pos[n]][2]=0;
pq.push({0,{pos[1],1}});pq.push({0,{pos[n],2}});
while(!pq.empty()){
int d=pq.top().fi,u=pq.top().se.fi,x=pq.top().se.se;pq.pop();
if(dist[u][x]!=d) continue;
for(int v:edge[u]){
add(v,d+(check[v]>=1),x);
if(check[v]){
if(x==1) add(v,d+dist[v][2]+(check[v]==n),3);
if(x==2) add(v,d+dist[v][1]+(check[v]==1),3);
}
}
}
int q;cin >> q;
for(int i=1;i<=q;i++){
int u;cin >> u;
cout << (dist[pos[u]][3]==n+1?-1:dist[pos[u]][3]) << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19124 KB |
Output is correct |
2 |
Correct |
11 ms |
19120 KB |
Output is correct |
3 |
Correct |
11 ms |
19124 KB |
Output is correct |
4 |
Correct |
969 ms |
103524 KB |
Output is correct |
5 |
Correct |
487 ms |
77284 KB |
Output is correct |
6 |
Correct |
338 ms |
72232 KB |
Output is correct |
7 |
Correct |
352 ms |
89168 KB |
Output is correct |
8 |
Correct |
213 ms |
72176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19028 KB |
Output is correct |
2 |
Correct |
12 ms |
19120 KB |
Output is correct |
3 |
Correct |
13 ms |
19028 KB |
Output is correct |
4 |
Correct |
15 ms |
19116 KB |
Output is correct |
5 |
Correct |
11 ms |
19028 KB |
Output is correct |
6 |
Correct |
16 ms |
19120 KB |
Output is correct |
7 |
Correct |
11 ms |
19028 KB |
Output is correct |
8 |
Correct |
16 ms |
19132 KB |
Output is correct |
9 |
Correct |
12 ms |
19028 KB |
Output is correct |
10 |
Correct |
14 ms |
19028 KB |
Output is correct |
11 |
Correct |
14 ms |
19256 KB |
Output is correct |
12 |
Correct |
11 ms |
19160 KB |
Output is correct |
13 |
Correct |
11 ms |
19156 KB |
Output is correct |
14 |
Correct |
16 ms |
19128 KB |
Output is correct |
15 |
Correct |
11 ms |
19176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19028 KB |
Output is correct |
2 |
Correct |
12 ms |
19120 KB |
Output is correct |
3 |
Correct |
13 ms |
19028 KB |
Output is correct |
4 |
Correct |
15 ms |
19116 KB |
Output is correct |
5 |
Correct |
11 ms |
19028 KB |
Output is correct |
6 |
Correct |
16 ms |
19120 KB |
Output is correct |
7 |
Correct |
11 ms |
19028 KB |
Output is correct |
8 |
Correct |
16 ms |
19132 KB |
Output is correct |
9 |
Correct |
12 ms |
19028 KB |
Output is correct |
10 |
Correct |
14 ms |
19028 KB |
Output is correct |
11 |
Correct |
14 ms |
19256 KB |
Output is correct |
12 |
Correct |
11 ms |
19160 KB |
Output is correct |
13 |
Correct |
11 ms |
19156 KB |
Output is correct |
14 |
Correct |
16 ms |
19128 KB |
Output is correct |
15 |
Correct |
11 ms |
19176 KB |
Output is correct |
16 |
Correct |
21 ms |
20052 KB |
Output is correct |
17 |
Correct |
25 ms |
19824 KB |
Output is correct |
18 |
Correct |
28 ms |
20020 KB |
Output is correct |
19 |
Correct |
22 ms |
20116 KB |
Output is correct |
20 |
Correct |
15 ms |
19864 KB |
Output is correct |
21 |
Correct |
19 ms |
19796 KB |
Output is correct |
22 |
Correct |
13 ms |
19796 KB |
Output is correct |
23 |
Correct |
20 ms |
20060 KB |
Output is correct |
24 |
Correct |
22 ms |
20020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19028 KB |
Output is correct |
2 |
Correct |
12 ms |
19120 KB |
Output is correct |
3 |
Correct |
13 ms |
19028 KB |
Output is correct |
4 |
Correct |
15 ms |
19116 KB |
Output is correct |
5 |
Correct |
11 ms |
19028 KB |
Output is correct |
6 |
Correct |
16 ms |
19120 KB |
Output is correct |
7 |
Correct |
11 ms |
19028 KB |
Output is correct |
8 |
Correct |
16 ms |
19132 KB |
Output is correct |
9 |
Correct |
12 ms |
19028 KB |
Output is correct |
10 |
Correct |
14 ms |
19028 KB |
Output is correct |
11 |
Correct |
14 ms |
19256 KB |
Output is correct |
12 |
Correct |
11 ms |
19160 KB |
Output is correct |
13 |
Correct |
11 ms |
19156 KB |
Output is correct |
14 |
Correct |
16 ms |
19128 KB |
Output is correct |
15 |
Correct |
11 ms |
19176 KB |
Output is correct |
16 |
Correct |
21 ms |
20052 KB |
Output is correct |
17 |
Correct |
25 ms |
19824 KB |
Output is correct |
18 |
Correct |
28 ms |
20020 KB |
Output is correct |
19 |
Correct |
22 ms |
20116 KB |
Output is correct |
20 |
Correct |
15 ms |
19864 KB |
Output is correct |
21 |
Correct |
19 ms |
19796 KB |
Output is correct |
22 |
Correct |
13 ms |
19796 KB |
Output is correct |
23 |
Correct |
20 ms |
20060 KB |
Output is correct |
24 |
Correct |
22 ms |
20020 KB |
Output is correct |
25 |
Correct |
11 ms |
19116 KB |
Output is correct |
26 |
Correct |
14 ms |
19028 KB |
Output is correct |
27 |
Correct |
17 ms |
20144 KB |
Output is correct |
28 |
Correct |
16 ms |
19924 KB |
Output is correct |
29 |
Correct |
20 ms |
19924 KB |
Output is correct |
30 |
Correct |
14 ms |
19780 KB |
Output is correct |
31 |
Correct |
23 ms |
19948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19124 KB |
Output is correct |
2 |
Correct |
11 ms |
19120 KB |
Output is correct |
3 |
Correct |
11 ms |
19124 KB |
Output is correct |
4 |
Correct |
969 ms |
103524 KB |
Output is correct |
5 |
Correct |
487 ms |
77284 KB |
Output is correct |
6 |
Correct |
338 ms |
72232 KB |
Output is correct |
7 |
Correct |
352 ms |
89168 KB |
Output is correct |
8 |
Correct |
213 ms |
72176 KB |
Output is correct |
9 |
Correct |
16 ms |
19028 KB |
Output is correct |
10 |
Correct |
12 ms |
19120 KB |
Output is correct |
11 |
Correct |
13 ms |
19028 KB |
Output is correct |
12 |
Correct |
15 ms |
19116 KB |
Output is correct |
13 |
Correct |
11 ms |
19028 KB |
Output is correct |
14 |
Correct |
16 ms |
19120 KB |
Output is correct |
15 |
Correct |
11 ms |
19028 KB |
Output is correct |
16 |
Correct |
16 ms |
19132 KB |
Output is correct |
17 |
Correct |
12 ms |
19028 KB |
Output is correct |
18 |
Correct |
14 ms |
19028 KB |
Output is correct |
19 |
Correct |
14 ms |
19256 KB |
Output is correct |
20 |
Correct |
11 ms |
19160 KB |
Output is correct |
21 |
Correct |
11 ms |
19156 KB |
Output is correct |
22 |
Correct |
16 ms |
19128 KB |
Output is correct |
23 |
Correct |
11 ms |
19176 KB |
Output is correct |
24 |
Correct |
21 ms |
20052 KB |
Output is correct |
25 |
Correct |
25 ms |
19824 KB |
Output is correct |
26 |
Correct |
28 ms |
20020 KB |
Output is correct |
27 |
Correct |
22 ms |
20116 KB |
Output is correct |
28 |
Correct |
15 ms |
19864 KB |
Output is correct |
29 |
Correct |
19 ms |
19796 KB |
Output is correct |
30 |
Correct |
13 ms |
19796 KB |
Output is correct |
31 |
Correct |
20 ms |
20060 KB |
Output is correct |
32 |
Correct |
22 ms |
20020 KB |
Output is correct |
33 |
Correct |
11 ms |
19116 KB |
Output is correct |
34 |
Correct |
14 ms |
19028 KB |
Output is correct |
35 |
Correct |
17 ms |
20144 KB |
Output is correct |
36 |
Correct |
16 ms |
19924 KB |
Output is correct |
37 |
Correct |
20 ms |
19924 KB |
Output is correct |
38 |
Correct |
14 ms |
19780 KB |
Output is correct |
39 |
Correct |
23 ms |
19948 KB |
Output is correct |
40 |
Correct |
1061 ms |
116088 KB |
Output is correct |
41 |
Correct |
533 ms |
78048 KB |
Output is correct |
42 |
Correct |
664 ms |
103508 KB |
Output is correct |
43 |
Correct |
696 ms |
105552 KB |
Output is correct |
44 |
Correct |
361 ms |
72976 KB |
Output is correct |
45 |
Correct |
407 ms |
76620 KB |
Output is correct |
46 |
Correct |
176 ms |
41992 KB |
Output is correct |
47 |
Correct |
558 ms |
86584 KB |
Output is correct |
48 |
Correct |
461 ms |
94984 KB |
Output is correct |