Submission #445927

#TimeUsernameProblemLanguageResultExecution timeMemory
445927prvocisloToll (BOI17_toll)C++17
100 / 100
193 ms23872 KiB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
 
const int maxk = 5, maxvr = 1 << 16, inf = 1e9 + 5;
struct node
{
    int dp[maxk][maxk], l, r;
    node () { for (int i = 0; i < maxk; i++) for (int j = 0; j < maxk; j++) dp[i][j] = inf; }
} st[maxvr*2];
void upd(int &a, ll b) { a = min((ll)a, b); }
int n, m, q, k;
int g[maxvr*maxk][maxk];
node merge(const node &a, const node &b)
{
    node nd;
    nd.l = a.l, nd.r = b.r;
    for (int s = 0; s < k; s++) for (int f = 0; f < k; f++) for (int i = 0; i < k; i++) for (int j = 0; j < k; j++)
        upd(nd.dp[s][f], (ll)a.dp[s][i] + (ll)g[a.r*k+i][j] + (ll)b.dp[j][f]);
    return nd;
}
void query(int l, int r, node &nd, bool &first, int vr = 0)
{
    if (r < st[vr].l || st[vr].r < l) return;
    if (l <= st[vr].l && st[vr].r <= r) 
    {
        if (first) 
        {
            for (int i = 0; i < k; i++) for (int j = 0; j < k; j++) nd.dp[i][j] = st[vr].dp[i][j];
            nd.l = st[vr].l, nd.r = st[vr].r;
            first = false;
        }
        else nd = merge(nd, st[vr]);
        return;
    }
    query(l, r, nd, first, vr*2+1);
    query(l, r, nd, first, vr*2+2);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    for (int i = 0; i < maxvr*maxk; i++) for (int j = 0; j < maxk; j++) g[i][j] = inf;
    cin >> k >> n >> m >> q;
    for (int i = 0, a, b, c; i < m; i++)
    {
        cin >> a >> b >> c;
        g[a][b%k] = c;
    }
    for (int i = maxvr-1; i < maxvr*2; i++) 
    {
        st[i].l = st[i].r = i-(maxvr-1);
        for (int j = 0; j < maxk; j++) st[i].dp[j][j] = 0;
    }
    for (int i = maxvr-2; i >= 0; i--) 
    {
        st[i] = merge(st[i*2+1], st[i*2+2]);
    }
    while (q--)
    {
        int a, b;
        cin >> a >> b;
        node nd; bool first = true;
        query(a/k, b/k, nd, first);
        int ans = nd.dp[a%k][b%k];
        if (ans == inf) cout << "-1\n";
        else cout << ans << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...