#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
/// #pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
const int LOG = 20;
int K, N, M, Q;
array<array<int, 5>, 5> P[50005][20];
array<array<int, 5>, 5> merge(array<array<int, 5>, 5> A,
array<array<int, 5>, 5> B) {
array<array<int, 5>, 5> res;
for(int l = 0; l < K; l++)
for(int i = 0; i < K; i++)
res[l][i] = 1e9 + 7;
for(int l = 0; l < K; l++)
for(int i = 0; i < K; i++)
for(int j = 0; j < K; j++)
res[l][i] = min(res[l][i], A[l][j] + B[j][i]);
return res;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> K >> N >> M >> Q;
for(int l = 0; l < N; l++)
for(int i = 0; i < LOG; i++)
for(int j = 0; j < K; j++)
for(int e = 0; e < K; e++)
P[l][i][j][e] = 1e9 + 7;
for(int l = 0; l < M; l++) {
int U, V, C; cin >> U >> V >> C;
P[U / K][0][U % K][V % K] = C;
}
for(int l = 1; l < LOG; l++) {
for(int i = 0; i + (1 << l) < (N + K - 1) / K; i++) {
P[i][l] = merge(P[i][l - 1], P[i + (1 << l - 1)][l - 1]);
}
}
while(Q--) {
int U, V; cin >> U >> V;
int X = U / K, Y = V / K;
array<array<int, 5>, 5> res;
for(int l = 0; l < K; l++) {
for(int i = 0; i < K; i++)
res[l][i] = 1e9 + 7;
res[l][l] = 0;
}
for(int l = LOG - 1; ~l; l--) {
if(X + (1 << l) > Y) continue;
res = merge(res, P[X][l]);
X += (1 << l);
}
cout << (res[U % K][V % K] == 1e9 + 7 ? -1 : res[U % K][V % K]) << "\n";
}
return 0;
}
Compilation message
toll.cpp: In function 'int32_t main()':
toll.cpp:48:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
48 | P[i][l] = merge(P[i][l - 1], P[i + (1 << l - 1)][l - 1]);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
99080 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
2260 KB |
Output is correct |
6 |
Correct |
3 ms |
2268 KB |
Output is correct |
7 |
Correct |
3 ms |
2260 KB |
Output is correct |
8 |
Correct |
140 ms |
99072 KB |
Output is correct |
9 |
Correct |
147 ms |
99036 KB |
Output is correct |
10 |
Correct |
138 ms |
98212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
99764 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
9 ms |
2372 KB |
Output is correct |
8 |
Correct |
15 ms |
2412 KB |
Output is correct |
9 |
Correct |
148 ms |
99044 KB |
Output is correct |
10 |
Correct |
211 ms |
100424 KB |
Output is correct |
11 |
Correct |
152 ms |
99932 KB |
Output is correct |
12 |
Correct |
229 ms |
99464 KB |
Output is correct |
13 |
Correct |
221 ms |
61412 KB |
Output is correct |
14 |
Correct |
106 ms |
60412 KB |
Output is correct |
15 |
Correct |
204 ms |
60232 KB |
Output is correct |
16 |
Correct |
221 ms |
60256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
7 ms |
2392 KB |
Output is correct |
9 |
Correct |
4 ms |
2260 KB |
Output is correct |
10 |
Correct |
131 ms |
98972 KB |
Output is correct |
11 |
Correct |
146 ms |
99684 KB |
Output is correct |
12 |
Correct |
194 ms |
100324 KB |
Output is correct |
13 |
Correct |
190 ms |
100532 KB |
Output is correct |
14 |
Correct |
191 ms |
99964 KB |
Output is correct |
15 |
Correct |
169 ms |
60208 KB |
Output is correct |
16 |
Correct |
168 ms |
60160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
7 ms |
2392 KB |
Output is correct |
9 |
Correct |
4 ms |
2260 KB |
Output is correct |
10 |
Correct |
131 ms |
98972 KB |
Output is correct |
11 |
Correct |
146 ms |
99684 KB |
Output is correct |
12 |
Correct |
194 ms |
100324 KB |
Output is correct |
13 |
Correct |
190 ms |
100532 KB |
Output is correct |
14 |
Correct |
191 ms |
99964 KB |
Output is correct |
15 |
Correct |
169 ms |
60208 KB |
Output is correct |
16 |
Correct |
168 ms |
60160 KB |
Output is correct |
17 |
Correct |
142 ms |
99724 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 |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
2268 KB |
Output is correct |
24 |
Correct |
4 ms |
2336 KB |
Output is correct |
25 |
Correct |
27 ms |
2384 KB |
Output is correct |
26 |
Correct |
14 ms |
2260 KB |
Output is correct |
27 |
Correct |
175 ms |
98908 KB |
Output is correct |
28 |
Correct |
187 ms |
100300 KB |
Output is correct |
29 |
Correct |
187 ms |
100548 KB |
Output is correct |
30 |
Correct |
196 ms |
100004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
99080 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
2260 KB |
Output is correct |
6 |
Correct |
3 ms |
2268 KB |
Output is correct |
7 |
Correct |
3 ms |
2260 KB |
Output is correct |
8 |
Correct |
140 ms |
99072 KB |
Output is correct |
9 |
Correct |
147 ms |
99036 KB |
Output is correct |
10 |
Correct |
138 ms |
98212 KB |
Output is correct |
11 |
Correct |
166 ms |
99764 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
9 ms |
2372 KB |
Output is correct |
18 |
Correct |
15 ms |
2412 KB |
Output is correct |
19 |
Correct |
148 ms |
99044 KB |
Output is correct |
20 |
Correct |
211 ms |
100424 KB |
Output is correct |
21 |
Correct |
152 ms |
99932 KB |
Output is correct |
22 |
Correct |
229 ms |
99464 KB |
Output is correct |
23 |
Correct |
221 ms |
61412 KB |
Output is correct |
24 |
Correct |
106 ms |
60412 KB |
Output is correct |
25 |
Correct |
204 ms |
60232 KB |
Output is correct |
26 |
Correct |
221 ms |
60256 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
2260 KB |
Output is correct |
33 |
Correct |
2 ms |
2260 KB |
Output is correct |
34 |
Correct |
7 ms |
2392 KB |
Output is correct |
35 |
Correct |
4 ms |
2260 KB |
Output is correct |
36 |
Correct |
131 ms |
98972 KB |
Output is correct |
37 |
Correct |
146 ms |
99684 KB |
Output is correct |
38 |
Correct |
194 ms |
100324 KB |
Output is correct |
39 |
Correct |
190 ms |
100532 KB |
Output is correct |
40 |
Correct |
191 ms |
99964 KB |
Output is correct |
41 |
Correct |
169 ms |
60208 KB |
Output is correct |
42 |
Correct |
168 ms |
60160 KB |
Output is correct |
43 |
Correct |
142 ms |
99724 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 |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
3 ms |
2268 KB |
Output is correct |
50 |
Correct |
4 ms |
2336 KB |
Output is correct |
51 |
Correct |
27 ms |
2384 KB |
Output is correct |
52 |
Correct |
14 ms |
2260 KB |
Output is correct |
53 |
Correct |
175 ms |
98908 KB |
Output is correct |
54 |
Correct |
187 ms |
100300 KB |
Output is correct |
55 |
Correct |
187 ms |
100548 KB |
Output is correct |
56 |
Correct |
196 ms |
100004 KB |
Output is correct |
57 |
Correct |
329 ms |
101468 KB |
Output is correct |
58 |
Correct |
162 ms |
99020 KB |
Output is correct |
59 |
Correct |
158 ms |
100028 KB |
Output is correct |