#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T>
bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
template<class T>
bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 50010, inf = 1e9, MAX_K = 5, MAX_L = 16;
int k, n, m, q;
#pragma GCC optimize("Ofast")
int dp[MAX_L][MAX_N][MAX_K][MAX_K];
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> k >> n >> m >> q;
for (int i = 0;i < MAX_L;++i) for (int j = 0;j < n;++j) for (int l = 0;l < k;++l) for (int m = 0;m < k;++m)
dp[i][j][l][m] = inf;
for (int a, b, t, i = 0;i < m;++i) {
cin >> a >> b >> t;
dp[0][a / k][a % k][b % k] = t;
}
for (int d = 1;1 << d <= n;++d) {
for (int i = 0;i < n / k;++i)
for (int j = 0;j < k;++j) for (int l = 0;l < k;++l)
for (int m = 0;m < k;++m)
chmin(dp[d][i][j][m], dp[d-1][i][j][l] + dp[d-1][i + (1<<(d-1))][l][m]);
}
auto qry = [&](int a, int b) {
if (a / k == b / k) return -1;
int dis = b / k - a / k;
int *mat = new int[k], *tmp = new int[k];
for (int i = 0;i < k;++i) mat[i] = inf;
mat[a % k] = 0;
for (int d = 0;1 << d <= dis;++d) if (dis >> d & 1) {
DE(d);
fill(tmp, tmp + k, inf);
for (int i = 0;i < k;++i) for (int j = 0;j < k;++j) {
chmin(tmp[j], mat[i] + dp[d][a / k][i][j]);
}
a += (1 << d) * k;
swap(tmp, mat);
}
return mat[b % k] == inf ? -1 : mat[b % k];
};
while (q--) {
int a, b;
cin >> a >> b;
cout << qry(a, b) << '\n';
}
}
Compilation message
toll.cpp: In lambda function:
toll.cpp:18:17: warning: statement has no effect [-Wunused-value]
18 | #define DE(...) 0
| ^
toll.cpp:56:4: note: in expansion of macro 'DE'
56 | DE(d);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
80364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
2156 KB |
Output is correct |
6 |
Correct |
2 ms |
2156 KB |
Output is correct |
7 |
Correct |
3 ms |
2156 KB |
Output is correct |
8 |
Correct |
86 ms |
80236 KB |
Output is correct |
9 |
Correct |
76 ms |
80236 KB |
Output is correct |
10 |
Correct |
57 ms |
79468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
80912 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
6 ms |
2796 KB |
Output is correct |
8 |
Correct |
7 ms |
2796 KB |
Output is correct |
9 |
Correct |
75 ms |
80364 KB |
Output is correct |
10 |
Correct |
121 ms |
81516 KB |
Output is correct |
11 |
Correct |
90 ms |
81004 KB |
Output is correct |
12 |
Correct |
88 ms |
80620 KB |
Output is correct |
13 |
Correct |
101 ms |
50028 KB |
Output is correct |
14 |
Correct |
69 ms |
49132 KB |
Output is correct |
15 |
Correct |
69 ms |
48876 KB |
Output is correct |
16 |
Correct |
68 ms |
48876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
2028 KB |
Output is correct |
8 |
Correct |
3 ms |
2156 KB |
Output is correct |
9 |
Correct |
3 ms |
2028 KB |
Output is correct |
10 |
Correct |
67 ms |
79468 KB |
Output is correct |
11 |
Correct |
94 ms |
80236 KB |
Output is correct |
12 |
Correct |
99 ms |
80748 KB |
Output is correct |
13 |
Correct |
103 ms |
81004 KB |
Output is correct |
14 |
Correct |
96 ms |
80524 KB |
Output is correct |
15 |
Correct |
72 ms |
48748 KB |
Output is correct |
16 |
Correct |
68 ms |
48640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
2028 KB |
Output is correct |
8 |
Correct |
3 ms |
2156 KB |
Output is correct |
9 |
Correct |
3 ms |
2028 KB |
Output is correct |
10 |
Correct |
67 ms |
79468 KB |
Output is correct |
11 |
Correct |
94 ms |
80236 KB |
Output is correct |
12 |
Correct |
99 ms |
80748 KB |
Output is correct |
13 |
Correct |
103 ms |
81004 KB |
Output is correct |
14 |
Correct |
96 ms |
80524 KB |
Output is correct |
15 |
Correct |
72 ms |
48748 KB |
Output is correct |
16 |
Correct |
68 ms |
48640 KB |
Output is correct |
17 |
Correct |
85 ms |
80372 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
3 ms |
2284 KB |
Output is correct |
24 |
Correct |
4 ms |
2284 KB |
Output is correct |
25 |
Correct |
5 ms |
2284 KB |
Output is correct |
26 |
Correct |
4 ms |
2284 KB |
Output is correct |
27 |
Correct |
77 ms |
79748 KB |
Output is correct |
28 |
Correct |
108 ms |
81132 KB |
Output is correct |
29 |
Correct |
107 ms |
81352 KB |
Output is correct |
30 |
Correct |
98 ms |
80748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
80364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
2156 KB |
Output is correct |
6 |
Correct |
2 ms |
2156 KB |
Output is correct |
7 |
Correct |
3 ms |
2156 KB |
Output is correct |
8 |
Correct |
86 ms |
80236 KB |
Output is correct |
9 |
Correct |
76 ms |
80236 KB |
Output is correct |
10 |
Correct |
57 ms |
79468 KB |
Output is correct |
11 |
Correct |
87 ms |
80912 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
6 ms |
2796 KB |
Output is correct |
18 |
Correct |
7 ms |
2796 KB |
Output is correct |
19 |
Correct |
75 ms |
80364 KB |
Output is correct |
20 |
Correct |
121 ms |
81516 KB |
Output is correct |
21 |
Correct |
90 ms |
81004 KB |
Output is correct |
22 |
Correct |
88 ms |
80620 KB |
Output is correct |
23 |
Correct |
101 ms |
50028 KB |
Output is correct |
24 |
Correct |
69 ms |
49132 KB |
Output is correct |
25 |
Correct |
69 ms |
48876 KB |
Output is correct |
26 |
Correct |
68 ms |
48876 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
2 ms |
2028 KB |
Output is correct |
33 |
Correct |
2 ms |
2028 KB |
Output is correct |
34 |
Correct |
3 ms |
2156 KB |
Output is correct |
35 |
Correct |
3 ms |
2028 KB |
Output is correct |
36 |
Correct |
67 ms |
79468 KB |
Output is correct |
37 |
Correct |
94 ms |
80236 KB |
Output is correct |
38 |
Correct |
99 ms |
80748 KB |
Output is correct |
39 |
Correct |
103 ms |
81004 KB |
Output is correct |
40 |
Correct |
96 ms |
80524 KB |
Output is correct |
41 |
Correct |
72 ms |
48748 KB |
Output is correct |
42 |
Correct |
68 ms |
48640 KB |
Output is correct |
43 |
Correct |
85 ms |
80372 KB |
Output is correct |
44 |
Correct |
1 ms |
492 KB |
Output is correct |
45 |
Correct |
1 ms |
492 KB |
Output is correct |
46 |
Correct |
1 ms |
492 KB |
Output is correct |
47 |
Correct |
1 ms |
492 KB |
Output is correct |
48 |
Correct |
1 ms |
492 KB |
Output is correct |
49 |
Correct |
3 ms |
2284 KB |
Output is correct |
50 |
Correct |
4 ms |
2284 KB |
Output is correct |
51 |
Correct |
5 ms |
2284 KB |
Output is correct |
52 |
Correct |
4 ms |
2284 KB |
Output is correct |
53 |
Correct |
77 ms |
79748 KB |
Output is correct |
54 |
Correct |
108 ms |
81132 KB |
Output is correct |
55 |
Correct |
107 ms |
81352 KB |
Output is correct |
56 |
Correct |
98 ms |
80748 KB |
Output is correct |
57 |
Correct |
128 ms |
82668 KB |
Output is correct |
58 |
Correct |
78 ms |
80236 KB |
Output is correct |
59 |
Correct |
95 ms |
81132 KB |
Output is correct |