#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const sigma = 5;
int const inf = 1000000000;
struct Matrix {
int dp[sigma][sigma];
Matrix() {
for(int i = 0; i < sigma; i++)
for(int j = 0; j < sigma; j++)
dp[i][j] = inf;
}
Matrix operator * (Matrix a) {
Matrix result;
for(int i = 0; i < sigma; i++)
for(int j = 0; j < sigma; j++)
for(int h = 0; h < sigma; h++)
result.dp[i][j] = std::min(result.dp[i][j], dp[i][h] + a.dp[h][j]);
return result;
}
};
class SegmentTree{
private:
std::vector<Matrix> aint;
public:
SegmentTree(int n) {
aint.resize(1 + 4 * n);
}
void build(int node, int from, int to, std::vector<Matrix> &aux) {
if(from < to) {
int mid = (from + to) / 2;
build(node * 2, from, mid, aux);
build(node * 2 + 1, mid + 1, to, aux);
aint[node] = aint[node * 2] * aint[node * 2 + 1];
} else
aint[node] = aux[from];
}
Matrix query(int node, int from, int to, int x, int y) {
if(from == x && to == y)
return aint[node];
else {
int mid = (from + to) / 2;
if(x <= mid && y <= mid)
return query(node * 2, from, mid, x, y);
else if(mid + 1 <= x && mid + 1 <= y)
return query(node * 2 + 1, mid + 1, to, x, y);
else
return query(node *2 , from, mid, x, mid) * query(node * 2 +1 , mid + 1, to, mid + 1, y);
}
}
};
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int k, n, m, q;
std::cin >> k >> n >> m >> q;
std::vector<Matrix> aux(n / k);
for(int i = 1;i <= m; i++) {
int x, y, cost;
std::cin >> x >> y >> cost;
aux[x / k].dp[x % k][y % k] = cost;
}
SegmentTree aint(n);
aint.build(1, 0, n / k - 1, aux);
for(int i = 1;i <= q; i++) {
int x, y;
std::cin >> x >> y;
if(x / k == y / k)
std::cout << -1 << '\n';
else {
Matrix ext = aint.query(1, 0, n / k - 1, x/ k, y / k - 1);
int result = ext.dp[x % k][y % k];
if(result == inf)
std::cout << -1 << '\n';
else
std::cout << result << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
24952 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
896 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
3 ms |
896 KB |
Output is correct |
8 |
Correct |
80 ms |
24824 KB |
Output is correct |
9 |
Correct |
77 ms |
24824 KB |
Output is correct |
10 |
Correct |
60 ms |
24872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
22520 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
12 ms |
896 KB |
Output is correct |
8 |
Correct |
12 ms |
896 KB |
Output is correct |
9 |
Correct |
82 ms |
25720 KB |
Output is correct |
10 |
Correct |
83 ms |
23908 KB |
Output is correct |
11 |
Correct |
79 ms |
24184 KB |
Output is correct |
12 |
Correct |
66 ms |
22904 KB |
Output is correct |
13 |
Correct |
65 ms |
15096 KB |
Output is correct |
14 |
Correct |
41 ms |
14584 KB |
Output is correct |
15 |
Correct |
36 ms |
13952 KB |
Output is correct |
16 |
Correct |
35 ms |
13952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
896 KB |
Output is correct |
7 |
Correct |
2 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
896 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
47 ms |
25592 KB |
Output is correct |
11 |
Correct |
52 ms |
23928 KB |
Output is correct |
12 |
Correct |
73 ms |
23672 KB |
Output is correct |
13 |
Correct |
67 ms |
24024 KB |
Output is correct |
14 |
Correct |
61 ms |
23416 KB |
Output is correct |
15 |
Correct |
34 ms |
13820 KB |
Output is correct |
16 |
Correct |
33 ms |
13912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
896 KB |
Output is correct |
7 |
Correct |
2 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
896 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
47 ms |
25592 KB |
Output is correct |
11 |
Correct |
52 ms |
23928 KB |
Output is correct |
12 |
Correct |
73 ms |
23672 KB |
Output is correct |
13 |
Correct |
67 ms |
24024 KB |
Output is correct |
14 |
Correct |
61 ms |
23416 KB |
Output is correct |
15 |
Correct |
34 ms |
13820 KB |
Output is correct |
16 |
Correct |
33 ms |
13912 KB |
Output is correct |
17 |
Correct |
65 ms |
23932 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
8 ms |
896 KB |
Output is correct |
24 |
Correct |
8 ms |
872 KB |
Output is correct |
25 |
Correct |
6 ms |
896 KB |
Output is correct |
26 |
Correct |
8 ms |
896 KB |
Output is correct |
27 |
Correct |
55 ms |
25732 KB |
Output is correct |
28 |
Correct |
72 ms |
23776 KB |
Output is correct |
29 |
Correct |
78 ms |
24056 KB |
Output is correct |
30 |
Correct |
60 ms |
23416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
24952 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
896 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
3 ms |
896 KB |
Output is correct |
8 |
Correct |
80 ms |
24824 KB |
Output is correct |
9 |
Correct |
77 ms |
24824 KB |
Output is correct |
10 |
Correct |
60 ms |
24872 KB |
Output is correct |
11 |
Correct |
88 ms |
22520 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
12 ms |
896 KB |
Output is correct |
18 |
Correct |
12 ms |
896 KB |
Output is correct |
19 |
Correct |
82 ms |
25720 KB |
Output is correct |
20 |
Correct |
83 ms |
23908 KB |
Output is correct |
21 |
Correct |
79 ms |
24184 KB |
Output is correct |
22 |
Correct |
66 ms |
22904 KB |
Output is correct |
23 |
Correct |
65 ms |
15096 KB |
Output is correct |
24 |
Correct |
41 ms |
14584 KB |
Output is correct |
25 |
Correct |
36 ms |
13952 KB |
Output is correct |
26 |
Correct |
35 ms |
13952 KB |
Output is correct |
27 |
Correct |
0 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
896 KB |
Output is correct |
33 |
Correct |
2 ms |
768 KB |
Output is correct |
34 |
Correct |
2 ms |
896 KB |
Output is correct |
35 |
Correct |
2 ms |
768 KB |
Output is correct |
36 |
Correct |
47 ms |
25592 KB |
Output is correct |
37 |
Correct |
52 ms |
23928 KB |
Output is correct |
38 |
Correct |
73 ms |
23672 KB |
Output is correct |
39 |
Correct |
67 ms |
24024 KB |
Output is correct |
40 |
Correct |
61 ms |
23416 KB |
Output is correct |
41 |
Correct |
34 ms |
13820 KB |
Output is correct |
42 |
Correct |
33 ms |
13912 KB |
Output is correct |
43 |
Correct |
65 ms |
23932 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
8 ms |
896 KB |
Output is correct |
50 |
Correct |
8 ms |
872 KB |
Output is correct |
51 |
Correct |
6 ms |
896 KB |
Output is correct |
52 |
Correct |
8 ms |
896 KB |
Output is correct |
53 |
Correct |
55 ms |
25732 KB |
Output is correct |
54 |
Correct |
72 ms |
23776 KB |
Output is correct |
55 |
Correct |
78 ms |
24056 KB |
Output is correct |
56 |
Correct |
60 ms |
23416 KB |
Output is correct |
57 |
Correct |
119 ms |
24540 KB |
Output is correct |
58 |
Correct |
81 ms |
25724 KB |
Output is correct |
59 |
Correct |
84 ms |
24184 KB |
Output is correct |