#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 50505
#define MAXS 17
#define INF 10000000000000
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MK 5
vector<vector<ll>> sp[MAX][MAXS];
int K;
vector<vector<ll>> operator+(vector<vector<ll>>& v1, vector<vector<ll>>& v2) {
vector<vector<ll>> res = vector<vector<ll>>(K, vector<ll>(K, INF));
int i, j, k;
for (i = 0; i < K; i++) for (j = 0; j < K; j++) for (k = 0; k < K; k++) res[i][j] = min(res[i][j], v1[i][k] + v2[k][j]);
return res;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N, M, Q;
cin >> K >> N >> M >> Q;
int i, j;
int X = (N + K - 1) / K;
for (i = 0; i < MAX; i++) for (j = 0; j < MAXS; j++) sp[i][j] = vector<vector<ll>>(K, vector<ll>(K, INF));
int a, b, c;
for (i = 1; i <= M; i++) {
cin >> a >> b >> c;
sp[a / K][0][a % K][b % K] = c;
}
for (j = 1; j < MAXS; j++) for (i = 0; i <= X; i++) if (i + (1 << (j - 1)) <= X + 1) sp[i][j] = sp[i][j - 1] + sp[i + (1 << (j - 1))][j - 1];
while (Q--) {
cin >> a >> b;
int aa = a;
int d = b / K - a / K;
if (!d) {
cout << -1 << ln;
continue;
}
a /= K;
vector<vector<ll>> v;
for (i = 0; i < MAXS; i++) {
if (d >> i & 1) {
if (v.empty()) v = sp[a][i];
else v = v + sp[a][i];
a += 1 << i;
}
}
ll ans = v[aa % K][b % K];
if (ans >= INF) cout << -1 << ln;
else cout << ans << ln;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
74228 KB |
Output is correct |
2 |
Correct |
100 ms |
74184 KB |
Output is correct |
3 |
Correct |
106 ms |
74212 KB |
Output is correct |
4 |
Correct |
108 ms |
74128 KB |
Output is correct |
5 |
Correct |
108 ms |
74196 KB |
Output is correct |
6 |
Correct |
104 ms |
74188 KB |
Output is correct |
7 |
Correct |
106 ms |
74224 KB |
Output is correct |
8 |
Correct |
229 ms |
74260 KB |
Output is correct |
9 |
Correct |
237 ms |
74232 KB |
Output is correct |
10 |
Correct |
207 ms |
74232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
128004 KB |
Output is correct |
2 |
Correct |
105 ms |
74192 KB |
Output is correct |
3 |
Correct |
164 ms |
127992 KB |
Output is correct |
4 |
Correct |
211 ms |
168304 KB |
Output is correct |
5 |
Correct |
319 ms |
275788 KB |
Output is correct |
6 |
Correct |
323 ms |
329560 KB |
Output is correct |
7 |
Correct |
113 ms |
74248 KB |
Output is correct |
8 |
Correct |
162 ms |
128236 KB |
Output is correct |
9 |
Correct |
208 ms |
75144 KB |
Output is correct |
10 |
Correct |
346 ms |
170660 KB |
Output is correct |
11 |
Correct |
289 ms |
129876 KB |
Output is correct |
12 |
Correct |
334 ms |
169640 KB |
Output is correct |
13 |
Correct |
655 ms |
332016 KB |
Output is correct |
14 |
Correct |
289 ms |
169752 KB |
Output is correct |
15 |
Correct |
400 ms |
330860 KB |
Output is correct |
16 |
Correct |
417 ms |
330780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
74228 KB |
Output is correct |
2 |
Correct |
158 ms |
127940 KB |
Output is correct |
3 |
Correct |
202 ms |
168316 KB |
Output is correct |
4 |
Correct |
268 ms |
275812 KB |
Output is correct |
5 |
Correct |
322 ms |
329572 KB |
Output is correct |
6 |
Correct |
111 ms |
74160 KB |
Output is correct |
7 |
Correct |
177 ms |
127948 KB |
Output is correct |
8 |
Correct |
328 ms |
329616 KB |
Output is correct |
9 |
Correct |
289 ms |
276024 KB |
Output is correct |
10 |
Correct |
201 ms |
75036 KB |
Output is correct |
11 |
Correct |
275 ms |
129532 KB |
Output is correct |
12 |
Correct |
344 ms |
170496 KB |
Output is correct |
13 |
Correct |
343 ms |
170680 KB |
Output is correct |
14 |
Correct |
338 ms |
170132 KB |
Output is correct |
15 |
Correct |
432 ms |
330804 KB |
Output is correct |
16 |
Correct |
408 ms |
330688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
74228 KB |
Output is correct |
2 |
Correct |
158 ms |
127940 KB |
Output is correct |
3 |
Correct |
202 ms |
168316 KB |
Output is correct |
4 |
Correct |
268 ms |
275812 KB |
Output is correct |
5 |
Correct |
322 ms |
329572 KB |
Output is correct |
6 |
Correct |
111 ms |
74160 KB |
Output is correct |
7 |
Correct |
177 ms |
127948 KB |
Output is correct |
8 |
Correct |
328 ms |
329616 KB |
Output is correct |
9 |
Correct |
289 ms |
276024 KB |
Output is correct |
10 |
Correct |
201 ms |
75036 KB |
Output is correct |
11 |
Correct |
275 ms |
129532 KB |
Output is correct |
12 |
Correct |
344 ms |
170496 KB |
Output is correct |
13 |
Correct |
343 ms |
170680 KB |
Output is correct |
14 |
Correct |
338 ms |
170132 KB |
Output is correct |
15 |
Correct |
432 ms |
330804 KB |
Output is correct |
16 |
Correct |
408 ms |
330688 KB |
Output is correct |
17 |
Correct |
286 ms |
129588 KB |
Output is correct |
18 |
Correct |
117 ms |
74132 KB |
Output is correct |
19 |
Correct |
160 ms |
128000 KB |
Output is correct |
20 |
Correct |
195 ms |
168316 KB |
Output is correct |
21 |
Correct |
303 ms |
275824 KB |
Output is correct |
22 |
Correct |
320 ms |
329720 KB |
Output is correct |
23 |
Correct |
116 ms |
74224 KB |
Output is correct |
24 |
Correct |
165 ms |
127948 KB |
Output is correct |
25 |
Correct |
342 ms |
329724 KB |
Output is correct |
26 |
Correct |
268 ms |
275812 KB |
Output is correct |
27 |
Correct |
216 ms |
75068 KB |
Output is correct |
28 |
Correct |
340 ms |
170492 KB |
Output is correct |
29 |
Correct |
344 ms |
170708 KB |
Output is correct |
30 |
Correct |
335 ms |
170188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
74228 KB |
Output is correct |
2 |
Correct |
100 ms |
74184 KB |
Output is correct |
3 |
Correct |
106 ms |
74212 KB |
Output is correct |
4 |
Correct |
108 ms |
74128 KB |
Output is correct |
5 |
Correct |
108 ms |
74196 KB |
Output is correct |
6 |
Correct |
104 ms |
74188 KB |
Output is correct |
7 |
Correct |
106 ms |
74224 KB |
Output is correct |
8 |
Correct |
229 ms |
74260 KB |
Output is correct |
9 |
Correct |
237 ms |
74232 KB |
Output is correct |
10 |
Correct |
207 ms |
74232 KB |
Output is correct |
11 |
Correct |
293 ms |
128004 KB |
Output is correct |
12 |
Correct |
105 ms |
74192 KB |
Output is correct |
13 |
Correct |
164 ms |
127992 KB |
Output is correct |
14 |
Correct |
211 ms |
168304 KB |
Output is correct |
15 |
Correct |
319 ms |
275788 KB |
Output is correct |
16 |
Correct |
323 ms |
329560 KB |
Output is correct |
17 |
Correct |
113 ms |
74248 KB |
Output is correct |
18 |
Correct |
162 ms |
128236 KB |
Output is correct |
19 |
Correct |
208 ms |
75144 KB |
Output is correct |
20 |
Correct |
346 ms |
170660 KB |
Output is correct |
21 |
Correct |
289 ms |
129876 KB |
Output is correct |
22 |
Correct |
334 ms |
169640 KB |
Output is correct |
23 |
Correct |
655 ms |
332016 KB |
Output is correct |
24 |
Correct |
289 ms |
169752 KB |
Output is correct |
25 |
Correct |
400 ms |
330860 KB |
Output is correct |
26 |
Correct |
417 ms |
330780 KB |
Output is correct |
27 |
Correct |
105 ms |
74228 KB |
Output is correct |
28 |
Correct |
158 ms |
127940 KB |
Output is correct |
29 |
Correct |
202 ms |
168316 KB |
Output is correct |
30 |
Correct |
268 ms |
275812 KB |
Output is correct |
31 |
Correct |
322 ms |
329572 KB |
Output is correct |
32 |
Correct |
111 ms |
74160 KB |
Output is correct |
33 |
Correct |
177 ms |
127948 KB |
Output is correct |
34 |
Correct |
328 ms |
329616 KB |
Output is correct |
35 |
Correct |
289 ms |
276024 KB |
Output is correct |
36 |
Correct |
201 ms |
75036 KB |
Output is correct |
37 |
Correct |
275 ms |
129532 KB |
Output is correct |
38 |
Correct |
344 ms |
170496 KB |
Output is correct |
39 |
Correct |
343 ms |
170680 KB |
Output is correct |
40 |
Correct |
338 ms |
170132 KB |
Output is correct |
41 |
Correct |
432 ms |
330804 KB |
Output is correct |
42 |
Correct |
408 ms |
330688 KB |
Output is correct |
43 |
Correct |
286 ms |
129588 KB |
Output is correct |
44 |
Correct |
117 ms |
74132 KB |
Output is correct |
45 |
Correct |
160 ms |
128000 KB |
Output is correct |
46 |
Correct |
195 ms |
168316 KB |
Output is correct |
47 |
Correct |
303 ms |
275824 KB |
Output is correct |
48 |
Correct |
320 ms |
329720 KB |
Output is correct |
49 |
Correct |
116 ms |
74224 KB |
Output is correct |
50 |
Correct |
165 ms |
127948 KB |
Output is correct |
51 |
Correct |
342 ms |
329724 KB |
Output is correct |
52 |
Correct |
268 ms |
275812 KB |
Output is correct |
53 |
Correct |
216 ms |
75068 KB |
Output is correct |
54 |
Correct |
340 ms |
170492 KB |
Output is correct |
55 |
Correct |
344 ms |
170708 KB |
Output is correct |
56 |
Correct |
335 ms |
170188 KB |
Output is correct |
57 |
Correct |
460 ms |
279292 KB |
Output is correct |
58 |
Correct |
234 ms |
75088 KB |
Output is correct |
59 |
Correct |
332 ms |
129808 KB |
Output is correct |