#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxk = 5, maxvr = 1 << 14, maxn = maxvr * maxk, 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[maxn][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 < maxn; 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
6220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6976 KB |
Output is correct |
2 |
Correct |
4 ms |
5324 KB |
Output is correct |
3 |
Correct |
5 ms |
5324 KB |
Output is correct |
4 |
Correct |
7 ms |
5324 KB |
Output is correct |
5 |
Correct |
11 ms |
5380 KB |
Output is correct |
6 |
Correct |
20 ms |
5380 KB |
Output is correct |
7 |
Correct |
10 ms |
5476 KB |
Output is correct |
8 |
Correct |
12 ms |
5452 KB |
Output is correct |
9 |
Correct |
23 ms |
6156 KB |
Output is correct |
10 |
Incorrect |
71 ms |
7656 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
5 ms |
5324 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
11 ms |
5380 KB |
Output is correct |
5 |
Correct |
21 ms |
5376 KB |
Output is correct |
6 |
Correct |
4 ms |
5336 KB |
Output is correct |
7 |
Correct |
6 ms |
5324 KB |
Output is correct |
8 |
Correct |
22 ms |
5452 KB |
Output is correct |
9 |
Correct |
12 ms |
5392 KB |
Output is correct |
10 |
Incorrect |
20 ms |
6136 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
5 ms |
5324 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
11 ms |
5380 KB |
Output is correct |
5 |
Correct |
21 ms |
5376 KB |
Output is correct |
6 |
Correct |
4 ms |
5336 KB |
Output is correct |
7 |
Correct |
6 ms |
5324 KB |
Output is correct |
8 |
Correct |
22 ms |
5452 KB |
Output is correct |
9 |
Correct |
12 ms |
5392 KB |
Output is correct |
10 |
Incorrect |
20 ms |
6136 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
6220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |