답안 #1092321

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1092321 2024-09-23T19:10:32 Z DeathIsAwe Toll (BOI17_toll) C++17
100 / 100
167 ms 77136 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';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 1880 KB Output is correct
2 Correct 0 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 3 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 39 ms 1872 KB Output is correct
9 Correct 37 ms 1628 KB Output is correct
10 Correct 14 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 75348 KB Output is correct
2 Correct 0 ms 856 KB Output is correct
3 Correct 22 ms 73680 KB Output is correct
4 Correct 25 ms 73556 KB Output is correct
5 Correct 26 ms 73820 KB Output is correct
6 Correct 29 ms 73816 KB Output is correct
7 Correct 14 ms 1064 KB Output is correct
8 Correct 37 ms 73820 KB Output is correct
9 Correct 38 ms 1628 KB Output is correct
10 Correct 126 ms 76084 KB Output is correct
11 Correct 97 ms 75344 KB Output is correct
12 Correct 100 ms 74880 KB Output is correct
13 Correct 117 ms 76084 KB Output is correct
14 Correct 87 ms 75060 KB Output is correct
15 Correct 87 ms 74836 KB Output is correct
16 Correct 90 ms 74836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 26 ms 73700 KB Output is correct
3 Correct 25 ms 73552 KB Output is correct
4 Correct 29 ms 73728 KB Output is correct
5 Correct 32 ms 73556 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 26 ms 73644 KB Output is correct
8 Correct 33 ms 73844 KB Output is correct
9 Correct 31 ms 73816 KB Output is correct
10 Correct 26 ms 1708 KB Output is correct
11 Correct 97 ms 75096 KB Output is correct
12 Correct 119 ms 75828 KB Output is correct
13 Correct 125 ms 76116 KB Output is correct
14 Correct 99 ms 75552 KB Output is correct
15 Correct 91 ms 74836 KB Output is correct
16 Correct 87 ms 74832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 26 ms 73700 KB Output is correct
3 Correct 25 ms 73552 KB Output is correct
4 Correct 29 ms 73728 KB Output is correct
5 Correct 32 ms 73556 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 26 ms 73644 KB Output is correct
8 Correct 33 ms 73844 KB Output is correct
9 Correct 31 ms 73816 KB Output is correct
10 Correct 26 ms 1708 KB Output is correct
11 Correct 97 ms 75096 KB Output is correct
12 Correct 119 ms 75828 KB Output is correct
13 Correct 125 ms 76116 KB Output is correct
14 Correct 99 ms 75552 KB Output is correct
15 Correct 91 ms 74836 KB Output is correct
16 Correct 87 ms 74832 KB Output is correct
17 Correct 89 ms 75344 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 24 ms 73820 KB Output is correct
20 Correct 25 ms 73552 KB Output is correct
21 Correct 34 ms 73556 KB Output is correct
22 Correct 31 ms 73628 KB Output is correct
23 Correct 5 ms 856 KB Output is correct
24 Correct 27 ms 74072 KB Output is correct
25 Correct 47 ms 73728 KB Output is correct
26 Correct 32 ms 73816 KB Output is correct
27 Correct 40 ms 1624 KB Output is correct
28 Correct 113 ms 75860 KB Output is correct
29 Correct 124 ms 76128 KB Output is correct
30 Correct 102 ms 75444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 1880 KB Output is correct
2 Correct 0 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 3 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 39 ms 1872 KB Output is correct
9 Correct 37 ms 1628 KB Output is correct
10 Correct 14 ms 980 KB Output is correct
11 Correct 95 ms 75348 KB Output is correct
12 Correct 0 ms 856 KB Output is correct
13 Correct 22 ms 73680 KB Output is correct
14 Correct 25 ms 73556 KB Output is correct
15 Correct 26 ms 73820 KB Output is correct
16 Correct 29 ms 73816 KB Output is correct
17 Correct 14 ms 1064 KB Output is correct
18 Correct 37 ms 73820 KB Output is correct
19 Correct 38 ms 1628 KB Output is correct
20 Correct 126 ms 76084 KB Output is correct
21 Correct 97 ms 75344 KB Output is correct
22 Correct 100 ms 74880 KB Output is correct
23 Correct 117 ms 76084 KB Output is correct
24 Correct 87 ms 75060 KB Output is correct
25 Correct 87 ms 74836 KB Output is correct
26 Correct 90 ms 74836 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 26 ms 73700 KB Output is correct
29 Correct 25 ms 73552 KB Output is correct
30 Correct 29 ms 73728 KB Output is correct
31 Correct 32 ms 73556 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 26 ms 73644 KB Output is correct
34 Correct 33 ms 73844 KB Output is correct
35 Correct 31 ms 73816 KB Output is correct
36 Correct 26 ms 1708 KB Output is correct
37 Correct 97 ms 75096 KB Output is correct
38 Correct 119 ms 75828 KB Output is correct
39 Correct 125 ms 76116 KB Output is correct
40 Correct 99 ms 75552 KB Output is correct
41 Correct 91 ms 74836 KB Output is correct
42 Correct 87 ms 74832 KB Output is correct
43 Correct 89 ms 75344 KB Output is correct
44 Correct 1 ms 860 KB Output is correct
45 Correct 24 ms 73820 KB Output is correct
46 Correct 25 ms 73552 KB Output is correct
47 Correct 34 ms 73556 KB Output is correct
48 Correct 31 ms 73628 KB Output is correct
49 Correct 5 ms 856 KB Output is correct
50 Correct 27 ms 74072 KB Output is correct
51 Correct 47 ms 73728 KB Output is correct
52 Correct 32 ms 73816 KB Output is correct
53 Correct 40 ms 1624 KB Output is correct
54 Correct 113 ms 75860 KB Output is correct
55 Correct 124 ms 76128 KB Output is correct
56 Correct 102 ms 75444 KB Output is correct
57 Correct 167 ms 77136 KB Output is correct
58 Correct 39 ms 1876 KB Output is correct
59 Correct 97 ms 75464 KB Output is correct