#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll M = 998244353;
// const ll M = 1e9 + 7;
#define all(v) (v).begin(), (v).end()
#define ff first
#define ss second
const ll INFL = 1e18;
const int INF = 1e9;
int lift[17][50000][5];
void solve() {
int K, N, M, O;
cin >> K >> N >> M >> O;
for (int i=0; i<17; i++) {
for (int j=0; j<N; j++) {
for (int k=0; k<K; k++) {
lift[i][j][k] = 1e9;
}
}
}
for (int i=0; i<M; i++) {
int a, b, t;
cin >> a >> b >> t;
lift[0][a][b%K] = t;
}
for (int sz=1; sz<17; sz++) {
for (int a=0; a<N; a++) {
for (int to=0; to<K; to++) {
for (int m=0; m<K; m++) {
int b = (a-(a%K))+(1<<(sz-1))*K+m;
if (b>=N) break;
lift[sz][a][to] = min(lift[sz][a][to], lift[sz-1][a][m]+lift[sz-1][b][to]);
}
}
}
}
while (O--) {
int a, b;
cin >> a >> b;
int d = ((b-(b%K))-(a-(a%K)))/K;
if (d==0) {
cout << "-1\n";
continue;
}
int mn[5];
int nxt[5];
d--;
for (int i=0; i<K; i++) mn[i] = lift[0][a][i];
int cur = a-(a%K)+K;
for (int i=0; i<17; i++) {
if (!(d>>i&1)) continue;
for (int j=0; j<K; j++) nxt[j] = 1e9;
for (int j=0; j<K; j++) {
for (int k=0; k<K; k++) {
nxt[k] = min(nxt[k], mn[j]+lift[i][cur+j][k]);
}
}
cur += (1<<i)*K;
d -= 1<<i;
for (int j=0; j<K; j++) {
mn[j] = nxt[j];
}
}
assert(d==0);
assert(cur==b-(b%K));
int ans = mn[b%K];
if (ans != 1e9) cout << ans << '\n';
else cout << "-1\n";
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int tc = 1;
// cin >> tc;
while (tc--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
18000 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
14 ms |
17948 KB |
Output is correct |
9 |
Correct |
14 ms |
17972 KB |
Output is correct |
10 |
Correct |
8 ms |
17208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
18744 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
856 KB |
Output is correct |
9 |
Correct |
18 ms |
17756 KB |
Output is correct |
10 |
Correct |
33 ms |
19284 KB |
Output is correct |
11 |
Correct |
25 ms |
18732 KB |
Output is correct |
12 |
Correct |
26 ms |
18224 KB |
Output is correct |
13 |
Correct |
34 ms |
15192 KB |
Output is correct |
14 |
Correct |
19 ms |
14172 KB |
Output is correct |
15 |
Correct |
25 ms |
13916 KB |
Output is correct |
16 |
Correct |
24 ms |
13912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
13 ms |
17752 KB |
Output is correct |
11 |
Correct |
21 ms |
18524 KB |
Output is correct |
12 |
Correct |
29 ms |
19124 KB |
Output is correct |
13 |
Correct |
31 ms |
19292 KB |
Output is correct |
14 |
Correct |
26 ms |
18776 KB |
Output is correct |
15 |
Correct |
23 ms |
13912 KB |
Output is correct |
16 |
Correct |
24 ms |
13916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
13 ms |
17752 KB |
Output is correct |
11 |
Correct |
21 ms |
18524 KB |
Output is correct |
12 |
Correct |
29 ms |
19124 KB |
Output is correct |
13 |
Correct |
31 ms |
19292 KB |
Output is correct |
14 |
Correct |
26 ms |
18776 KB |
Output is correct |
15 |
Correct |
23 ms |
13912 KB |
Output is correct |
16 |
Correct |
24 ms |
13916 KB |
Output is correct |
17 |
Correct |
22 ms |
18520 KB |
Output is correct |
18 |
Correct |
1 ms |
6584 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
736 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
856 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
12 ms |
17756 KB |
Output is correct |
28 |
Correct |
31 ms |
19256 KB |
Output is correct |
29 |
Correct |
31 ms |
19384 KB |
Output is correct |
30 |
Correct |
28 ms |
18932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
18000 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
14 ms |
17948 KB |
Output is correct |
9 |
Correct |
14 ms |
17972 KB |
Output is correct |
10 |
Correct |
8 ms |
17208 KB |
Output is correct |
11 |
Correct |
23 ms |
18744 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
856 KB |
Output is correct |
19 |
Correct |
18 ms |
17756 KB |
Output is correct |
20 |
Correct |
33 ms |
19284 KB |
Output is correct |
21 |
Correct |
25 ms |
18732 KB |
Output is correct |
22 |
Correct |
26 ms |
18224 KB |
Output is correct |
23 |
Correct |
34 ms |
15192 KB |
Output is correct |
24 |
Correct |
19 ms |
14172 KB |
Output is correct |
25 |
Correct |
25 ms |
13916 KB |
Output is correct |
26 |
Correct |
24 ms |
13912 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
1 ms |
6748 KB |
Output is correct |
36 |
Correct |
13 ms |
17752 KB |
Output is correct |
37 |
Correct |
21 ms |
18524 KB |
Output is correct |
38 |
Correct |
29 ms |
19124 KB |
Output is correct |
39 |
Correct |
31 ms |
19292 KB |
Output is correct |
40 |
Correct |
26 ms |
18776 KB |
Output is correct |
41 |
Correct |
23 ms |
13912 KB |
Output is correct |
42 |
Correct |
24 ms |
13916 KB |
Output is correct |
43 |
Correct |
22 ms |
18520 KB |
Output is correct |
44 |
Correct |
1 ms |
6584 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
736 KB |
Output is correct |
50 |
Correct |
1 ms |
860 KB |
Output is correct |
51 |
Correct |
2 ms |
856 KB |
Output is correct |
52 |
Correct |
2 ms |
6748 KB |
Output is correct |
53 |
Correct |
12 ms |
17756 KB |
Output is correct |
54 |
Correct |
31 ms |
19256 KB |
Output is correct |
55 |
Correct |
31 ms |
19384 KB |
Output is correct |
56 |
Correct |
28 ms |
18932 KB |
Output is correct |
57 |
Correct |
44 ms |
20316 KB |
Output is correct |
58 |
Correct |
17 ms |
18016 KB |
Output is correct |
59 |
Correct |
23 ms |
18744 KB |
Output is correct |