답안 #747356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747356 2023-05-24T05:55:28 Z GrindMachine Toll (BOI17_toll) C++17
0 / 100
38 ms 16972 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]);
                }
            }
        }

        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;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 29 ms 16296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 38 ms 16972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 29 ms 16296 KB Output isn't correct
2 Halted 0 ms 0 KB -