답안 #1085935

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1085935 2024-09-09T06:31:01 Z Bananabread Passport (JOI23_passport) C++17
100 / 100
535 ms 134904 KB
#include<bits/stdc++.h>
#define ll long long
#define ntr "\n"
#define mod (ll)(1e9+7)
#define taskname "temp"
#define frep freopen(taskname".inp","r",stdin); freopen(taskname".out","w",stdout);
using namespace std;
vector<pair<ll,ll>> adj[800100];
ll id[200010];
void build(ll pos,ll l,ll r){
    if(l==r){
        id[l]=pos;
    }
    else{
        ll mid=(l+r)/2;
        build(2*pos,l,mid);
        build(2*pos+1,mid+1,r);
        adj[2*pos].push_back({pos,0});
        adj[2*pos+1].push_back({pos,0});
    }
}
void add(ll pos,ll l,ll r,ll u,ll v,ll x){
    if(v<l||u>r) return ;
    else if(u<=l&&r<=v){
        adj[pos].push_back({id[x],1});
    }
    else{
        ll mid=(l+r)/2;
        add(2*pos,l,mid,u,v,x);
        add(2*pos+1,mid+1,r,u,v,x);
    }
}
ll n,q;
ll ans[800010];
ll dist[800010];
ll vis[800010];
ll a[200010];
ll b[200010];
ll l[200001];
ll r[200001];
void proc(ll st){
    memset(dist,0x3f,sizeof(dist));
    memset(vis,0,sizeof(vis));
    deque<ll> dq;
    dq.push_front(id[st]);
    dist[id[st]]=0;
    while(!dq.empty()){
        ll u=dq.front();
        dq.pop_front();
        if(vis[u]) continue;
        for(auto [v,w]:adj[u]){
            if(dist[v]>dist[u]+w){
                dist[v]=dist[u]+w;
                if(w==0){
                    dq.push_front(v);
                }
                else{
                    dq.push_back(v);
                }
            }
        }
    }
    dist[id[st]]=1;
    for(int i=1;i<=n;i++){
        if(st==1) a[i]=dist[id[i]];
        if(st==n) b[i]=dist[id[i]];
    }
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    //frep;
    cin>>n;
    build(1,1,n+1);
    for(int i=1;i<=n;i++){
        cin>>l[i]>>r[i];
        add(1,1,n+1,l[i],r[i],i);
    }
    proc(1);
    proc(n);
//    for(int i=1;i<=n;i++){
//        cout<<a[i]<<' ';
//    }
//    cout<<ntr;
//    for(int i=1;i<=n;i++){
//        cout<<b[i]<<' ';
//    }
//    cout<<ntr;
//    for(int i=1;i<=n+1;i++){
//        cout<<id[i]<<' ';
//    }
//    cout<<ntr;
    for(int i=1;i<=n;i++){
        adj[id[n+1]].push_back({id[i],max(1LL,a[i]+b[i]-1)});
        //cout<<n+1<<' '<<i<<' '<<a[i]+b[i]<<ntr;
    }
//    cout<<ntr;
//    for(auto [v,w]:adj[id[n+1]]){
//        cout<<id[n+1]<<' '<<v<<' '<<w<<ntr;
//    }
    priority_queue<pair<ll,ll>> pq;
    pq.push({0,id[n+1]});
    memset(dist,0x3f,sizeof(dist));
    memset(vis,0,sizeof(vis));
    dist[id[n+1]]=0;
    while(!pq.empty()){
        ll u=pq.top().second;
        pq.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(auto [v,w]:adj[u]){
            if(dist[v]>dist[u]+w){
                dist[v]=dist[u]+w;
                pq.push({-dist[v],v});
            }
        }
    }
    for(int i=1;i<=n;i++){
        ans[i]=dist[id[i]];
    }
    cin>>q;
    for(int i=1;i<=q;i++){
        ll x;
        cin>>x;
        cout<<(ans[x]<1e18 ? ans[x] : -1)<<ntr;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31576 KB Output is correct
2 Correct 15 ms 31580 KB Output is correct
3 Correct 16 ms 31636 KB Output is correct
4 Correct 530 ms 120972 KB Output is correct
5 Correct 290 ms 81600 KB Output is correct
6 Correct 179 ms 71384 KB Output is correct
7 Correct 203 ms 114328 KB Output is correct
8 Correct 132 ms 104840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31576 KB Output is correct
2 Correct 15 ms 31580 KB Output is correct
3 Correct 14 ms 31816 KB Output is correct
4 Correct 15 ms 31580 KB Output is correct
5 Correct 15 ms 31732 KB Output is correct
6 Correct 15 ms 31580 KB Output is correct
7 Correct 15 ms 31644 KB Output is correct
8 Correct 16 ms 31580 KB Output is correct
9 Correct 16 ms 31648 KB Output is correct
10 Correct 14 ms 31576 KB Output is correct
11 Correct 14 ms 31772 KB Output is correct
12 Correct 15 ms 31836 KB Output is correct
13 Correct 15 ms 31836 KB Output is correct
14 Correct 15 ms 31796 KB Output is correct
15 Correct 14 ms 31832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31576 KB Output is correct
2 Correct 15 ms 31580 KB Output is correct
3 Correct 14 ms 31816 KB Output is correct
4 Correct 15 ms 31580 KB Output is correct
5 Correct 15 ms 31732 KB Output is correct
6 Correct 15 ms 31580 KB Output is correct
7 Correct 15 ms 31644 KB Output is correct
8 Correct 16 ms 31580 KB Output is correct
9 Correct 16 ms 31648 KB Output is correct
10 Correct 14 ms 31576 KB Output is correct
11 Correct 14 ms 31772 KB Output is correct
12 Correct 15 ms 31836 KB Output is correct
13 Correct 15 ms 31836 KB Output is correct
14 Correct 15 ms 31796 KB Output is correct
15 Correct 14 ms 31832 KB Output is correct
16 Correct 16 ms 32604 KB Output is correct
17 Correct 17 ms 32228 KB Output is correct
18 Correct 18 ms 32796 KB Output is correct
19 Correct 20 ms 32860 KB Output is correct
20 Correct 16 ms 32344 KB Output is correct
21 Correct 16 ms 32348 KB Output is correct
22 Correct 16 ms 32604 KB Output is correct
23 Correct 17 ms 32604 KB Output is correct
24 Correct 16 ms 32604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31576 KB Output is correct
2 Correct 15 ms 31580 KB Output is correct
3 Correct 14 ms 31816 KB Output is correct
4 Correct 15 ms 31580 KB Output is correct
5 Correct 15 ms 31732 KB Output is correct
6 Correct 15 ms 31580 KB Output is correct
7 Correct 15 ms 31644 KB Output is correct
8 Correct 16 ms 31580 KB Output is correct
9 Correct 16 ms 31648 KB Output is correct
10 Correct 14 ms 31576 KB Output is correct
11 Correct 14 ms 31772 KB Output is correct
12 Correct 15 ms 31836 KB Output is correct
13 Correct 15 ms 31836 KB Output is correct
14 Correct 15 ms 31796 KB Output is correct
15 Correct 14 ms 31832 KB Output is correct
16 Correct 16 ms 32604 KB Output is correct
17 Correct 17 ms 32228 KB Output is correct
18 Correct 18 ms 32796 KB Output is correct
19 Correct 20 ms 32860 KB Output is correct
20 Correct 16 ms 32344 KB Output is correct
21 Correct 16 ms 32348 KB Output is correct
22 Correct 16 ms 32604 KB Output is correct
23 Correct 17 ms 32604 KB Output is correct
24 Correct 16 ms 32604 KB Output is correct
25 Correct 15 ms 31576 KB Output is correct
26 Correct 15 ms 31576 KB Output is correct
27 Correct 23 ms 32604 KB Output is correct
28 Correct 17 ms 32348 KB Output is correct
29 Correct 18 ms 32344 KB Output is correct
30 Correct 18 ms 32300 KB Output is correct
31 Correct 17 ms 32568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31576 KB Output is correct
2 Correct 15 ms 31580 KB Output is correct
3 Correct 16 ms 31636 KB Output is correct
4 Correct 530 ms 120972 KB Output is correct
5 Correct 290 ms 81600 KB Output is correct
6 Correct 179 ms 71384 KB Output is correct
7 Correct 203 ms 114328 KB Output is correct
8 Correct 132 ms 104840 KB Output is correct
9 Correct 15 ms 31576 KB Output is correct
10 Correct 15 ms 31580 KB Output is correct
11 Correct 14 ms 31816 KB Output is correct
12 Correct 15 ms 31580 KB Output is correct
13 Correct 15 ms 31732 KB Output is correct
14 Correct 15 ms 31580 KB Output is correct
15 Correct 15 ms 31644 KB Output is correct
16 Correct 16 ms 31580 KB Output is correct
17 Correct 16 ms 31648 KB Output is correct
18 Correct 14 ms 31576 KB Output is correct
19 Correct 14 ms 31772 KB Output is correct
20 Correct 15 ms 31836 KB Output is correct
21 Correct 15 ms 31836 KB Output is correct
22 Correct 15 ms 31796 KB Output is correct
23 Correct 14 ms 31832 KB Output is correct
24 Correct 16 ms 32604 KB Output is correct
25 Correct 17 ms 32228 KB Output is correct
26 Correct 18 ms 32796 KB Output is correct
27 Correct 20 ms 32860 KB Output is correct
28 Correct 16 ms 32344 KB Output is correct
29 Correct 16 ms 32348 KB Output is correct
30 Correct 16 ms 32604 KB Output is correct
31 Correct 17 ms 32604 KB Output is correct
32 Correct 16 ms 32604 KB Output is correct
33 Correct 15 ms 31576 KB Output is correct
34 Correct 15 ms 31576 KB Output is correct
35 Correct 23 ms 32604 KB Output is correct
36 Correct 17 ms 32348 KB Output is correct
37 Correct 18 ms 32344 KB Output is correct
38 Correct 18 ms 32300 KB Output is correct
39 Correct 17 ms 32568 KB Output is correct
40 Correct 535 ms 127896 KB Output is correct
41 Correct 288 ms 83284 KB Output is correct
42 Correct 372 ms 134900 KB Output is correct
43 Correct 352 ms 134904 KB Output is correct
44 Correct 199 ms 72752 KB Output is correct
45 Correct 263 ms 87176 KB Output is correct
46 Correct 112 ms 55536 KB Output is correct
47 Correct 316 ms 106052 KB Output is correct
48 Correct 237 ms 102968 KB Output is correct