#include <bits/stdc++.h>
using namespace std;
const int N = 50005;
using ll = long long;
int kk, n, m, q;
struct Matrix {
ll a[5][5];
inline ll *operator[](size_t x) {
return a[x];
}
inline const ll *operator[](size_t x) const {
return a[x];
}
} INF;
Matrix merge(const Matrix &a, const Matrix &b) {
Matrix c = INF;
for (int i = 0; i < kk; i++)
for (int j = 0; j < kk; j++)
for (int k = 0; k < kk; k++)
c[i][j] = min(c[i][j], a[i][k] + b[k][j]);
return c;
}
Matrix a[N];
struct TNode {
int l, r;
Matrix v;
} t[N << 2];
inline int lc(int pos) {
return pos << 1;
};
inline int rc(int pos) {
return pos << 1 | 1;
}
void build(int pos, int l, int r) {
t[pos].l = l;
t[pos].r = r;
if (l == r) {
t[pos].v = a[l];
return;
}
int mid = (l + r) >> 1;
build(lc(pos), l, mid);
build(rc(pos), mid + 1, r);
t[pos].v = merge(t[lc(pos)].v, t[rc(pos)].v);
}
Matrix query(int pos, int l, int r) {
if (t[pos].l == l && t[pos].r == r)
return t[pos].v;
int mid = (t[pos].l + t[pos].r) >> 1;
if (r <= mid)
return query(lc(pos), l, r);
else if (l > mid)
return query(rc(pos), l, r);
else
return merge(query(lc(pos), l, mid), query(rc(pos), mid + 1, r));
}
int main() {
ios::sync_with_stdio(false);
cin >> kk >> n >> m >> q;
for (int i = 0; i < kk; i++)
for (int j = 0; j < kk; j++)
INF[i][j] = LLONG_MAX >> 2;
for (int i = 0; i <= n / kk; i++)
a[i] = INF;
for (int i = 1; i <= m; i++) {
int t1, t2, t3;
cin >> t1 >> t2 >> t3;
a[t2 / kk][t1 % kk][t2 % kk] = t3;
}
build(1, 0, n / kk);
while (q--) {
int t1, t2;
cin >> t1 >> t2;
if (t1 / kk >= t2 / kk) {
cout << -1 << endl;
continue;
}
ll ans = query(1, t1 / kk + 1, t2 / kk)[t1 % kk][t2 % kk];
if (ans == LLONG_MAX >> 2)
ans = -1;
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
36940 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
3 ms |
940 KB |
Output is correct |
8 |
Correct |
54 ms |
36820 KB |
Output is correct |
9 |
Correct |
55 ms |
36856 KB |
Output is correct |
10 |
Correct |
40 ms |
36920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
18672 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
18 ms |
980 KB |
Output is correct |
8 |
Correct |
19 ms |
760 KB |
Output is correct |
9 |
Correct |
60 ms |
36972 KB |
Output is correct |
10 |
Correct |
61 ms |
11564 KB |
Output is correct |
11 |
Correct |
55 ms |
18688 KB |
Output is correct |
12 |
Correct |
47 ms |
11532 KB |
Output is correct |
13 |
Correct |
47 ms |
4868 KB |
Output is correct |
14 |
Correct |
33 ms |
9040 KB |
Output is correct |
15 |
Correct |
32 ms |
4960 KB |
Output is correct |
16 |
Correct |
33 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
31 ms |
36812 KB |
Output is correct |
11 |
Correct |
32 ms |
18664 KB |
Output is correct |
12 |
Correct |
38 ms |
11396 KB |
Output is correct |
13 |
Correct |
52 ms |
11524 KB |
Output is correct |
14 |
Correct |
36 ms |
11512 KB |
Output is correct |
15 |
Correct |
22 ms |
4856 KB |
Output is correct |
16 |
Correct |
22 ms |
4848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
31 ms |
36812 KB |
Output is correct |
11 |
Correct |
32 ms |
18664 KB |
Output is correct |
12 |
Correct |
38 ms |
11396 KB |
Output is correct |
13 |
Correct |
52 ms |
11524 KB |
Output is correct |
14 |
Correct |
36 ms |
11512 KB |
Output is correct |
15 |
Correct |
22 ms |
4856 KB |
Output is correct |
16 |
Correct |
22 ms |
4848 KB |
Output is correct |
17 |
Correct |
40 ms |
18684 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
7 ms |
980 KB |
Output is correct |
24 |
Correct |
6 ms |
596 KB |
Output is correct |
25 |
Correct |
8 ms |
468 KB |
Output is correct |
26 |
Correct |
7 ms |
468 KB |
Output is correct |
27 |
Correct |
41 ms |
36860 KB |
Output is correct |
28 |
Correct |
45 ms |
11508 KB |
Output is correct |
29 |
Correct |
53 ms |
11428 KB |
Output is correct |
30 |
Correct |
39 ms |
11536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
36940 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
3 ms |
940 KB |
Output is correct |
8 |
Correct |
54 ms |
36820 KB |
Output is correct |
9 |
Correct |
55 ms |
36856 KB |
Output is correct |
10 |
Correct |
40 ms |
36920 KB |
Output is correct |
11 |
Correct |
58 ms |
18672 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
18 ms |
980 KB |
Output is correct |
18 |
Correct |
19 ms |
760 KB |
Output is correct |
19 |
Correct |
60 ms |
36972 KB |
Output is correct |
20 |
Correct |
61 ms |
11564 KB |
Output is correct |
21 |
Correct |
55 ms |
18688 KB |
Output is correct |
22 |
Correct |
47 ms |
11532 KB |
Output is correct |
23 |
Correct |
47 ms |
4868 KB |
Output is correct |
24 |
Correct |
33 ms |
9040 KB |
Output is correct |
25 |
Correct |
32 ms |
4960 KB |
Output is correct |
26 |
Correct |
33 ms |
4956 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
436 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
31 ms |
36812 KB |
Output is correct |
37 |
Correct |
32 ms |
18664 KB |
Output is correct |
38 |
Correct |
38 ms |
11396 KB |
Output is correct |
39 |
Correct |
52 ms |
11524 KB |
Output is correct |
40 |
Correct |
36 ms |
11512 KB |
Output is correct |
41 |
Correct |
22 ms |
4856 KB |
Output is correct |
42 |
Correct |
22 ms |
4848 KB |
Output is correct |
43 |
Correct |
40 ms |
18684 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
324 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
7 ms |
980 KB |
Output is correct |
50 |
Correct |
6 ms |
596 KB |
Output is correct |
51 |
Correct |
8 ms |
468 KB |
Output is correct |
52 |
Correct |
7 ms |
468 KB |
Output is correct |
53 |
Correct |
41 ms |
36860 KB |
Output is correct |
54 |
Correct |
45 ms |
11508 KB |
Output is correct |
55 |
Correct |
53 ms |
11428 KB |
Output is correct |
56 |
Correct |
39 ms |
11536 KB |
Output is correct |
57 |
Correct |
77 ms |
9544 KB |
Output is correct |
58 |
Correct |
53 ms |
36904 KB |
Output is correct |
59 |
Correct |
53 ms |
18764 KB |
Output is correct |