Submission #924726

# Submission time Handle Problem Language Result Execution time Memory
924726 2024-02-09T14:57:04 Z a_l_i_r_e_z_a Toll (BOI17_toll) C++17
100 / 100
81 ms 63320 KB
// In the name of God
// Hope is last to die

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
// #define int long long    
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())

const int maxn = 2e5, lg = 16;
const int inf = 1e9 + 7, mod = 1e9 + 7;
int k, n, m, q;

struct D{
    int node[5];
    D(){
        for(int i = 0; i < 5; i++) node[i] = inf;
    }
} dp[maxn][lg];

D merge(D a, int u, int b){
    int aa[5];
    for(int i = 0; i < k; i++){
        int x = inf;
        for(int j = 0; j < k; j++) smin(x, a.node[j] + dp[u * k + j][b].node[i]);
        aa[i] = x;
    }
    for(int i = 0; i < k; i++) a.node[i] = aa[i];
    return a;
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> k >> n >> m >> q;
    for(int i = 0; i < m; i++){
        int u, v, w; cin >> u >> v >> w;
        dp[u][0].node[v % k] = w;
    }
    for(int i = n - 1; i >= 0; i--){
        for(int j = 1; j < lg; j++){
            dp[i][j] = merge(dp[i][j - 1], (i / k) + (1ll << (j - 1)), j - 1);
        }
    }
    while(q--){
        int u, v; cin >> u >> v;
        if(u / k >= v / k){
            cout << -1 << '\n';
            continue;
        }
        int x = u / k + 1, y = v / k;
        D ans = dp[u][0];
        for(int i = 0; i < lg; i++){
            if((y - x) & (1ll << i)){
                ans = merge(ans, x, i);
                x += 1ll << i;
            }
        }
        if(ans.node[v % k] >= inf) cout << -1 << '\n';
        else cout << ans.node[v % k] << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 54 ms 63060 KB Output is correct
2 Correct 13 ms 62916 KB Output is correct
3 Correct 12 ms 62888 KB Output is correct
4 Correct 12 ms 62876 KB Output is correct
5 Correct 13 ms 62916 KB Output is correct
6 Correct 13 ms 63068 KB Output is correct
7 Correct 12 ms 63064 KB Output is correct
8 Correct 53 ms 63068 KB Output is correct
9 Correct 45 ms 63064 KB Output is correct
10 Correct 36 ms 63068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 63068 KB Output is correct
2 Correct 12 ms 63068 KB Output is correct
3 Correct 12 ms 63068 KB Output is correct
4 Correct 12 ms 63064 KB Output is correct
5 Correct 12 ms 63068 KB Output is correct
6 Correct 13 ms 62908 KB Output is correct
7 Correct 15 ms 63064 KB Output is correct
8 Correct 16 ms 63068 KB Output is correct
9 Correct 44 ms 63068 KB Output is correct
10 Correct 77 ms 63064 KB Output is correct
11 Correct 67 ms 63064 KB Output is correct
12 Correct 72 ms 63064 KB Output is correct
13 Correct 63 ms 63056 KB Output is correct
14 Correct 49 ms 63068 KB Output is correct
15 Correct 52 ms 63064 KB Output is correct
16 Correct 58 ms 63068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 63068 KB Output is correct
2 Correct 12 ms 63068 KB Output is correct
3 Correct 12 ms 62860 KB Output is correct
4 Correct 12 ms 63064 KB Output is correct
5 Correct 12 ms 63068 KB Output is correct
6 Correct 13 ms 63068 KB Output is correct
7 Correct 14 ms 63064 KB Output is correct
8 Correct 13 ms 63068 KB Output is correct
9 Correct 13 ms 63068 KB Output is correct
10 Correct 41 ms 63068 KB Output is correct
11 Correct 53 ms 63068 KB Output is correct
12 Correct 62 ms 63064 KB Output is correct
13 Correct 69 ms 63064 KB Output is correct
14 Correct 62 ms 63064 KB Output is correct
15 Correct 47 ms 63068 KB Output is correct
16 Correct 50 ms 63064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 63068 KB Output is correct
2 Correct 12 ms 63068 KB Output is correct
3 Correct 12 ms 62860 KB Output is correct
4 Correct 12 ms 63064 KB Output is correct
5 Correct 12 ms 63068 KB Output is correct
6 Correct 13 ms 63068 KB Output is correct
7 Correct 14 ms 63064 KB Output is correct
8 Correct 13 ms 63068 KB Output is correct
9 Correct 13 ms 63068 KB Output is correct
10 Correct 41 ms 63068 KB Output is correct
11 Correct 53 ms 63068 KB Output is correct
12 Correct 62 ms 63064 KB Output is correct
13 Correct 69 ms 63064 KB Output is correct
14 Correct 62 ms 63064 KB Output is correct
15 Correct 47 ms 63068 KB Output is correct
16 Correct 50 ms 63064 KB Output is correct
17 Correct 56 ms 62916 KB Output is correct
18 Correct 12 ms 63064 KB Output is correct
19 Correct 12 ms 63064 KB Output is correct
20 Correct 12 ms 63064 KB Output is correct
21 Correct 12 ms 63068 KB Output is correct
22 Correct 12 ms 63076 KB Output is correct
23 Correct 13 ms 62924 KB Output is correct
24 Correct 14 ms 63068 KB Output is correct
25 Correct 14 ms 63320 KB Output is correct
26 Correct 16 ms 63068 KB Output is correct
27 Correct 43 ms 62912 KB Output is correct
28 Correct 64 ms 63064 KB Output is correct
29 Correct 67 ms 63068 KB Output is correct
30 Correct 65 ms 63064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 63060 KB Output is correct
2 Correct 13 ms 62916 KB Output is correct
3 Correct 12 ms 62888 KB Output is correct
4 Correct 12 ms 62876 KB Output is correct
5 Correct 13 ms 62916 KB Output is correct
6 Correct 13 ms 63068 KB Output is correct
7 Correct 12 ms 63064 KB Output is correct
8 Correct 53 ms 63068 KB Output is correct
9 Correct 45 ms 63064 KB Output is correct
10 Correct 36 ms 63068 KB Output is correct
11 Correct 56 ms 63068 KB Output is correct
12 Correct 12 ms 63068 KB Output is correct
13 Correct 12 ms 63068 KB Output is correct
14 Correct 12 ms 63064 KB Output is correct
15 Correct 12 ms 63068 KB Output is correct
16 Correct 13 ms 62908 KB Output is correct
17 Correct 15 ms 63064 KB Output is correct
18 Correct 16 ms 63068 KB Output is correct
19 Correct 44 ms 63068 KB Output is correct
20 Correct 77 ms 63064 KB Output is correct
21 Correct 67 ms 63064 KB Output is correct
22 Correct 72 ms 63064 KB Output is correct
23 Correct 63 ms 63056 KB Output is correct
24 Correct 49 ms 63068 KB Output is correct
25 Correct 52 ms 63064 KB Output is correct
26 Correct 58 ms 63068 KB Output is correct
27 Correct 12 ms 63068 KB Output is correct
28 Correct 12 ms 63068 KB Output is correct
29 Correct 12 ms 62860 KB Output is correct
30 Correct 12 ms 63064 KB Output is correct
31 Correct 12 ms 63068 KB Output is correct
32 Correct 13 ms 63068 KB Output is correct
33 Correct 14 ms 63064 KB Output is correct
34 Correct 13 ms 63068 KB Output is correct
35 Correct 13 ms 63068 KB Output is correct
36 Correct 41 ms 63068 KB Output is correct
37 Correct 53 ms 63068 KB Output is correct
38 Correct 62 ms 63064 KB Output is correct
39 Correct 69 ms 63064 KB Output is correct
40 Correct 62 ms 63064 KB Output is correct
41 Correct 47 ms 63068 KB Output is correct
42 Correct 50 ms 63064 KB Output is correct
43 Correct 56 ms 62916 KB Output is correct
44 Correct 12 ms 63064 KB Output is correct
45 Correct 12 ms 63064 KB Output is correct
46 Correct 12 ms 63064 KB Output is correct
47 Correct 12 ms 63068 KB Output is correct
48 Correct 12 ms 63076 KB Output is correct
49 Correct 13 ms 62924 KB Output is correct
50 Correct 14 ms 63068 KB Output is correct
51 Correct 14 ms 63320 KB Output is correct
52 Correct 16 ms 63068 KB Output is correct
53 Correct 43 ms 62912 KB Output is correct
54 Correct 64 ms 63064 KB Output is correct
55 Correct 67 ms 63068 KB Output is correct
56 Correct 65 ms 63064 KB Output is correct
57 Correct 81 ms 63068 KB Output is correct
58 Correct 48 ms 63068 KB Output is correct
59 Correct 66 ms 63068 KB Output is correct