답안 #946697

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
946697 2024-03-14T22:46:51 Z vladilius Passport (JOI23_passport) C++17
6 / 100
693 ms 77040 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int inf = 1e9;

struct ST{
    vector<vector<pii>> t;
    vector<int> idx;
    int n;
    void build(int v, int tl, int tr){
        if (tl == tr){
            idx[tl] = v;
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        build(vv, tl, tm);
        build(vv + 1, tm + 1, tr);
        t[vv].push_back({v, 0});
        t[vv + 1].push_back({v, 0});
    }
    ST(int ns){
        n = ns;
        t.resize(4 * n);
        idx.resize(n + 1);
        build(1, 1, n);
    }
    void add(int v, int tl, int tr, int& l, int& r, int& i){
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            if (i != v){
                t[v].push_back({i, 1});
            }
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        add(vv, tl, tm, l, r, i);
        add(vv + 1, tm + 1, tr, l, r, i);
    }
    void add(int p, int l, int r){
        add(1, 1, n, l, r, idx[p]);
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n; cin>>n;
    ST T(n);
    for (int i = 1; i <= n; i++){
        int l, r; cin>>l>>r;
        T.add(i, l, r);
    }
    vector<int> out(4 * n);
    auto dijkstra = [&](int v){
        priority_queue<pii, vector<pii>, greater<pii>> pq;
        pq.push({0, v});
        vector<int> dist(4 * n, inf);
        dist[v] = 0;
        while (!pq.empty()){
            auto [d, k] = pq.top(); pq.pop();
            for (auto [i, w]: T.t[k]){
                if (dist[i] > dist[k] + w){
                    dist[i] = dist[k] + w;
                    pq.push({dist[i], i});
                }
            }
        }
        for (int i = 1; i < 4 * n; i++){
            out[i] += dist[i];
        }
    };
    dijkstra(T.idx[1]);
    dijkstra(T.idx[n]);
    for (int i = 1; i < 4 * n; i++){
        out[i] = min(out[i], inf);
    }
    
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    for (int i = 1; i < 4 * n; i++){
        if (out[i] != inf){
            pq.push({out[i], i});
        }
    }
    while (!pq.empty()){
        auto [d, v] = pq.top(); pq.pop();
        for (auto [i, w]: T.t[v]){
            if (out[i] > out[v] + w){
                out[i] = out[v] + w;
                pq.push({out[i], i});
            }
        }
    }
    
    int q; cin>>q;
    while (q--){
        int x; cin>>x;
        int ans = out[T.idx[x]];
        if (ans == inf){
            cout<<-1<<"\n";
            continue;
        }
        cout<<ans<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 693 ms 77040 KB Output is correct
5 Correct 283 ms 51768 KB Output is correct
6 Correct 156 ms 43296 KB Output is correct
7 Correct 226 ms 48892 KB Output is correct
8 Correct 104 ms 43528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 548 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Incorrect 0 ms 348 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 548 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Incorrect 0 ms 348 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 548 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Incorrect 0 ms 348 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 693 ms 77040 KB Output is correct
5 Correct 283 ms 51768 KB Output is correct
6 Correct 156 ms 43296 KB Output is correct
7 Correct 226 ms 48892 KB Output is correct
8 Correct 104 ms 43528 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 548 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Incorrect 0 ms 348 KB Output isn't correct
17 Halted 0 ms 0 KB -