Submission #753626

# Submission time Handle Problem Language Result Execution time Memory
753626 2023-06-05T14:56:41 Z hafo Toll (BOI17_toll) C++14
100 / 100
126 ms 157800 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 16;
const int maxn = 5e4 + 7;
const ll oo = 1e17 + 69;

int k, n, q, m, u, v, w;

struct matrix{
    ll a[5][5];

    void init() {
        for(int i = 0; i < k; i++)
            for(int j = 0; j < k; j++) a[i][j] = oo;
    }

    friend matrix operator * (matrix a, matrix b) {
        matrix c;
        c.init();
        for(int i = 0; i < k; i++) {
            for(int j = 0; j < k; j++) {
                for(int h = 0; h < k; h++) {
                    mini(c.a[i][j], a.a[i][h] + b.a[h][j]);
                }
            }
        }
        return c;
    }
};

matrix st[LOG][maxn];

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

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>k>>n>>m>>q;
    for(int i = 0; i < LOG; i++)
        for(int j = 0; j < n / k; j++) st[i][j].init();
    while(m--) {
        cin>>u>>v>>w;
        st[0][u / k].a[u % k][v % k] = w;
    }

    for(int i = 1; i < LOG; i++) {
        for(int u = 0; u + (1 << i) - 1 <= (n - 1) / k; u++) {
            st[i][u] = st[i - 1][u] * st[i - 1][u + (1 << (i - 1))];
        }
    }

    while(q--) {
        cin>>u>>v;
        if(u / k >= v / k) {
            cout<<-1<<"\n";
            continue;
        }
        matrix ans;
        for(int i = 0; i < k; i++)
            for(int j = 0; j < k; j++) ans.a[i][j] = (i == j ? 0:oo);
        int len = v / k - u / k;
        int s = u / k;
        for(int i = 0; i < LOG; i++)
        if((len >> i) & 1) {
            matrix cur;
            cur.init();
            cur = ans * st[i][s];
            ans = cur;
            s += (1 << i);
        }
        if(ans.a[u % k][v % k] == oo) cout<<-1;
        else cout<<ans.a[u % k][v % k];
        cout<<"\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 110 ms 156920 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 3540 KB Output is correct
6 Correct 3 ms 3540 KB Output is correct
7 Correct 3 ms 3540 KB Output is correct
8 Correct 126 ms 156872 KB Output is correct
9 Correct 124 ms 156860 KB Output is correct
10 Correct 109 ms 156880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 78692 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 7 ms 3680 KB Output is correct
8 Correct 7 ms 2132 KB Output is correct
9 Correct 108 ms 157800 KB Output is correct
10 Correct 89 ms 54936 KB Output is correct
11 Correct 100 ms 80488 KB Output is correct
12 Correct 72 ms 53900 KB Output is correct
13 Correct 77 ms 21608 KB Output is correct
14 Correct 54 ms 33180 KB Output is correct
15 Correct 53 ms 20388 KB Output is correct
16 Correct 53 ms 20428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 3 ms 3540 KB Output is correct
7 Correct 2 ms 2004 KB Output is correct
8 Correct 2 ms 1132 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 107 ms 157688 KB Output is correct
11 Correct 81 ms 80200 KB Output is correct
12 Correct 72 ms 54732 KB Output is correct
13 Correct 85 ms 55032 KB Output is correct
14 Correct 67 ms 54400 KB Output is correct
15 Correct 49 ms 20380 KB Output is correct
16 Correct 56 ms 20416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 3 ms 3540 KB Output is correct
7 Correct 2 ms 2004 KB Output is correct
8 Correct 2 ms 1132 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 107 ms 157688 KB Output is correct
11 Correct 81 ms 80200 KB Output is correct
12 Correct 72 ms 54732 KB Output is correct
13 Correct 85 ms 55032 KB Output is correct
14 Correct 67 ms 54400 KB Output is correct
15 Correct 49 ms 20380 KB Output is correct
16 Correct 56 ms 20416 KB Output is correct
17 Correct 73 ms 80268 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 320 KB Output is correct
23 Correct 4 ms 3540 KB Output is correct
24 Correct 4 ms 2004 KB Output is correct
25 Correct 6 ms 1108 KB Output is correct
26 Correct 5 ms 1236 KB Output is correct
27 Correct 101 ms 157620 KB Output is correct
28 Correct 75 ms 54776 KB Output is correct
29 Correct 79 ms 55040 KB Output is correct
30 Correct 75 ms 54456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 156920 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 3540 KB Output is correct
6 Correct 3 ms 3540 KB Output is correct
7 Correct 3 ms 3540 KB Output is correct
8 Correct 126 ms 156872 KB Output is correct
9 Correct 124 ms 156860 KB Output is correct
10 Correct 109 ms 156880 KB Output is correct
11 Correct 86 ms 78692 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 7 ms 3680 KB Output is correct
18 Correct 7 ms 2132 KB Output is correct
19 Correct 108 ms 157800 KB Output is correct
20 Correct 89 ms 54936 KB Output is correct
21 Correct 100 ms 80488 KB Output is correct
22 Correct 72 ms 53900 KB Output is correct
23 Correct 77 ms 21608 KB Output is correct
24 Correct 54 ms 33180 KB Output is correct
25 Correct 53 ms 20388 KB Output is correct
26 Correct 53 ms 20428 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 3 ms 3540 KB Output is correct
33 Correct 2 ms 2004 KB Output is correct
34 Correct 2 ms 1132 KB Output is correct
35 Correct 2 ms 1236 KB Output is correct
36 Correct 107 ms 157688 KB Output is correct
37 Correct 81 ms 80200 KB Output is correct
38 Correct 72 ms 54732 KB Output is correct
39 Correct 85 ms 55032 KB Output is correct
40 Correct 67 ms 54400 KB Output is correct
41 Correct 49 ms 20380 KB Output is correct
42 Correct 56 ms 20416 KB Output is correct
43 Correct 73 ms 80268 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 324 KB Output is correct
48 Correct 1 ms 320 KB Output is correct
49 Correct 4 ms 3540 KB Output is correct
50 Correct 4 ms 2004 KB Output is correct
51 Correct 6 ms 1108 KB Output is correct
52 Correct 5 ms 1236 KB Output is correct
53 Correct 101 ms 157620 KB Output is correct
54 Correct 75 ms 54776 KB Output is correct
55 Correct 79 ms 55040 KB Output is correct
56 Correct 75 ms 54456 KB Output is correct
57 Correct 113 ms 42876 KB Output is correct
58 Correct 111 ms 157756 KB Output is correct
59 Correct 87 ms 80524 KB Output is correct