Submission #629966

#TimeUsernameProblemLanguageResultExecution timeMemory
629966KhoaToll (BOI17_toll)C++14
100 / 100
116 ms86840 KiB
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()
#define inc(i, a, b) for (int i = a; i <= b; i++)
#define dec(i, a, b) for (int i = a; i >= b; i--)

typedef long long ll;
typedef pair<int, int> ii;

void print() { cerr << '\n'; }
template <typename T, typename... Args>
void print(const T &a, const Args &...args) { cerr << a << ' ', print(args...); }

int k, n, m, t;
int dp[50000][17][5][5], res[5][5], tmp[5][5];

#define mem(a, val) inc(i, 0, 4) inc(j, 0, 4) a[i][j] = val;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> k >> n >> m >> t;
    memset(dp, 0x3f, sizeof dp);
    
    while (m--)
    {
        int a, b, t;
        cin >> a >> b >> t;
        dp[a / k][0][a % k][b % k] = t;
    }
    auto combine = [&](int target[5][5], int a[5][5], int b[5][5])
    {
        inc(i, 0, k -1) inc(j, 0, k - 1) inc(K, 0, k - 1)
            target[i][j] = min(target[i][j], a[i][K] + b[K][j]);
    };
    
    for (int j = 1; j < 17; j++)
        for (int i = 0; i + (1 << j) < (n + k - 1) / k; i++)
            combine(dp[i][j], dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);

    while (t--)
    {
        int a, b;
        cin >> a >> b;
        mem(res, 1e9);
        inc(i, 0, 4) res[i][i] = 0;
        int cur = a / k;
        dec(i, 16, 0)
        {
            if (cur + (1 << i) <= b / k)
            {
                mem(tmp, 1e9);
                combine(tmp, res, dp[cur][i]);
                mem(res, tmp[i][j]);
                cur += (1 << i);
            }
        }
        a %= k, b %= k;
        cout << (res[a][b] == 1e9 ? -1 : res[a][b]) << '\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...