답안 #961903

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961903 2024-04-12T17:34:06 Z MarwenElarbi Passport (JOI23_passport) C++17
100 / 100
276 ms 37880 KB
#include <bits/stdc++.h>
 
using namespace std;
 
struct Ticket {
    int c, p, a, b;
};
 
struct SegmentTree {
    int n, sz;
    vector<int> mx;
    vector<Ticket> tickets;
 
    SegmentTree(vector<Ticket> tickets) : tickets(tickets) {
        n = 1;
        sz = (int)tickets.size();
        while (n < sz) {
            n <<= 1;
        }
        mx.assign(2 * n, 0);
        for (int i = 0; i < n; ++i) {
            if (i < sz) {
                mx[i + n] = tickets[i].b;
            } else {
                mx[i + n] = -1;             
            }
        }
        for (int i = n - 1; i >= 1; --i) {
            pull(i);
        }
    }
 
    void pull(int i) {
        mx[i] = max(mx[2 * i], mx[2 * i + 1]);
    }
 
    void remove(vector<int>& v, int p, int i = 1, int l = 0, int r = -1) {
        if (r == -1) {
            r += n;
        }
        if (l >= sz || tickets[l].a > p || mx[i] < p) {
            return;
        } else if (l == r) {
            mx[i] = -1;
            v.push_back(l);
        } else {
            int m = (l + r) >> 1;
            remove(v, p, 2 * i, l, m);
            remove(v, p, 2 * i + 1, m + 1, r);
            pull(i);
        }
    }
};  
 
int main() {
    /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    const long long INF = 1e18;
    int n;
    cin >> n;
    int k=n;
    vector<Ticket> tickets(k);
    int cnt=0;
    for (auto& t : tickets) {
        cin >> t.a >> t.b;
        t.c=cnt;
        t.p=1;
        --t.a, --t.b;
        cnt++;
    }
    sort(tickets.begin(), tickets.end(), [](const auto& l, const auto& r) {
        return l.a < r.a;
    });
    auto dijkstra = [&](vector<long long> dist) {
        priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
        for (int i = n; i < n + k; ++i) {
            dist[tickets[i - n].c] = min(dist[tickets[i - n].c], dist[i] + tickets[i - n].p);
        }
        for (int i = 0; i < n; ++i) {
            if (dist[i] < INF) {
                pq.push({dist[i], i});
            }
        }
        SegmentTree seg(tickets);
        while (!pq.empty()) {
            auto x = pq.top();
            pq.pop();
            if (x.first > dist[x.second]) {
                continue;
            }
            vector<int> removed;
            seg.remove(removed, x.second);
            for (int r : removed) {
                if (dist[r + n] > x.first) {
                    dist[r + n] = x.first;
                    if (dist[tickets[r].c] > x.first + tickets[r].p) {
                        dist[tickets[r].c] = x.first + tickets[r].p;
                        pq.push({dist[tickets[r].c], tickets[r].c});
                    }
                }
            }
        }
        return dist;
    };
    vector<long long> start_left(n + k, INF);
    start_left[0] = 0;
    vector<long long> dist_left = dijkstra(start_left);
    vector<long long> start_right(n + k, INF);
    start_right[n - 1] = 0;
    vector<long long> dist_right = dijkstra(start_right);
    vector<long long> dist(n + k);
    for (int i = 0; i < n + k; ++i) {
        //cout <<i<<" "<<dist_left[i]<<" "<<dist_right[i]<<endl;
        dist[i] = dist_left[i] + dist_right[i];
    }
    dist = dijkstra(dist);
    int q;
    cin>>q;
    for (int i = 0; i < q; ++i) {
        int x;
        cin>>x;
        x--;
        if (dist[x] < INF) {
            cout << dist[x] << '\n';
        } else {
            cout << -1 << '\n';
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 233 ms 36128 KB Output is correct
5 Correct 181 ms 35940 KB Output is correct
6 Correct 181 ms 36364 KB Output is correct
7 Correct 105 ms 35992 KB Output is correct
8 Correct 60 ms 26008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 2 ms 752 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
22 Correct 1 ms 832 KB Output is correct
23 Correct 2 ms 848 KB Output is correct
24 Correct 2 ms 828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 2 ms 752 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
22 Correct 1 ms 832 KB Output is correct
23 Correct 2 ms 848 KB Output is correct
24 Correct 2 ms 828 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 3 ms 852 KB Output is correct
28 Correct 2 ms 860 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 2 ms 860 KB Output is correct
31 Correct 2 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 233 ms 36128 KB Output is correct
5 Correct 181 ms 35940 KB Output is correct
6 Correct 181 ms 36364 KB Output is correct
7 Correct 105 ms 35992 KB Output is correct
8 Correct 60 ms 26008 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 2 ms 856 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 2 ms 752 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 2 ms 860 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 1 ms 832 KB Output is correct
31 Correct 2 ms 848 KB Output is correct
32 Correct 2 ms 828 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 3 ms 852 KB Output is correct
36 Correct 2 ms 860 KB Output is correct
37 Correct 2 ms 860 KB Output is correct
38 Correct 2 ms 860 KB Output is correct
39 Correct 2 ms 852 KB Output is correct
40 Correct 276 ms 37880 KB Output is correct
41 Correct 215 ms 36896 KB Output is correct
42 Correct 194 ms 36964 KB Output is correct
43 Correct 172 ms 37144 KB Output is correct
44 Correct 180 ms 37252 KB Output is correct
45 Correct 170 ms 37032 KB Output is correct
46 Correct 82 ms 13796 KB Output is correct
47 Correct 187 ms 36084 KB Output is correct
48 Correct 149 ms 36828 KB Output is correct