Submission #1092322

# Submission time Handle Problem Language Result Execution time Memory
1092322 2024-09-23T19:11:08 Z DeathIsAwe Toll (BOI17_toll) C++17
100 / 100
194 ms 75600 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
int segment[131072];
const int maxi = 1000000000;
int graph[50000][5][15][5];


void add(int pos, int a) {
    pos += 65536;
    segment[pos] = a;
    pos /= 2;
    while (pos > 0) {
        if (segment[2*pos + 1] == -1 || segment[2*pos] == -1) {
            segment[pos] = -1;
        } else {
            segment[pos] = segment[2*pos + 1] + segment[2*pos];
        }
        pos /= 2;
    }
}


int find(int a, int b) {
    a += 65536; b += 65536 - 1;
    int val = 0;
    while (a <= b) {
        if (a % 2 == 1) {
            if (segment[a] == -1) {
                val = -1; break;
            }
            val += segment[a++];
        }
        if (b % 2 == 0) {
            if (segment[b] == -1) {
                val = -1; break;
            }
            val += segment[b--];
        }
        a /= 2; b /= 2;
    }
    if (val == 0) {
        return -1;
    }
    return val;
}


int main() {
    int k, n, m, o, d1, d2, t; cin >> k >> n >> m >> o;
    if (k == 1) {
        for (int i=0;i<131072;i++) {
            segment[i] = -1;
        }
        for (int i=0;i<m;i++) {
            cin >> d1 >> d2 >> t;
            add(d1, t);
        }
        for (int i=0;i<o;i++) {
            cin >> d1 >> d2;
            cout << find(d1, d2) << '\n';
        }
        return 0;
    }
    int asdf = (n + k - 1) / k;
    for (int i=0;i<50000;i++) {
        for (int j=0;j<k;j++) {
            for (int x=0;x<15;x++) {
                for (int y=0;y<k;y++) {
                    graph[i][j][x][y] = maxi;
                }
            }
        }
    }
    for (int i=0;i<m;i++) {
        cin >> d1 >> d2 >> t;
        graph[d1/k][d1%k][0][d2%k] = t;
    }
    int add = 1;
    for (int i=1;i<15;i++) {
        for (int j=0;j<asdf;j++) {
            for (int x=0;x<k;x++) {
                for (int y=0;y<k;y++) {
                    for (int z=0;z<k;z++) {
                        graph[j][x][i][y] = min(graph[j][x][i][y], graph[j][x][i-1][z] + graph[j + add][z][i-1][y]);
                        if (graph[j][x][i][y] > maxi) {
                            graph[j][x][i][y] = maxi;
                        }
                    }
                }
            }
        }
        add *= 2;
    }


    int ans[5], newans[5], pow2, pow2val, diff, amodk, adivk, bmodk, bdivk;
    for (int i=0;i<o;i++) {
        cin >> d1 >> d2;
        amodk = d1 % k; adivk = d1 / k;
        bmodk = d2 % k; bdivk = d2 / k;
        pow2val = 16384; pow2 = 14;
        diff = bdivk - adivk;
        if (diff <= 0) {
            cout << -1 << '\n'; continue;
        }
        while (true) {
            if (diff < pow2val) {
                pow2val /= 2;
                pow2 -= 1;
                continue;
            }
            diff -= pow2val;
            //cout << pow2 << '\n';
            for (int j=0;j<k;j++) {
                ans[j] = graph[adivk][amodk][pow2][j];
            }
            adivk += pow2val;
            pow2val /= 2;
            pow2 -= 1;
            break;
        }
        //cout << ans[0] << ' ' << ans[2] << ' ' << adivk << ' ' << pow2 << '\n';


        while (diff > 0) {
            if (diff < pow2val) {
                pow2val /= 2;
                pow2 -= 1;
                continue;
            }
            diff -= pow2val;
            for (int j=0;j<k;j++) {
                newans[j] = maxi;
                for (int x=0; x<k;x++) {
                    if (graph[adivk][x][pow2][j] < maxi && ans[x] < maxi) {
                        newans[j] = min(newans[j], graph[adivk][x][pow2][j] + ans[x]);
                    }
                }
            }
            for (int j=0;j<k;j++) {
                ans[j] = newans[j];
            }
            adivk += pow2val;
            pow2val /= 2;
            pow2 -= 1;
        }
        if (ans[bmodk] == maxi) {
            cout << -1 << '\n'; continue;
        }
        cout << ans[bmodk] << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 40 ms 1008 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 0 ms 860 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 38 ms 984 KB Output is correct
9 Correct 36 ms 944 KB Output is correct
10 Correct 14 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 73808 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 23 ms 73764 KB Output is correct
4 Correct 25 ms 73820 KB Output is correct
5 Correct 33 ms 73552 KB Output is correct
6 Correct 29 ms 73748 KB Output is correct
7 Correct 14 ms 860 KB Output is correct
8 Correct 39 ms 73676 KB Output is correct
9 Correct 38 ms 860 KB Output is correct
10 Correct 136 ms 73812 KB Output is correct
11 Correct 98 ms 73808 KB Output is correct
12 Correct 100 ms 73792 KB Output is correct
13 Correct 126 ms 73796 KB Output is correct
14 Correct 83 ms 73796 KB Output is correct
15 Correct 92 ms 73808 KB Output is correct
16 Correct 97 ms 73820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 22 ms 73708 KB Output is correct
3 Correct 23 ms 73820 KB Output is correct
4 Correct 26 ms 73820 KB Output is correct
5 Correct 28 ms 73820 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 23 ms 73564 KB Output is correct
8 Correct 31 ms 73816 KB Output is correct
9 Correct 28 ms 73556 KB Output is correct
10 Correct 24 ms 856 KB Output is correct
11 Correct 90 ms 73792 KB Output is correct
12 Correct 112 ms 73584 KB Output is correct
13 Correct 131 ms 73556 KB Output is correct
14 Correct 102 ms 73552 KB Output is correct
15 Correct 99 ms 73808 KB Output is correct
16 Correct 90 ms 73552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 22 ms 73708 KB Output is correct
3 Correct 23 ms 73820 KB Output is correct
4 Correct 26 ms 73820 KB Output is correct
5 Correct 28 ms 73820 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 23 ms 73564 KB Output is correct
8 Correct 31 ms 73816 KB Output is correct
9 Correct 28 ms 73556 KB Output is correct
10 Correct 24 ms 856 KB Output is correct
11 Correct 90 ms 73792 KB Output is correct
12 Correct 112 ms 73584 KB Output is correct
13 Correct 131 ms 73556 KB Output is correct
14 Correct 102 ms 73552 KB Output is correct
15 Correct 99 ms 73808 KB Output is correct
16 Correct 90 ms 73552 KB Output is correct
17 Correct 92 ms 73812 KB Output is correct
18 Correct 0 ms 856 KB Output is correct
19 Correct 25 ms 73820 KB Output is correct
20 Correct 25 ms 73820 KB Output is correct
21 Correct 27 ms 73552 KB Output is correct
22 Correct 37 ms 73636 KB Output is correct
23 Correct 5 ms 856 KB Output is correct
24 Correct 29 ms 73820 KB Output is correct
25 Correct 39 ms 73688 KB Output is correct
26 Correct 34 ms 73820 KB Output is correct
27 Correct 28 ms 1700 KB Output is correct
28 Correct 134 ms 75092 KB Output is correct
29 Correct 131 ms 75204 KB Output is correct
30 Correct 118 ms 74940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 1008 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 0 ms 860 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 38 ms 984 KB Output is correct
9 Correct 36 ms 944 KB Output is correct
10 Correct 14 ms 860 KB Output is correct
11 Correct 104 ms 73808 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 23 ms 73764 KB Output is correct
14 Correct 25 ms 73820 KB Output is correct
15 Correct 33 ms 73552 KB Output is correct
16 Correct 29 ms 73748 KB Output is correct
17 Correct 14 ms 860 KB Output is correct
18 Correct 39 ms 73676 KB Output is correct
19 Correct 38 ms 860 KB Output is correct
20 Correct 136 ms 73812 KB Output is correct
21 Correct 98 ms 73808 KB Output is correct
22 Correct 100 ms 73792 KB Output is correct
23 Correct 126 ms 73796 KB Output is correct
24 Correct 83 ms 73796 KB Output is correct
25 Correct 92 ms 73808 KB Output is correct
26 Correct 97 ms 73820 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 22 ms 73708 KB Output is correct
29 Correct 23 ms 73820 KB Output is correct
30 Correct 26 ms 73820 KB Output is correct
31 Correct 28 ms 73820 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 23 ms 73564 KB Output is correct
34 Correct 31 ms 73816 KB Output is correct
35 Correct 28 ms 73556 KB Output is correct
36 Correct 24 ms 856 KB Output is correct
37 Correct 90 ms 73792 KB Output is correct
38 Correct 112 ms 73584 KB Output is correct
39 Correct 131 ms 73556 KB Output is correct
40 Correct 102 ms 73552 KB Output is correct
41 Correct 99 ms 73808 KB Output is correct
42 Correct 90 ms 73552 KB Output is correct
43 Correct 92 ms 73812 KB Output is correct
44 Correct 0 ms 856 KB Output is correct
45 Correct 25 ms 73820 KB Output is correct
46 Correct 25 ms 73820 KB Output is correct
47 Correct 27 ms 73552 KB Output is correct
48 Correct 37 ms 73636 KB Output is correct
49 Correct 5 ms 856 KB Output is correct
50 Correct 29 ms 73820 KB Output is correct
51 Correct 39 ms 73688 KB Output is correct
52 Correct 34 ms 73820 KB Output is correct
53 Correct 28 ms 1700 KB Output is correct
54 Correct 134 ms 75092 KB Output is correct
55 Correct 131 ms 75204 KB Output is correct
56 Correct 118 ms 74940 KB Output is correct
57 Correct 194 ms 75600 KB Output is correct
58 Correct 39 ms 1624 KB Output is correct
59 Correct 107 ms 74896 KB Output is correct