#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
21188 KB |
Output is correct |
2 |
Correct |
17 ms |
20556 KB |
Output is correct |
3 |
Correct |
17 ms |
20556 KB |
Output is correct |
4 |
Correct |
15 ms |
20556 KB |
Output is correct |
5 |
Correct |
15 ms |
20596 KB |
Output is correct |
6 |
Correct |
16 ms |
20584 KB |
Output is correct |
7 |
Correct |
16 ms |
20488 KB |
Output is correct |
8 |
Correct |
48 ms |
21452 KB |
Output is correct |
9 |
Correct |
45 ms |
21344 KB |
Output is correct |
10 |
Correct |
32 ms |
20712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
21188 KB |
Output is correct |
2 |
Correct |
14 ms |
20556 KB |
Output is correct |
3 |
Correct |
20 ms |
20576 KB |
Output is correct |
4 |
Correct |
30 ms |
20556 KB |
Output is correct |
5 |
Correct |
46 ms |
20556 KB |
Output is correct |
6 |
Correct |
81 ms |
20556 KB |
Output is correct |
7 |
Correct |
24 ms |
20556 KB |
Output is correct |
8 |
Correct |
24 ms |
20720 KB |
Output is correct |
9 |
Correct |
40 ms |
21356 KB |
Output is correct |
10 |
Correct |
109 ms |
22876 KB |
Output is correct |
11 |
Correct |
59 ms |
22212 KB |
Output is correct |
12 |
Correct |
63 ms |
21840 KB |
Output is correct |
13 |
Correct |
148 ms |
22944 KB |
Output is correct |
14 |
Correct |
59 ms |
21996 KB |
Output is correct |
15 |
Correct |
126 ms |
21760 KB |
Output is correct |
16 |
Correct |
122 ms |
21772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
20468 KB |
Output is correct |
2 |
Correct |
16 ms |
20556 KB |
Output is correct |
3 |
Correct |
26 ms |
20552 KB |
Output is correct |
4 |
Correct |
45 ms |
20556 KB |
Output is correct |
5 |
Correct |
85 ms |
20556 KB |
Output is correct |
6 |
Correct |
16 ms |
20556 KB |
Output is correct |
7 |
Correct |
21 ms |
20572 KB |
Output is correct |
8 |
Correct |
85 ms |
20524 KB |
Output is correct |
9 |
Correct |
47 ms |
20604 KB |
Output is correct |
10 |
Correct |
31 ms |
21108 KB |
Output is correct |
11 |
Correct |
46 ms |
22076 KB |
Output is correct |
12 |
Correct |
65 ms |
22724 KB |
Output is correct |
13 |
Correct |
77 ms |
22976 KB |
Output is correct |
14 |
Correct |
66 ms |
22344 KB |
Output is correct |
15 |
Correct |
112 ms |
21824 KB |
Output is correct |
16 |
Correct |
106 ms |
21700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
20468 KB |
Output is correct |
2 |
Correct |
16 ms |
20556 KB |
Output is correct |
3 |
Correct |
26 ms |
20552 KB |
Output is correct |
4 |
Correct |
45 ms |
20556 KB |
Output is correct |
5 |
Correct |
85 ms |
20556 KB |
Output is correct |
6 |
Correct |
16 ms |
20556 KB |
Output is correct |
7 |
Correct |
21 ms |
20572 KB |
Output is correct |
8 |
Correct |
85 ms |
20524 KB |
Output is correct |
9 |
Correct |
47 ms |
20604 KB |
Output is correct |
10 |
Correct |
31 ms |
21108 KB |
Output is correct |
11 |
Correct |
46 ms |
22076 KB |
Output is correct |
12 |
Correct |
65 ms |
22724 KB |
Output is correct |
13 |
Correct |
77 ms |
22976 KB |
Output is correct |
14 |
Correct |
66 ms |
22344 KB |
Output is correct |
15 |
Correct |
112 ms |
21824 KB |
Output is correct |
16 |
Correct |
106 ms |
21700 KB |
Output is correct |
17 |
Correct |
60 ms |
22096 KB |
Output is correct |
18 |
Correct |
16 ms |
20556 KB |
Output is correct |
19 |
Correct |
17 ms |
20544 KB |
Output is correct |
20 |
Correct |
27 ms |
20572 KB |
Output is correct |
21 |
Correct |
45 ms |
20560 KB |
Output is correct |
22 |
Correct |
89 ms |
20556 KB |
Output is correct |
23 |
Correct |
17 ms |
20556 KB |
Output is correct |
24 |
Correct |
20 ms |
20556 KB |
Output is correct |
25 |
Correct |
98 ms |
20644 KB |
Output is correct |
26 |
Correct |
55 ms |
20556 KB |
Output is correct |
27 |
Correct |
41 ms |
21396 KB |
Output is correct |
28 |
Correct |
102 ms |
22720 KB |
Output is correct |
29 |
Correct |
81 ms |
22972 KB |
Output is correct |
30 |
Correct |
69 ms |
22348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
21188 KB |
Output is correct |
2 |
Correct |
17 ms |
20556 KB |
Output is correct |
3 |
Correct |
17 ms |
20556 KB |
Output is correct |
4 |
Correct |
15 ms |
20556 KB |
Output is correct |
5 |
Correct |
15 ms |
20596 KB |
Output is correct |
6 |
Correct |
16 ms |
20584 KB |
Output is correct |
7 |
Correct |
16 ms |
20488 KB |
Output is correct |
8 |
Correct |
48 ms |
21452 KB |
Output is correct |
9 |
Correct |
45 ms |
21344 KB |
Output is correct |
10 |
Correct |
32 ms |
20712 KB |
Output is correct |
11 |
Correct |
60 ms |
21188 KB |
Output is correct |
12 |
Correct |
14 ms |
20556 KB |
Output is correct |
13 |
Correct |
20 ms |
20576 KB |
Output is correct |
14 |
Correct |
30 ms |
20556 KB |
Output is correct |
15 |
Correct |
46 ms |
20556 KB |
Output is correct |
16 |
Correct |
81 ms |
20556 KB |
Output is correct |
17 |
Correct |
24 ms |
20556 KB |
Output is correct |
18 |
Correct |
24 ms |
20720 KB |
Output is correct |
19 |
Correct |
40 ms |
21356 KB |
Output is correct |
20 |
Correct |
109 ms |
22876 KB |
Output is correct |
21 |
Correct |
59 ms |
22212 KB |
Output is correct |
22 |
Correct |
63 ms |
21840 KB |
Output is correct |
23 |
Correct |
148 ms |
22944 KB |
Output is correct |
24 |
Correct |
59 ms |
21996 KB |
Output is correct |
25 |
Correct |
126 ms |
21760 KB |
Output is correct |
26 |
Correct |
122 ms |
21772 KB |
Output is correct |
27 |
Correct |
15 ms |
20468 KB |
Output is correct |
28 |
Correct |
16 ms |
20556 KB |
Output is correct |
29 |
Correct |
26 ms |
20552 KB |
Output is correct |
30 |
Correct |
45 ms |
20556 KB |
Output is correct |
31 |
Correct |
85 ms |
20556 KB |
Output is correct |
32 |
Correct |
16 ms |
20556 KB |
Output is correct |
33 |
Correct |
21 ms |
20572 KB |
Output is correct |
34 |
Correct |
85 ms |
20524 KB |
Output is correct |
35 |
Correct |
47 ms |
20604 KB |
Output is correct |
36 |
Correct |
31 ms |
21108 KB |
Output is correct |
37 |
Correct |
46 ms |
22076 KB |
Output is correct |
38 |
Correct |
65 ms |
22724 KB |
Output is correct |
39 |
Correct |
77 ms |
22976 KB |
Output is correct |
40 |
Correct |
66 ms |
22344 KB |
Output is correct |
41 |
Correct |
112 ms |
21824 KB |
Output is correct |
42 |
Correct |
106 ms |
21700 KB |
Output is correct |
43 |
Correct |
60 ms |
22096 KB |
Output is correct |
44 |
Correct |
16 ms |
20556 KB |
Output is correct |
45 |
Correct |
17 ms |
20544 KB |
Output is correct |
46 |
Correct |
27 ms |
20572 KB |
Output is correct |
47 |
Correct |
45 ms |
20560 KB |
Output is correct |
48 |
Correct |
89 ms |
20556 KB |
Output is correct |
49 |
Correct |
17 ms |
20556 KB |
Output is correct |
50 |
Correct |
20 ms |
20556 KB |
Output is correct |
51 |
Correct |
98 ms |
20644 KB |
Output is correct |
52 |
Correct |
55 ms |
20556 KB |
Output is correct |
53 |
Correct |
41 ms |
21396 KB |
Output is correct |
54 |
Correct |
102 ms |
22720 KB |
Output is correct |
55 |
Correct |
81 ms |
22972 KB |
Output is correct |
56 |
Correct |
69 ms |
22348 KB |
Output is correct |
57 |
Correct |
193 ms |
23872 KB |
Output is correct |
58 |
Correct |
63 ms |
21448 KB |
Output is correct |
59 |
Correct |
68 ms |
22280 KB |
Output is correct |