Submission #368143

# Submission time Handle Problem Language Result Execution time Memory
368143 2021-02-19T16:16:41 Z Sorting Toll (BOI17_toll) C++17
100 / 100
160 ms 13036 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 5e4 + 3;
const int O = 1e4 + 3;
const int K = 5 + 3;
const int INF = 1e9;

void check_min(int &a, int b){ a = a < b ? a : b; }

int k, n, m, o;
vector<array<int, 3>> adj[N], rev_adj[N];
int ans[O], lef[N], rig[N];

void dnc(int l, int r, const vector<array<int, 3>> &queries){
    if(queries.empty()) return;
    if(l == r){
        for(auto [a, b, idx]: queries)
            ans[idx] = -1;
        return;
    }

    int mid = (l + r) >> 1;

    vector<array<int, 3>> todo[2];
    vector<array<int, 3>> curr;
    for(auto [a, b, i]: queries){
        if(a / k <= mid && mid <= b / k){
            curr.push_back({a, b, i});
            continue;
        }

        if(b / k < mid) todo[0].push_back({a, b, i});
        else todo[1].push_back({a, b, i});
    }

    for(int i = 0; i < k; ++i){
        for(int j = 0; j < k; ++j)
            lef[mid * k + j] = (j == i) ? 0 : INF;
        for(int j2 = mid - 1; j2 >= l; --j2){
            for(int j = 0; j < k; ++j)
                lef[j2 * k + j] = INF;
            for(auto [from, to, t]: rev_adj[j2 + 1])
                check_min(lef[to], lef[from] + t);
        }

        for(int j = 0; j < k; ++j)
            rig[mid * k + j] = (j == i) ? 0 : INF;
        for(int j2 = mid + 1; j2 <= r; ++j2){
            for(int j = 0; j < k; ++j)
                rig[j2 * k + j] = INF;
            for(auto [from, to, t]: adj[j2 - 1])
                check_min(rig[to], rig[from] + t);
        }

        for(auto [a, b, idx]: curr)
            check_min(ans[idx], lef[a] + rig[b]);
    }

    dnc(l, mid - 1, todo[0]), dnc(mid + 1, r, todo[1]);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> k >> n >> m >> o;
    for(int i = 0; i < m; ++i){
        int a, b, t;
        cin >> a >> b >> t;
        adj[a / k].push_back({a, b, t});
        rev_adj[b / k].push_back({b, a, t});
    }

    vector<array<int, 3>> queries;
    for(int i = 0; i < o; ++i){
        int a, b;
        cin >> a >> b;
        queries.push_back({a, b, i});
    }

    fill(ans, ans + o, INF);
    dnc(0, n / k, queries);

    for(int i = 0; i < o; ++i)
        if(ans[i] == INF)
            ans[i] = -1;

    for(int i = 0; i < o; ++i)
        cout << ans[i] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 58 ms 7612 KB Output is correct
2 Correct 2 ms 2780 KB Output is correct
3 Correct 2 ms 2800 KB Output is correct
4 Correct 3 ms 2668 KB Output is correct
5 Correct 4 ms 2796 KB Output is correct
6 Correct 3 ms 2816 KB Output is correct
7 Correct 4 ms 2796 KB Output is correct
8 Correct 43 ms 7568 KB Output is correct
9 Correct 41 ms 7344 KB Output is correct
10 Correct 7 ms 3628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 8340 KB Output is correct
2 Correct 3 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2768 KB Output is correct
7 Correct 7 ms 3276 KB Output is correct
8 Correct 7 ms 3396 KB Output is correct
9 Correct 40 ms 7472 KB Output is correct
10 Correct 86 ms 11056 KB Output is correct
11 Correct 79 ms 8496 KB Output is correct
12 Correct 65 ms 7852 KB Output is correct
13 Correct 105 ms 10476 KB Output is correct
14 Correct 81 ms 9068 KB Output is correct
15 Correct 48 ms 7020 KB Output is correct
16 Correct 106 ms 7020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2944 KB Output is correct
8 Correct 10 ms 2924 KB Output is correct
9 Correct 14 ms 2924 KB Output is correct
10 Correct 55 ms 7020 KB Output is correct
11 Correct 90 ms 7916 KB Output is correct
12 Correct 113 ms 10604 KB Output is correct
13 Correct 104 ms 12780 KB Output is correct
14 Correct 72 ms 9068 KB Output is correct
15 Correct 46 ms 6892 KB Output is correct
16 Correct 41 ms 6892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2944 KB Output is correct
8 Correct 10 ms 2924 KB Output is correct
9 Correct 14 ms 2924 KB Output is correct
10 Correct 55 ms 7020 KB Output is correct
11 Correct 90 ms 7916 KB Output is correct
12 Correct 113 ms 10604 KB Output is correct
13 Correct 104 ms 12780 KB Output is correct
14 Correct 72 ms 9068 KB Output is correct
15 Correct 46 ms 6892 KB Output is correct
16 Correct 41 ms 6892 KB Output is correct
17 Correct 60 ms 8044 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2672 KB Output is correct
20 Correct 2 ms 2668 KB Output is correct
21 Correct 2 ms 2668 KB Output is correct
22 Correct 2 ms 2668 KB Output is correct
23 Correct 4 ms 2944 KB Output is correct
24 Correct 4 ms 3052 KB Output is correct
25 Correct 6 ms 3052 KB Output is correct
26 Correct 5 ms 3052 KB Output is correct
27 Correct 49 ms 7148 KB Output is correct
28 Correct 90 ms 10860 KB Output is correct
29 Correct 107 ms 13036 KB Output is correct
30 Correct 82 ms 9208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 7612 KB Output is correct
2 Correct 2 ms 2780 KB Output is correct
3 Correct 2 ms 2800 KB Output is correct
4 Correct 3 ms 2668 KB Output is correct
5 Correct 4 ms 2796 KB Output is correct
6 Correct 3 ms 2816 KB Output is correct
7 Correct 4 ms 2796 KB Output is correct
8 Correct 43 ms 7568 KB Output is correct
9 Correct 41 ms 7344 KB Output is correct
10 Correct 7 ms 3628 KB Output is correct
11 Correct 73 ms 8340 KB Output is correct
12 Correct 3 ms 2668 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 2 ms 2688 KB Output is correct
16 Correct 2 ms 2768 KB Output is correct
17 Correct 7 ms 3276 KB Output is correct
18 Correct 7 ms 3396 KB Output is correct
19 Correct 40 ms 7472 KB Output is correct
20 Correct 86 ms 11056 KB Output is correct
21 Correct 79 ms 8496 KB Output is correct
22 Correct 65 ms 7852 KB Output is correct
23 Correct 105 ms 10476 KB Output is correct
24 Correct 81 ms 9068 KB Output is correct
25 Correct 48 ms 7020 KB Output is correct
26 Correct 106 ms 7020 KB Output is correct
27 Correct 3 ms 2668 KB Output is correct
28 Correct 2 ms 2668 KB Output is correct
29 Correct 2 ms 2668 KB Output is correct
30 Correct 2 ms 2668 KB Output is correct
31 Correct 2 ms 2668 KB Output is correct
32 Correct 3 ms 2796 KB Output is correct
33 Correct 3 ms 2944 KB Output is correct
34 Correct 10 ms 2924 KB Output is correct
35 Correct 14 ms 2924 KB Output is correct
36 Correct 55 ms 7020 KB Output is correct
37 Correct 90 ms 7916 KB Output is correct
38 Correct 113 ms 10604 KB Output is correct
39 Correct 104 ms 12780 KB Output is correct
40 Correct 72 ms 9068 KB Output is correct
41 Correct 46 ms 6892 KB Output is correct
42 Correct 41 ms 6892 KB Output is correct
43 Correct 60 ms 8044 KB Output is correct
44 Correct 2 ms 2668 KB Output is correct
45 Correct 2 ms 2672 KB Output is correct
46 Correct 2 ms 2668 KB Output is correct
47 Correct 2 ms 2668 KB Output is correct
48 Correct 2 ms 2668 KB Output is correct
49 Correct 4 ms 2944 KB Output is correct
50 Correct 4 ms 3052 KB Output is correct
51 Correct 6 ms 3052 KB Output is correct
52 Correct 5 ms 3052 KB Output is correct
53 Correct 49 ms 7148 KB Output is correct
54 Correct 90 ms 10860 KB Output is correct
55 Correct 107 ms 13036 KB Output is correct
56 Correct 82 ms 9208 KB Output is correct
57 Correct 160 ms 12460 KB Output is correct
58 Correct 68 ms 7512 KB Output is correct
59 Correct 80 ms 8524 KB Output is correct