Submission #747359

# Submission time Handle Problem Language Result Execution time Memory
747359 2023-05-24T05:56:43 Z GrindMachine Toll (BOI17_toll) C++17
100 / 100
76 ms 18772 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
official sol
https://github.com/galletas1712/CompetitiveProgramming/blob/master/Olympiad/Baltic/Baltic17-toll.cpp


queries => think if we can force segtree
to use segtree, we should be able to merge the left and right nodes efficiently
what info do we require to merge?

k is unusually small

we can look at the n points on a number line split into consecutive blocks of size = k

find min cost to go from block1[x] to block2[y]

if we have the min cost to go from left_most_block[i] to right_most_block[j] in the form of cost[i][j] for the left and right nodes, can we merge efficiently?

yes, we can merge in matmul style

build a segtree of matrices of dimensions k*k and use it to answer queries

(dnc prolly works faster than segtree)

*/

const int MOD = 1e9 + 7;
const int N = 5e4 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
const int K = 5;

int adj[N][K][K];

template<typename T>
struct segtree {
    // https://codeforces.com/blog/entry/18051

    /*=======================================================*/

    struct data {
        int a[K][K];
        bool active;

        data(){
            memset(a,0x3f,sizeof a);
            active = false;
        }
    };

    data neutral = data();

    data merge(data &left, data &right) {
        if(!left.active) return right;
        if(!right.active) return left;

        data curr;

        rep(x,K){
            rep(y,K){
                rep(z,K){
                    amin(curr.a[x][z], left.a[x][y] + right.a[y][z]);
                }
            }
        }

        curr.active = 1;

        return curr;
    }

    void create(int i) {
        int block = i-n;

        rep(x,K){
            rep(y,K){
                tr[i].a[x][y] = adj[block][x][y];
            }
        }

        tr[i].active = 1;
    }

    void modify(int i, T v) {

    }

    /*=======================================================*/

    int n;
    vector<data> tr;

    segtree() {

    }

    segtree(int siz) {
        init(siz);
    }

    void init(int siz) {
        n = siz;
        tr.assign(2 * n, neutral);
    }

    void build(int siz) {
        rep(i, siz) create(i + n);
        rev(i, n - 1, 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
    }

    void pupd(int i, T v) {
        modify(i + n, v);
        for (i = (i + n) >> 1; i; i >>= 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
    }

    data query(int l, int r) {
        data resl = neutral, resr = neutral;

        for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) resl = merge(resl, tr[l++]);
            if (!(r & 1)) resr = merge(tr[r--], resr);
        }

        return merge(resl, resr);
    }
};

void solve(int test_case)
{
    int k,n,m,q; cin >> k >> n >> m >> q;
    memset(adj,0x3f,sizeof adj);

    rep(i,m){
        int u,v,w; cin >> u >> v >> w;
        adj[u/k][u%k][v%k] = w;
    }

    segtree<int> st(n+5);
    st.build(n/k+1);

    while(q--){
        int u,v; cin >> u >> v;
        int b1 = u/k, b2 = v/k;
        int ans = -1;

        if(b1 != b2) {
            ans = st.query(b1,b2-1).a[u%k][v%k];
            if(ans >= inf1){
                ans = -1;
            }
        }

        cout << ans << endl;
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 54 ms 15456 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5328 KB Output is correct
7 Correct 5 ms 5332 KB Output is correct
8 Correct 54 ms 16332 KB Output is correct
9 Correct 53 ms 16268 KB Output is correct
10 Correct 40 ms 15512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 15444 KB Output is correct
2 Correct 2 ms 5204 KB Output is correct
3 Correct 3 ms 5216 KB Output is correct
4 Correct 2 ms 5192 KB Output is correct
5 Correct 2 ms 5204 KB Output is correct
6 Correct 2 ms 5204 KB Output is correct
7 Correct 16 ms 5528 KB Output is correct
8 Correct 15 ms 5520 KB Output is correct
9 Correct 53 ms 16284 KB Output is correct
10 Correct 67 ms 17608 KB Output is correct
11 Correct 61 ms 17128 KB Output is correct
12 Correct 52 ms 16700 KB Output is correct
13 Correct 45 ms 13640 KB Output is correct
14 Correct 35 ms 12748 KB Output is correct
15 Correct 29 ms 12536 KB Output is correct
16 Correct 30 ms 12488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5204 KB Output is correct
2 Correct 2 ms 5204 KB Output is correct
3 Correct 2 ms 5204 KB Output is correct
4 Correct 2 ms 5204 KB Output is correct
5 Correct 2 ms 5204 KB Output is correct
6 Correct 3 ms 5332 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 4 ms 5456 KB Output is correct
10 Correct 30 ms 16140 KB Output is correct
11 Correct 36 ms 16844 KB Output is correct
12 Correct 45 ms 17484 KB Output is correct
13 Correct 48 ms 17740 KB Output is correct
14 Correct 37 ms 17096 KB Output is correct
15 Correct 24 ms 12500 KB Output is correct
16 Correct 23 ms 12492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5204 KB Output is correct
2 Correct 2 ms 5204 KB Output is correct
3 Correct 2 ms 5204 KB Output is correct
4 Correct 2 ms 5204 KB Output is correct
5 Correct 2 ms 5204 KB Output is correct
6 Correct 3 ms 5332 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 4 ms 5456 KB Output is correct
10 Correct 30 ms 16140 KB Output is correct
11 Correct 36 ms 16844 KB Output is correct
12 Correct 45 ms 17484 KB Output is correct
13 Correct 48 ms 17740 KB Output is correct
14 Correct 37 ms 17096 KB Output is correct
15 Correct 24 ms 12500 KB Output is correct
16 Correct 23 ms 12492 KB Output is correct
17 Correct 42 ms 16872 KB Output is correct
18 Correct 2 ms 5204 KB Output is correct
19 Correct 2 ms 5204 KB Output is correct
20 Correct 3 ms 5204 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 2 ms 5204 KB Output is correct
23 Correct 7 ms 5460 KB Output is correct
24 Correct 7 ms 5460 KB Output is correct
25 Correct 6 ms 5460 KB Output is correct
26 Correct 7 ms 5460 KB Output is correct
27 Correct 36 ms 16168 KB Output is correct
28 Correct 48 ms 17552 KB Output is correct
29 Correct 52 ms 17868 KB Output is correct
30 Correct 41 ms 17236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 15456 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5328 KB Output is correct
7 Correct 5 ms 5332 KB Output is correct
8 Correct 54 ms 16332 KB Output is correct
9 Correct 53 ms 16268 KB Output is correct
10 Correct 40 ms 15512 KB Output is correct
11 Correct 55 ms 15444 KB Output is correct
12 Correct 2 ms 5204 KB Output is correct
13 Correct 3 ms 5216 KB Output is correct
14 Correct 2 ms 5192 KB Output is correct
15 Correct 2 ms 5204 KB Output is correct
16 Correct 2 ms 5204 KB Output is correct
17 Correct 16 ms 5528 KB Output is correct
18 Correct 15 ms 5520 KB Output is correct
19 Correct 53 ms 16284 KB Output is correct
20 Correct 67 ms 17608 KB Output is correct
21 Correct 61 ms 17128 KB Output is correct
22 Correct 52 ms 16700 KB Output is correct
23 Correct 45 ms 13640 KB Output is correct
24 Correct 35 ms 12748 KB Output is correct
25 Correct 29 ms 12536 KB Output is correct
26 Correct 30 ms 12488 KB Output is correct
27 Correct 2 ms 5204 KB Output is correct
28 Correct 2 ms 5204 KB Output is correct
29 Correct 2 ms 5204 KB Output is correct
30 Correct 2 ms 5204 KB Output is correct
31 Correct 2 ms 5204 KB Output is correct
32 Correct 3 ms 5332 KB Output is correct
33 Correct 3 ms 5332 KB Output is correct
34 Correct 3 ms 5460 KB Output is correct
35 Correct 4 ms 5456 KB Output is correct
36 Correct 30 ms 16140 KB Output is correct
37 Correct 36 ms 16844 KB Output is correct
38 Correct 45 ms 17484 KB Output is correct
39 Correct 48 ms 17740 KB Output is correct
40 Correct 37 ms 17096 KB Output is correct
41 Correct 24 ms 12500 KB Output is correct
42 Correct 23 ms 12492 KB Output is correct
43 Correct 42 ms 16872 KB Output is correct
44 Correct 2 ms 5204 KB Output is correct
45 Correct 2 ms 5204 KB Output is correct
46 Correct 3 ms 5204 KB Output is correct
47 Correct 3 ms 5204 KB Output is correct
48 Correct 2 ms 5204 KB Output is correct
49 Correct 7 ms 5460 KB Output is correct
50 Correct 7 ms 5460 KB Output is correct
51 Correct 6 ms 5460 KB Output is correct
52 Correct 7 ms 5460 KB Output is correct
53 Correct 36 ms 16168 KB Output is correct
54 Correct 48 ms 17552 KB Output is correct
55 Correct 52 ms 17868 KB Output is correct
56 Correct 41 ms 17236 KB Output is correct
57 Correct 76 ms 18772 KB Output is correct
58 Correct 53 ms 16308 KB Output is correct
59 Correct 62 ms 17104 KB Output is correct