#include <bits/stdc++.h>
using namespace std;
const int MK = 5;
const int MX = 5e4 + 10;
const int INF = 1e9 + 7;
int K, N, M, Q;
void chmin(int& a, int b){
if(b < a) a = b;
}
struct matrix{
int arr[MK][MK];
void init(int t = INF){
for(int i = 0; i < K; i++){
for(int j = 0; j < K; j++) arr[i][j] = INF;
arr[i][i] = t;
}
}
matrix operator + (const matrix& other){
matrix c; c.init();
for(int i = 0; i < K; i++){
for(int j = 0; j < K; j++){
for(int k = 0; k < K; k++){
chmin(c.arr[i][j], arr[i][k] + other.arr[k][j]);
}
}
}
return c;
}
};
matrix A[MX];
struct segtree{
matrix st[MX << 1];
void build(){
for(int i = MX; i < (MX << 1); i++) st[i] = A[i - MX];
for(int i = MX - 1; i > 0; i--) st[i] = st[i << 1] + st[i << 1|1];
}
matrix que(int l, int r){
matrix lf, rg; lf.init(0); rg.init(0);
for(l += MX, r += MX + 1; l < r; l >>= 1, r >>= 1){
if(l & 1) lf = lf + st[l], l++;
if(r & 1) --r, rg = st[r] + rg;
}
lf = lf + rg;
return lf;
}
} S;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> K >> N >> M >> Q;
for(int i = 0; i < (N - 1) / K; i++) A[i].init();
for(int i = 0; i < M; i++){
int a, b, t; cin >> a >> b >> t;
chmin(A[a / K].arr[a % K][b % K], t);
}
S.build();
for(int i = 0; i < Q; i++){
int a, b; cin >> a >> b;
if(a / K == b / K){
cout << -1 << '\n';
continue;
}
matrix ans = S.que(a / K, b / K - 1);
if(ans.arr[a % K][b % K] == INF) cout << -1 << '\n';
else cout << ans.arr[a % K][b % K] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
10068 KB |
Output is correct |
3 |
Correct |
6 ms |
10068 KB |
Output is correct |
4 |
Correct |
6 ms |
10068 KB |
Output is correct |
5 |
Correct |
6 ms |
10196 KB |
Output is correct |
6 |
Correct |
6 ms |
10196 KB |
Output is correct |
7 |
Correct |
6 ms |
10196 KB |
Output is correct |
8 |
Correct |
27 ms |
15916 KB |
Output is correct |
9 |
Correct |
35 ms |
15824 KB |
Output is correct |
10 |
Correct |
13 ms |
15060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
14288 KB |
Output is correct |
2 |
Correct |
6 ms |
10068 KB |
Output is correct |
3 |
Correct |
6 ms |
10068 KB |
Output is correct |
4 |
Correct |
7 ms |
10068 KB |
Output is correct |
5 |
Correct |
9 ms |
10052 KB |
Output is correct |
6 |
Correct |
14 ms |
10068 KB |
Output is correct |
7 |
Correct |
9 ms |
10324 KB |
Output is correct |
8 |
Correct |
11 ms |
10300 KB |
Output is correct |
9 |
Correct |
34 ms |
15832 KB |
Output is correct |
10 |
Correct |
59 ms |
14032 KB |
Output is correct |
11 |
Correct |
44 ms |
14272 KB |
Output is correct |
12 |
Correct |
40 ms |
12952 KB |
Output is correct |
13 |
Correct |
61 ms |
13144 KB |
Output is correct |
14 |
Correct |
38 ms |
12476 KB |
Output is correct |
15 |
Correct |
42 ms |
11852 KB |
Output is correct |
16 |
Correct |
45 ms |
11876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10024 KB |
Output is correct |
2 |
Correct |
7 ms |
10068 KB |
Output is correct |
3 |
Correct |
7 ms |
10112 KB |
Output is correct |
4 |
Correct |
9 ms |
10060 KB |
Output is correct |
5 |
Correct |
14 ms |
10064 KB |
Output is correct |
6 |
Correct |
7 ms |
10196 KB |
Output is correct |
7 |
Correct |
8 ms |
10196 KB |
Output is correct |
8 |
Correct |
13 ms |
10196 KB |
Output is correct |
9 |
Correct |
9 ms |
10068 KB |
Output is correct |
10 |
Correct |
22 ms |
15736 KB |
Output is correct |
11 |
Correct |
34 ms |
14016 KB |
Output is correct |
12 |
Correct |
48 ms |
13844 KB |
Output is correct |
13 |
Correct |
52 ms |
14092 KB |
Output is correct |
14 |
Correct |
41 ms |
13528 KB |
Output is correct |
15 |
Correct |
35 ms |
11852 KB |
Output is correct |
16 |
Correct |
32 ms |
11852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10024 KB |
Output is correct |
2 |
Correct |
7 ms |
10068 KB |
Output is correct |
3 |
Correct |
7 ms |
10112 KB |
Output is correct |
4 |
Correct |
9 ms |
10060 KB |
Output is correct |
5 |
Correct |
14 ms |
10064 KB |
Output is correct |
6 |
Correct |
7 ms |
10196 KB |
Output is correct |
7 |
Correct |
8 ms |
10196 KB |
Output is correct |
8 |
Correct |
13 ms |
10196 KB |
Output is correct |
9 |
Correct |
9 ms |
10068 KB |
Output is correct |
10 |
Correct |
22 ms |
15736 KB |
Output is correct |
11 |
Correct |
34 ms |
14016 KB |
Output is correct |
12 |
Correct |
48 ms |
13844 KB |
Output is correct |
13 |
Correct |
52 ms |
14092 KB |
Output is correct |
14 |
Correct |
41 ms |
13528 KB |
Output is correct |
15 |
Correct |
35 ms |
11852 KB |
Output is correct |
16 |
Correct |
32 ms |
11852 KB |
Output is correct |
17 |
Correct |
35 ms |
14008 KB |
Output is correct |
18 |
Correct |
6 ms |
10068 KB |
Output is correct |
19 |
Correct |
8 ms |
10068 KB |
Output is correct |
20 |
Correct |
7 ms |
10068 KB |
Output is correct |
21 |
Correct |
9 ms |
10048 KB |
Output is correct |
22 |
Correct |
12 ms |
10068 KB |
Output is correct |
23 |
Correct |
8 ms |
10260 KB |
Output is correct |
24 |
Correct |
8 ms |
10156 KB |
Output is correct |
25 |
Correct |
19 ms |
10196 KB |
Output is correct |
26 |
Correct |
13 ms |
10196 KB |
Output is correct |
27 |
Correct |
24 ms |
15812 KB |
Output is correct |
28 |
Correct |
49 ms |
13964 KB |
Output is correct |
29 |
Correct |
53 ms |
14088 KB |
Output is correct |
30 |
Correct |
59 ms |
13580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
10068 KB |
Output is correct |
3 |
Correct |
6 ms |
10068 KB |
Output is correct |
4 |
Correct |
6 ms |
10068 KB |
Output is correct |
5 |
Correct |
6 ms |
10196 KB |
Output is correct |
6 |
Correct |
6 ms |
10196 KB |
Output is correct |
7 |
Correct |
6 ms |
10196 KB |
Output is correct |
8 |
Correct |
27 ms |
15916 KB |
Output is correct |
9 |
Correct |
35 ms |
15824 KB |
Output is correct |
10 |
Correct |
13 ms |
15060 KB |
Output is correct |
11 |
Correct |
37 ms |
14288 KB |
Output is correct |
12 |
Correct |
6 ms |
10068 KB |
Output is correct |
13 |
Correct |
6 ms |
10068 KB |
Output is correct |
14 |
Correct |
7 ms |
10068 KB |
Output is correct |
15 |
Correct |
9 ms |
10052 KB |
Output is correct |
16 |
Correct |
14 ms |
10068 KB |
Output is correct |
17 |
Correct |
9 ms |
10324 KB |
Output is correct |
18 |
Correct |
11 ms |
10300 KB |
Output is correct |
19 |
Correct |
34 ms |
15832 KB |
Output is correct |
20 |
Correct |
59 ms |
14032 KB |
Output is correct |
21 |
Correct |
44 ms |
14272 KB |
Output is correct |
22 |
Correct |
40 ms |
12952 KB |
Output is correct |
23 |
Correct |
61 ms |
13144 KB |
Output is correct |
24 |
Correct |
38 ms |
12476 KB |
Output is correct |
25 |
Correct |
42 ms |
11852 KB |
Output is correct |
26 |
Correct |
45 ms |
11876 KB |
Output is correct |
27 |
Correct |
6 ms |
10024 KB |
Output is correct |
28 |
Correct |
7 ms |
10068 KB |
Output is correct |
29 |
Correct |
7 ms |
10112 KB |
Output is correct |
30 |
Correct |
9 ms |
10060 KB |
Output is correct |
31 |
Correct |
14 ms |
10064 KB |
Output is correct |
32 |
Correct |
7 ms |
10196 KB |
Output is correct |
33 |
Correct |
8 ms |
10196 KB |
Output is correct |
34 |
Correct |
13 ms |
10196 KB |
Output is correct |
35 |
Correct |
9 ms |
10068 KB |
Output is correct |
36 |
Correct |
22 ms |
15736 KB |
Output is correct |
37 |
Correct |
34 ms |
14016 KB |
Output is correct |
38 |
Correct |
48 ms |
13844 KB |
Output is correct |
39 |
Correct |
52 ms |
14092 KB |
Output is correct |
40 |
Correct |
41 ms |
13528 KB |
Output is correct |
41 |
Correct |
35 ms |
11852 KB |
Output is correct |
42 |
Correct |
32 ms |
11852 KB |
Output is correct |
43 |
Correct |
35 ms |
14008 KB |
Output is correct |
44 |
Correct |
6 ms |
10068 KB |
Output is correct |
45 |
Correct |
8 ms |
10068 KB |
Output is correct |
46 |
Correct |
7 ms |
10068 KB |
Output is correct |
47 |
Correct |
9 ms |
10048 KB |
Output is correct |
48 |
Correct |
12 ms |
10068 KB |
Output is correct |
49 |
Correct |
8 ms |
10260 KB |
Output is correct |
50 |
Correct |
8 ms |
10156 KB |
Output is correct |
51 |
Correct |
19 ms |
10196 KB |
Output is correct |
52 |
Correct |
13 ms |
10196 KB |
Output is correct |
53 |
Correct |
24 ms |
15812 KB |
Output is correct |
54 |
Correct |
49 ms |
13964 KB |
Output is correct |
55 |
Correct |
53 ms |
14088 KB |
Output is correct |
56 |
Correct |
59 ms |
13580 KB |
Output is correct |
57 |
Correct |
87 ms |
14592 KB |
Output is correct |
58 |
Correct |
27 ms |
15912 KB |
Output is correct |
59 |
Correct |
41 ms |
14264 KB |
Output is correct |