This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define task "SKILEVEL"
#define pii pair<ll, pll>
using namespace std;
using ll = long long;
const int N = 1e5+5;
const ll mod = 1e15;
const ll base = 350;
const ll cs = 331;
ll m, n, t, k, a[N], tong, b[N], dp[N][16][5][5], q, ans;
vector<ll> adj[N], kq;
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void sol()
{
cin >> k >> n >> m >> q;
for(int i = 0; i < n/k; i ++)
{
for(int j = 0; j < 16; j ++)
{
for(int l = 0; l < k; l ++)
{
for(int r = 0; r < k; r ++)dp[i][j][l][r] = mod;
}
}
}
while(m -- > 0)
{
ll x, y, w;
cin >> x >> y >> w;
dp[x/k][0][x%k][y%k] = min(dp[x/k][0][x%k][y%k], w);
}
for(int j = 1; j < 16; j ++)
{
for(int i = 0; i + (1<<j) - 1< n/k; i ++)
{
for(int l = 0; l < k; l ++)
{
for(int r = 0; r < k; r ++)
{
for(int p = 0; p < k; p ++)
{
dp[i][j][l][r] = min(dp[i][j][l][r], dp[i][j-1][l][p] + dp[i+(1<<(j-1))][j-1][p][r]);
}
}
}
}
}
while(q -- > 0)
{
ll x, y;
cin >> x >> y;
ll u = x / k, v = y / k;
vector<ll> kq, pre;
kq.resize(k, mod);
kq[x%k] = 0;
ans = mod;
for(int j = 15; j >= 0; j --)
{
if(u + (1<<j) <= v)
{
pre.resize(k, mod);
for(int l = 0; l < k; l ++)
{
for(int r = 0; r < k; r ++)pre[r] = min(pre[r], kq[l] + dp[u][j][l][r]);
}
swap(kq, pre);
u += (1<<j);
}
}
ans = kq[y%k];
if(ans >= mod)ans = -1;
cout << ans << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
2
6 113 3
1 2 3 4 5 6
1 4
6 113 70
1 2 3 4 5 6
1 4 3
*/
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |