Submission #1354718

#TimeUsernameProblemLanguageResultExecution timeMemory
1354718otariusToll (BOI17_toll)C++20
7 / 100
50 ms88432 KiB
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngl(chrono::steady_clock::now().time_since_epoch().count());

// #define int long long
// #define int unsigned long long

// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>

// const ll mod = 1e9 + 7;
// const ll mod = 998244353;

const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 5 * 1e4 + 15;

int k, n, m, o, dp[maxN][18][5][5], ans[5][5], tmp[5][5];
void solve() {
    cin >> k >> n >> m >> o;

    for (int i = 0; i <= n; i++)
        for (int j = 0; j < 18; j++)
            for (int k = 0; k < 5; k++)
                for (int p = 0; p < 5; p++) dp[i][j][k][p] = inf;

    for (int i = 1; i <= m; i++) {
        int a, b, t; cin >> a >> b >> t;
        dp[a / k][0][a % k][b % k] = t;
    }

    for (int j = 1; j < 18; j++) {
        for (int i = 0; i + (1 << j) <= (n + k - 1) / k; i++) {
            for (int x = 0; x < k; x++) for (int y = 0; y < k; y++) for (int z = 0; z < k; z++) {
                dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][j - 1][x][z] + dp[i + (1 << (j - 1))][j - 1][z][y]);
            }
        }
    }

    while (o--) {
        int a, b; cin >> a >> b;

        int cur = a / k;

        for (int x = 0; x < k; x++)
            for (int y = 0; y < k; y++) ans[x][y] = 0;
        for (int i = 17; i >= 0; i--) {
            if (cur + (1 << i) <= b / k) {
                for (int x = 0; x < k; x++)
                    for (int y = 0; y < k; y++) tmp[x][y] = inf;
                for (int x = 0; x < k; x++) for (int y = 0; y < k; y++) for (int z = 0; z < k; z++) {
                    tmp[x][y] = min(tmp[x][y], ans[x][z] + dp[cur][i][z][y]);
                }

                for (int x = 0; x < k; x++)
                    for (int y = 0; y < k; y++)
                        ans[x][y] = tmp[x][y];
                cur += (1 << i);
            }
        }

        cout << (ans[a % k][b % k] == inf ? -1 : ans[a % k][b % k]) << '\n';
    }
}
int32_t main() { 
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...