답안 #1086760

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1086760 2024-09-11T12:19:22 Z nathan4690 Passport (JOI23_passport) C++17
100 / 100
801 ms 152468 KB
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;

ll n, rpos[maxn], l, r, lf[maxn], rt[maxn], q, x, d[maxn], d1[maxn],dn[maxn];
vector<pair<ll,ll>> G[maxn];
bool vis[maxn];
priority_queue<pll, vector<pll>, greater<pll>> pq;
vector<int> rr;

void addEdge(ll u, ll v, ll w){
    // cout << u << " -> " << v << " w = " << w;el;
    G[u].push_back({v, w});
}

void build(ll idx, ll l, ll r){
    if(l == r){
        rpos[l] = idx;
        return;
    }
//    addEdge(idx, idx*2, 0);
//    addEdge(idx, idx*2+1, 0);
    addEdge(idx*2, idx, 0);
    addEdge(idx*2+1, idx, 0);
    ll mid = (l+r)/2;
    build(idx*2, l, mid);
    build(idx*2+1, mid+1, r);
}

void addSeg(ll idx, ll l, ll r, ll u, ll v, ll tar, ll w){
    // connects tar -> [u,v] if not rev
    if(r < u || v < l) return;
    if(u <= l && r <= v){
        addEdge(idx, tar, w);
        return;
    }
    ll mid = (l+r)/2;
    addSeg(idx*2, l, mid, u, v, tar, w);
    addSeg(idx*2+1, mid+1, r, u, v, tar, w);
}

void calc(){
    while(!pq.empty()){
        pair<ll,ll> item = pq.top();
        pq.pop();
        ll dist = item.first, u = item.second;
        if(dist != d[u]) continue;
        vis[u] = true;
        for(pair<ll,ll> e: G[u]){
            ll c = e.first, w = e.second;
            if(d[u] + w < d[c]){
                // cout << u << " -> " << c << " changed: " << d[u] + w;el;
                d[c] = d[u] + w;
                pq.push({d[c], c});
            }
        }
    }
}

void dijkstra(ll s){
    d[0] = inf;
    f1(i,4*n) d[i] = inf;
    d[s] = 0ll;
    pq.push({d[s], s});
    calc();
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n;
    build(1, 1, n);
    f1(i,n){
        cin >> l >> r;
        lf[i] = l; rt[i] = r;
//        if(l != i){
//            addSeg(1,1,n,l,i-1,rpos[i],1);
//        }
//        if(i != r){
//            addSeg(1,1,n,i+1,r,rpos[i],1);
//        }
        addSeg(1,1,n,l,r,rpos[i],1);
    }
    dijkstra(rpos[1]);
    f1(i,4*n) {
        d1[i] = d[i];
    }
    dijkstra(rpos[n]);
    f1(i,4*n) {
        dn[i] = d[i];
    }
    f1(i,n) rr.push_back(rpos[i]);
    sort(rr.begin(), rr.end());
    f1(i,4*n) d[i] = inf;
    f1(i,4*n) {
        // cout << i << " = " << d1[i] << ' ' << dn[i];el;
        d[i] = d1[i] + dn[i] - binary_search(rr.begin(), rr.end(), i) + (i == rpos[1] || i == rpos[n]);
        pq.push({d[i], i});
    }
    // f1(i, 4*n) pq.push({min(d[i], d1[i] + dn[i]), i});
    calc();
    cin >> q;
    while(q--){
        cin >> x;
        // cout << x << " = " << rpos[x];el;
        cout << (d[rpos[x]] < 1e9 ? d[rpos[x]] : -1);el;
    }
    return 0;
}

Compilation message

passport.cpp: In function 'int main()':
passport.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 10 ms 23788 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 715 ms 137728 KB Output is correct
5 Correct 392 ms 98120 KB Output is correct
6 Correct 259 ms 87864 KB Output is correct
7 Correct 266 ms 82476 KB Output is correct
8 Correct 229 ms 94500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23952 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 10 ms 23964 KB Output is correct
5 Correct 11 ms 23928 KB Output is correct
6 Correct 10 ms 23964 KB Output is correct
7 Correct 11 ms 23900 KB Output is correct
8 Correct 10 ms 23864 KB Output is correct
9 Correct 10 ms 23896 KB Output is correct
10 Correct 10 ms 23908 KB Output is correct
11 Correct 14 ms 23900 KB Output is correct
12 Correct 10 ms 23948 KB Output is correct
13 Correct 10 ms 24052 KB Output is correct
14 Correct 11 ms 24056 KB Output is correct
15 Correct 13 ms 24092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23952 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 10 ms 23964 KB Output is correct
5 Correct 11 ms 23928 KB Output is correct
6 Correct 10 ms 23964 KB Output is correct
7 Correct 11 ms 23900 KB Output is correct
8 Correct 10 ms 23864 KB Output is correct
9 Correct 10 ms 23896 KB Output is correct
10 Correct 10 ms 23908 KB Output is correct
11 Correct 14 ms 23900 KB Output is correct
12 Correct 10 ms 23948 KB Output is correct
13 Correct 10 ms 24052 KB Output is correct
14 Correct 11 ms 24056 KB Output is correct
15 Correct 13 ms 24092 KB Output is correct
16 Correct 15 ms 25180 KB Output is correct
17 Correct 14 ms 24924 KB Output is correct
18 Correct 14 ms 25368 KB Output is correct
19 Correct 16 ms 25436 KB Output is correct
20 Correct 13 ms 24924 KB Output is correct
21 Correct 14 ms 25032 KB Output is correct
22 Correct 13 ms 24924 KB Output is correct
23 Correct 15 ms 25048 KB Output is correct
24 Correct 14 ms 25180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23952 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 10 ms 23964 KB Output is correct
5 Correct 11 ms 23928 KB Output is correct
6 Correct 10 ms 23964 KB Output is correct
7 Correct 11 ms 23900 KB Output is correct
8 Correct 10 ms 23864 KB Output is correct
9 Correct 10 ms 23896 KB Output is correct
10 Correct 10 ms 23908 KB Output is correct
11 Correct 14 ms 23900 KB Output is correct
12 Correct 10 ms 23948 KB Output is correct
13 Correct 10 ms 24052 KB Output is correct
14 Correct 11 ms 24056 KB Output is correct
15 Correct 13 ms 24092 KB Output is correct
16 Correct 15 ms 25180 KB Output is correct
17 Correct 14 ms 24924 KB Output is correct
18 Correct 14 ms 25368 KB Output is correct
19 Correct 16 ms 25436 KB Output is correct
20 Correct 13 ms 24924 KB Output is correct
21 Correct 14 ms 25032 KB Output is correct
22 Correct 13 ms 24924 KB Output is correct
23 Correct 15 ms 25048 KB Output is correct
24 Correct 14 ms 25180 KB Output is correct
25 Correct 11 ms 24156 KB Output is correct
26 Correct 11 ms 23896 KB Output is correct
27 Correct 15 ms 25180 KB Output is correct
28 Correct 18 ms 25104 KB Output is correct
29 Correct 13 ms 24920 KB Output is correct
30 Correct 14 ms 24992 KB Output is correct
31 Correct 15 ms 24928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 10 ms 23788 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 715 ms 137728 KB Output is correct
5 Correct 392 ms 98120 KB Output is correct
6 Correct 259 ms 87864 KB Output is correct
7 Correct 266 ms 82476 KB Output is correct
8 Correct 229 ms 94500 KB Output is correct
9 Correct 11 ms 23952 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 10 ms 23900 KB Output is correct
12 Correct 10 ms 23964 KB Output is correct
13 Correct 11 ms 23928 KB Output is correct
14 Correct 10 ms 23964 KB Output is correct
15 Correct 11 ms 23900 KB Output is correct
16 Correct 10 ms 23864 KB Output is correct
17 Correct 10 ms 23896 KB Output is correct
18 Correct 10 ms 23908 KB Output is correct
19 Correct 14 ms 23900 KB Output is correct
20 Correct 10 ms 23948 KB Output is correct
21 Correct 10 ms 24052 KB Output is correct
22 Correct 11 ms 24056 KB Output is correct
23 Correct 13 ms 24092 KB Output is correct
24 Correct 15 ms 25180 KB Output is correct
25 Correct 14 ms 24924 KB Output is correct
26 Correct 14 ms 25368 KB Output is correct
27 Correct 16 ms 25436 KB Output is correct
28 Correct 13 ms 24924 KB Output is correct
29 Correct 14 ms 25032 KB Output is correct
30 Correct 13 ms 24924 KB Output is correct
31 Correct 15 ms 25048 KB Output is correct
32 Correct 14 ms 25180 KB Output is correct
33 Correct 11 ms 24156 KB Output is correct
34 Correct 11 ms 23896 KB Output is correct
35 Correct 15 ms 25180 KB Output is correct
36 Correct 18 ms 25104 KB Output is correct
37 Correct 13 ms 24920 KB Output is correct
38 Correct 14 ms 24992 KB Output is correct
39 Correct 15 ms 24928 KB Output is correct
40 Correct 801 ms 138480 KB Output is correct
41 Correct 422 ms 98832 KB Output is correct
42 Correct 514 ms 152468 KB Output is correct
43 Correct 510 ms 151456 KB Output is correct
44 Correct 276 ms 87860 KB Output is correct
45 Correct 370 ms 102328 KB Output is correct
46 Correct 151 ms 53828 KB Output is correct
47 Correct 491 ms 106300 KB Output is correct
48 Correct 406 ms 119324 KB Output is correct