Submission #1092009

# Submission time Handle Problem Language Result Execution time Memory
1092009 2024-09-22T20:25:03 Z raphaelp Toll (BOI17_toll) C++14
100 / 100
191 ms 15704 KB
#include <bits/stdc++.h>
using namespace std;
long long l(long long x) { return (x << 1); }
long long r(long long x) { return (x << 1) + 1; }
void setup(long long L, long long R, vector<vector<vector<long long>>> &dp, long long x, long long K)
{
    if (L == R)
        return;
    long long m = (L + R) / 2;
    setup(L, m, dp, l(x), K);
    setup(m + 1, R, dp, r(x), K);
    for (long long i = 0; i < K; i++)
    {
        for (long long j = 0; j < K; j++)
        {
            for (long long k = 0; k < K; k++)
            {
                dp[x][i][j] = min(dp[x][i][j], dp[l(x)][i][k] + dp[r(x)][k][j]);
            }
        }
    }
}
vector<vector<long long>> query(long long L, long long R, long long a, long long b, long long x, long long K, vector<vector<vector<long long>>> &dp)
{
    vector<vector<long long>> ans;
    if (L > b || R < a)
        return ans;
    ans.assign(K, vector<long long>(K, 1000000000000));
    if (L >= a && R <= b)
        return dp[x];
    long long m = (L + R) / 2;
    vector<vector<long long>> lef = query(L, m, a, b, l(x), K, dp);
    vector<vector<long long>> right = query(m + 1, R, a, b, r(x), K, dp);
    if (lef.size() == 0)
        ans = right;
    else if (right.size() == 0)
        ans = lef;
    else
        for (long long i = 0; i < K; i++)
        {
            for (long long j = 0; j < K; j++)
            {
                for (long long k = 0; k < K; k++)
                {
                    ans[i][j] = min(ans[i][j], lef[i][k] + right[k][j]);
                }
            }
        }
    return ans;
}
int main()
{
    long long N, M, K, O;
    cin >> K >> N >> M >> O;
    long long N2 = (1 << (long long)ceil(log2(ceil((double)N / K))));
    vector<vector<vector<long long>>> dp(2 * N2, vector<vector<long long>>(K, vector<long long>(K, 1000000000000)));
    for (long long i = 0; i < M; i++)
    {
        long long a, b, t;
        cin >> a >> b >> t;
        dp[N2 + a / K][a % K][b % K] = t;
    }
    setup(0, N2 - 1, dp, 1, K);
    for (long long i = 0; i < O; i++)
    {
        long long a, b;
        cin >> a >> b;
        if (a / K == b / K)
        {
            cout << -1 << '\n';
            continue;
        }
        vector<vector<long long>> temp = query(0, N2 - 1, a / K, b / K - 1, 1, K, dp);
        cout << ((temp[a % K][b % K] == 1000000000000) ? -1 : temp[a % K][b % K]) << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 81 ms 12624 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 604 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 118 ms 12636 KB Output is correct
9 Correct 79 ms 12380 KB Output is correct
10 Correct 57 ms 11868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 11748 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 25 ms 664 KB Output is correct
8 Correct 28 ms 600 KB Output is correct
9 Correct 73 ms 12628 KB Output is correct
10 Correct 119 ms 15540 KB Output is correct
11 Correct 94 ms 11860 KB Output is correct
12 Correct 90 ms 14416 KB Output is correct
13 Correct 102 ms 9128 KB Output is correct
14 Correct 61 ms 8272 KB Output is correct
15 Correct 56 ms 7864 KB Output is correct
16 Correct 55 ms 7764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 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 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 3 ms 452 KB Output is correct
9 Correct 2 ms 600 KB Output is correct
10 Correct 45 ms 12476 KB Output is correct
11 Correct 56 ms 11600 KB Output is correct
12 Correct 82 ms 15424 KB Output is correct
13 Correct 97 ms 15548 KB Output is correct
14 Correct 75 ms 14932 KB Output is correct
15 Correct 43 ms 7760 KB Output is correct
16 Correct 44 ms 7760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 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 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 3 ms 452 KB Output is correct
9 Correct 2 ms 600 KB Output is correct
10 Correct 45 ms 12476 KB Output is correct
11 Correct 56 ms 11600 KB Output is correct
12 Correct 82 ms 15424 KB Output is correct
13 Correct 97 ms 15548 KB Output is correct
14 Correct 75 ms 14932 KB Output is correct
15 Correct 43 ms 7760 KB Output is correct
16 Correct 44 ms 7760 KB Output is correct
17 Correct 81 ms 11604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 436 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 10 ms 604 KB Output is correct
24 Correct 12 ms 600 KB Output is correct
25 Correct 17 ms 604 KB Output is correct
26 Correct 15 ms 604 KB Output is correct
27 Correct 54 ms 12352 KB Output is correct
28 Correct 106 ms 15444 KB Output is correct
29 Correct 142 ms 15704 KB Output is correct
30 Correct 85 ms 15036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 12624 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 604 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 118 ms 12636 KB Output is correct
9 Correct 79 ms 12380 KB Output is correct
10 Correct 57 ms 11868 KB Output is correct
11 Correct 89 ms 11748 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 25 ms 664 KB Output is correct
18 Correct 28 ms 600 KB Output is correct
19 Correct 73 ms 12628 KB Output is correct
20 Correct 119 ms 15540 KB Output is correct
21 Correct 94 ms 11860 KB Output is correct
22 Correct 90 ms 14416 KB Output is correct
23 Correct 102 ms 9128 KB Output is correct
24 Correct 61 ms 8272 KB Output is correct
25 Correct 56 ms 7864 KB Output is correct
26 Correct 55 ms 7764 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 3 ms 452 KB Output is correct
35 Correct 2 ms 600 KB Output is correct
36 Correct 45 ms 12476 KB Output is correct
37 Correct 56 ms 11600 KB Output is correct
38 Correct 82 ms 15424 KB Output is correct
39 Correct 97 ms 15548 KB Output is correct
40 Correct 75 ms 14932 KB Output is correct
41 Correct 43 ms 7760 KB Output is correct
42 Correct 44 ms 7760 KB Output is correct
43 Correct 81 ms 11604 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 436 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 10 ms 604 KB Output is correct
50 Correct 12 ms 600 KB Output is correct
51 Correct 17 ms 604 KB Output is correct
52 Correct 15 ms 604 KB Output is correct
53 Correct 54 ms 12352 KB Output is correct
54 Correct 106 ms 15444 KB Output is correct
55 Correct 142 ms 15704 KB Output is correct
56 Correct 85 ms 15036 KB Output is correct
57 Correct 191 ms 14276 KB Output is correct
58 Correct 79 ms 12624 KB Output is correct
59 Correct 116 ms 11860 KB Output is correct