#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
#define F first
#define S second
#define pb push_back
#define lc (ind << 1)
#define rc (lc | 1)
#define md ((b + e) >> 1)
const ll N = 5e4 + 5;
const ll M = 5 + 3;
const ll LOG = 17;
const ll INF = 1e18 + 5;
const ll ziad = 1e9 + 5;
const ll MOD = 1e9 + 7;
int k, n, m, q;
ll dis[N][LOG][M];
ll dist[2][M];
inline void build(){
int ghar = (n - 1) / k;
for (int i = 1; i < LOG; i ++){
for (int A = 0; A < n; A ++){
int X = A / k;
int Y = X + (1 << (i - 1));
int Z = Y + (1 << (i - 1));
if (ghar < Z) break;
for (int j = 0; j < k; j ++){
int B = Y * k + j;
if (n <= B) break;
for (int l = 0; l < k; l ++){
int C = Z * k + l;
if (n <= C) break;
dis[A][i][l] = min(dis[A][i][l], dis[A][i - 1][j] + dis[B][i - 1][l]);
}
}
}
}
return;
}
inline ll get(int s, int e){
if (e / k <= s / k) return -1;
memset(dist, 63, sizeof dist);
dist[0][s % k] = 0;
ll state = s / k;
ll dif = e / k - s / k;
for (int i = LOG - 1; i >= 0; i --){
if ((dif >> i) & 1){
for (int a = 0; a < k; a ++){
int A = state * k + a;
if (n <= A) break;
for (int b = 0; b < k; b ++){
int B = (state + (1 << i)) * k + b;
if (n <= B) break;
dist[1][b] = min(dist[1][b], dist[0][a] + dis[A][i][b]);
}
}
for (int j = 0; j < k; j ++){
dist[0][j] = dist[1][j];
dist[1][j] = INF;
}
state += (1 << i);
}
}
if (ziad < dist[0][e % k]) return -1;
return dist[0][e % k];
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(dis, 63, sizeof dis);
cin >> k >> n >> m >> q;
for (int i = 0; i < m; i ++){
int u, v, w;
cin >> u >> v >> w;
dis[u][0][v % k] = w;
}
build();
while (q --){
int s, e;
cin >> s >> e;
cout << get(s, e) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
53516 KB |
Output is correct |
2 |
Correct |
22 ms |
53452 KB |
Output is correct |
3 |
Correct |
23 ms |
53504 KB |
Output is correct |
4 |
Correct |
25 ms |
53452 KB |
Output is correct |
5 |
Correct |
25 ms |
53548 KB |
Output is correct |
6 |
Correct |
25 ms |
53496 KB |
Output is correct |
7 |
Correct |
24 ms |
53492 KB |
Output is correct |
8 |
Correct |
74 ms |
54412 KB |
Output is correct |
9 |
Correct |
68 ms |
54408 KB |
Output is correct |
10 |
Correct |
51 ms |
53676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
53528 KB |
Output is correct |
2 |
Correct |
24 ms |
53452 KB |
Output is correct |
3 |
Correct |
24 ms |
53480 KB |
Output is correct |
4 |
Correct |
22 ms |
53516 KB |
Output is correct |
5 |
Correct |
22 ms |
53456 KB |
Output is correct |
6 |
Correct |
24 ms |
53524 KB |
Output is correct |
7 |
Correct |
27 ms |
53560 KB |
Output is correct |
8 |
Correct |
27 ms |
53696 KB |
Output is correct |
9 |
Correct |
67 ms |
54408 KB |
Output is correct |
10 |
Correct |
103 ms |
55236 KB |
Output is correct |
11 |
Correct |
106 ms |
55176 KB |
Output is correct |
12 |
Correct |
87 ms |
54804 KB |
Output is correct |
13 |
Correct |
132 ms |
55380 KB |
Output is correct |
14 |
Correct |
65 ms |
54936 KB |
Output is correct |
15 |
Correct |
92 ms |
54668 KB |
Output is correct |
16 |
Correct |
94 ms |
54772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
53516 KB |
Output is correct |
2 |
Correct |
23 ms |
53452 KB |
Output is correct |
3 |
Correct |
23 ms |
53532 KB |
Output is correct |
4 |
Correct |
27 ms |
53448 KB |
Output is correct |
5 |
Correct |
23 ms |
53544 KB |
Output is correct |
6 |
Correct |
24 ms |
53456 KB |
Output is correct |
7 |
Correct |
23 ms |
53452 KB |
Output is correct |
8 |
Correct |
24 ms |
53572 KB |
Output is correct |
9 |
Correct |
24 ms |
53584 KB |
Output is correct |
10 |
Correct |
60 ms |
54256 KB |
Output is correct |
11 |
Correct |
80 ms |
54980 KB |
Output is correct |
12 |
Correct |
97 ms |
55236 KB |
Output is correct |
13 |
Correct |
102 ms |
55196 KB |
Output is correct |
14 |
Correct |
86 ms |
55192 KB |
Output is correct |
15 |
Correct |
90 ms |
54664 KB |
Output is correct |
16 |
Correct |
89 ms |
54704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
53516 KB |
Output is correct |
2 |
Correct |
23 ms |
53452 KB |
Output is correct |
3 |
Correct |
23 ms |
53532 KB |
Output is correct |
4 |
Correct |
27 ms |
53448 KB |
Output is correct |
5 |
Correct |
23 ms |
53544 KB |
Output is correct |
6 |
Correct |
24 ms |
53456 KB |
Output is correct |
7 |
Correct |
23 ms |
53452 KB |
Output is correct |
8 |
Correct |
24 ms |
53572 KB |
Output is correct |
9 |
Correct |
24 ms |
53584 KB |
Output is correct |
10 |
Correct |
60 ms |
54256 KB |
Output is correct |
11 |
Correct |
80 ms |
54980 KB |
Output is correct |
12 |
Correct |
97 ms |
55236 KB |
Output is correct |
13 |
Correct |
102 ms |
55196 KB |
Output is correct |
14 |
Correct |
86 ms |
55192 KB |
Output is correct |
15 |
Correct |
90 ms |
54664 KB |
Output is correct |
16 |
Correct |
89 ms |
54704 KB |
Output is correct |
17 |
Correct |
81 ms |
55068 KB |
Output is correct |
18 |
Correct |
25 ms |
53540 KB |
Output is correct |
19 |
Correct |
25 ms |
53580 KB |
Output is correct |
20 |
Correct |
24 ms |
53452 KB |
Output is correct |
21 |
Correct |
24 ms |
53520 KB |
Output is correct |
22 |
Correct |
24 ms |
53444 KB |
Output is correct |
23 |
Correct |
27 ms |
53528 KB |
Output is correct |
24 |
Correct |
27 ms |
53508 KB |
Output is correct |
25 |
Correct |
27 ms |
53644 KB |
Output is correct |
26 |
Correct |
27 ms |
53564 KB |
Output is correct |
27 |
Correct |
65 ms |
54340 KB |
Output is correct |
28 |
Correct |
112 ms |
55176 KB |
Output is correct |
29 |
Correct |
101 ms |
55268 KB |
Output is correct |
30 |
Correct |
101 ms |
55232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
53516 KB |
Output is correct |
2 |
Correct |
22 ms |
53452 KB |
Output is correct |
3 |
Correct |
23 ms |
53504 KB |
Output is correct |
4 |
Correct |
25 ms |
53452 KB |
Output is correct |
5 |
Correct |
25 ms |
53548 KB |
Output is correct |
6 |
Correct |
25 ms |
53496 KB |
Output is correct |
7 |
Correct |
24 ms |
53492 KB |
Output is correct |
8 |
Correct |
74 ms |
54412 KB |
Output is correct |
9 |
Correct |
68 ms |
54408 KB |
Output is correct |
10 |
Correct |
51 ms |
53676 KB |
Output is correct |
11 |
Correct |
88 ms |
53528 KB |
Output is correct |
12 |
Correct |
24 ms |
53452 KB |
Output is correct |
13 |
Correct |
24 ms |
53480 KB |
Output is correct |
14 |
Correct |
22 ms |
53516 KB |
Output is correct |
15 |
Correct |
22 ms |
53456 KB |
Output is correct |
16 |
Correct |
24 ms |
53524 KB |
Output is correct |
17 |
Correct |
27 ms |
53560 KB |
Output is correct |
18 |
Correct |
27 ms |
53696 KB |
Output is correct |
19 |
Correct |
67 ms |
54408 KB |
Output is correct |
20 |
Correct |
103 ms |
55236 KB |
Output is correct |
21 |
Correct |
106 ms |
55176 KB |
Output is correct |
22 |
Correct |
87 ms |
54804 KB |
Output is correct |
23 |
Correct |
132 ms |
55380 KB |
Output is correct |
24 |
Correct |
65 ms |
54936 KB |
Output is correct |
25 |
Correct |
92 ms |
54668 KB |
Output is correct |
26 |
Correct |
94 ms |
54772 KB |
Output is correct |
27 |
Correct |
29 ms |
53516 KB |
Output is correct |
28 |
Correct |
23 ms |
53452 KB |
Output is correct |
29 |
Correct |
23 ms |
53532 KB |
Output is correct |
30 |
Correct |
27 ms |
53448 KB |
Output is correct |
31 |
Correct |
23 ms |
53544 KB |
Output is correct |
32 |
Correct |
24 ms |
53456 KB |
Output is correct |
33 |
Correct |
23 ms |
53452 KB |
Output is correct |
34 |
Correct |
24 ms |
53572 KB |
Output is correct |
35 |
Correct |
24 ms |
53584 KB |
Output is correct |
36 |
Correct |
60 ms |
54256 KB |
Output is correct |
37 |
Correct |
80 ms |
54980 KB |
Output is correct |
38 |
Correct |
97 ms |
55236 KB |
Output is correct |
39 |
Correct |
102 ms |
55196 KB |
Output is correct |
40 |
Correct |
86 ms |
55192 KB |
Output is correct |
41 |
Correct |
90 ms |
54664 KB |
Output is correct |
42 |
Correct |
89 ms |
54704 KB |
Output is correct |
43 |
Correct |
81 ms |
55068 KB |
Output is correct |
44 |
Correct |
25 ms |
53540 KB |
Output is correct |
45 |
Correct |
25 ms |
53580 KB |
Output is correct |
46 |
Correct |
24 ms |
53452 KB |
Output is correct |
47 |
Correct |
24 ms |
53520 KB |
Output is correct |
48 |
Correct |
24 ms |
53444 KB |
Output is correct |
49 |
Correct |
27 ms |
53528 KB |
Output is correct |
50 |
Correct |
27 ms |
53508 KB |
Output is correct |
51 |
Correct |
27 ms |
53644 KB |
Output is correct |
52 |
Correct |
27 ms |
53564 KB |
Output is correct |
53 |
Correct |
65 ms |
54340 KB |
Output is correct |
54 |
Correct |
112 ms |
55176 KB |
Output is correct |
55 |
Correct |
101 ms |
55268 KB |
Output is correct |
56 |
Correct |
101 ms |
55232 KB |
Output is correct |
57 |
Correct |
182 ms |
55268 KB |
Output is correct |
58 |
Correct |
71 ms |
54448 KB |
Output is correct |
59 |
Correct |
90 ms |
55304 KB |
Output is correct |