#include<iostream>
using namespace std;
typedef long long ll;
const int N = 50001, C = 6, K = 17;
const ll inf = 1e18;
ll G[N][C];
ll dis[N][C][K];
int main()
{
int k, n, m, o;
cin >> k >> n >> m >> o;
for(int i = 0; i < n; i++)
for(int j = 0; j < k; j++)
G[i][j] = inf;
for(int i = 0; i < n; i ++)
for(int j = 0; j < k; j++)
for(int l = 0; l < K; l++)
dis[i][j][k] = inf;
for(int i = 0; i < m; i++)
{
ll a, b, t;
cin >> a >> b >> t;
G[a][b % k] = min(G[a][b % k], t);
}
for(int i = n - 1; i >= 0; i--)
{
for(int j = 0; j < k; j++)
dis[i][j][0] = G[i][j];
for(int p = 1; p < K; p++)
for(int j = 0; j < k && (1 << p) * k + i - i%k + j < n; j++)
{
ll mx = inf;
for(int l = 0; l < k; l++)
{
int v = (1 << (p-1)) * k + i - i%k + l;
if(v >= n) break;
mx = min(mx, dis[i][l][p - 1] + dis[v][j][p-1]);
}
dis[i][j][p] = mx;
}
}
while(o--)
{
int a, b;
cin >> a >> b;
int d = (b / k) - (a / k);
if(d == 0)
{
cout << -1 << '\n';
continue;
}
ll dist[k], v[k];
int f = __builtin_ctz(d);
for(int i = 0; i < k; i++)
{
dist[i] = dis[a][i][f];
v[i] = (a - a % k) + (1 << f) * k + i;
}
for(int l = f + 1; l < K; l++)
if((1 << l) & d)
{
ll newD[k];
for(int i = 0; i < k; i++)
newD[i] = inf;
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
newD[j] = min(dist[i] + dis[v[i]][j][l], newD[j]);
for(int i = 0; i < k; i++)
dist[i] = newD[i], v[i] += (1 << l) * k;
}
cout << (dist[b % k] >= inf ? -1 : dist[b%k]) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
43220 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
4 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
4700 KB |
Output is correct |
8 |
Correct |
65 ms |
43196 KB |
Output is correct |
9 |
Correct |
64 ms |
43208 KB |
Output is correct |
10 |
Correct |
33 ms |
42828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
43984 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2496 KB |
Output is correct |
7 |
Correct |
22 ms |
4804 KB |
Output is correct |
8 |
Correct |
19 ms |
4700 KB |
Output is correct |
9 |
Correct |
60 ms |
43316 KB |
Output is correct |
10 |
Correct |
123 ms |
44080 KB |
Output is correct |
11 |
Correct |
92 ms |
43944 KB |
Output is correct |
12 |
Correct |
89 ms |
43600 KB |
Output is correct |
13 |
Correct |
108 ms |
28976 KB |
Output is correct |
14 |
Correct |
68 ms |
28536 KB |
Output is correct |
15 |
Correct |
67 ms |
28348 KB |
Output is correct |
16 |
Correct |
75 ms |
28332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2496 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
2 ms |
4696 KB |
Output is correct |
8 |
Correct |
4 ms |
4792 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
10 |
Correct |
44 ms |
43220 KB |
Output is correct |
11 |
Correct |
76 ms |
43940 KB |
Output is correct |
12 |
Correct |
102 ms |
44068 KB |
Output is correct |
13 |
Correct |
113 ms |
44368 KB |
Output is correct |
14 |
Correct |
125 ms |
43948 KB |
Output is correct |
15 |
Correct |
70 ms |
28264 KB |
Output is correct |
16 |
Correct |
59 ms |
28244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2496 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
2 ms |
4696 KB |
Output is correct |
8 |
Correct |
4 ms |
4792 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
10 |
Correct |
44 ms |
43220 KB |
Output is correct |
11 |
Correct |
76 ms |
43940 KB |
Output is correct |
12 |
Correct |
102 ms |
44068 KB |
Output is correct |
13 |
Correct |
113 ms |
44368 KB |
Output is correct |
14 |
Correct |
125 ms |
43948 KB |
Output is correct |
15 |
Correct |
70 ms |
28264 KB |
Output is correct |
16 |
Correct |
59 ms |
28244 KB |
Output is correct |
17 |
Correct |
87 ms |
43956 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2552 KB |
Output is correct |
23 |
Correct |
6 ms |
4700 KB |
Output is correct |
24 |
Correct |
6 ms |
4788 KB |
Output is correct |
25 |
Correct |
8 ms |
4700 KB |
Output is correct |
26 |
Correct |
8 ms |
4696 KB |
Output is correct |
27 |
Correct |
49 ms |
43208 KB |
Output is correct |
28 |
Correct |
105 ms |
44212 KB |
Output is correct |
29 |
Correct |
143 ms |
44884 KB |
Output is correct |
30 |
Correct |
99 ms |
43924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
43220 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
4 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
4700 KB |
Output is correct |
8 |
Correct |
65 ms |
43196 KB |
Output is correct |
9 |
Correct |
64 ms |
43208 KB |
Output is correct |
10 |
Correct |
33 ms |
42828 KB |
Output is correct |
11 |
Correct |
97 ms |
43984 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2496 KB |
Output is correct |
17 |
Correct |
22 ms |
4804 KB |
Output is correct |
18 |
Correct |
19 ms |
4700 KB |
Output is correct |
19 |
Correct |
60 ms |
43316 KB |
Output is correct |
20 |
Correct |
123 ms |
44080 KB |
Output is correct |
21 |
Correct |
92 ms |
43944 KB |
Output is correct |
22 |
Correct |
89 ms |
43600 KB |
Output is correct |
23 |
Correct |
108 ms |
28976 KB |
Output is correct |
24 |
Correct |
68 ms |
28536 KB |
Output is correct |
25 |
Correct |
67 ms |
28348 KB |
Output is correct |
26 |
Correct |
75 ms |
28332 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2496 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
4696 KB |
Output is correct |
33 |
Correct |
2 ms |
4696 KB |
Output is correct |
34 |
Correct |
4 ms |
4792 KB |
Output is correct |
35 |
Correct |
3 ms |
4700 KB |
Output is correct |
36 |
Correct |
44 ms |
43220 KB |
Output is correct |
37 |
Correct |
76 ms |
43940 KB |
Output is correct |
38 |
Correct |
102 ms |
44068 KB |
Output is correct |
39 |
Correct |
113 ms |
44368 KB |
Output is correct |
40 |
Correct |
125 ms |
43948 KB |
Output is correct |
41 |
Correct |
70 ms |
28264 KB |
Output is correct |
42 |
Correct |
59 ms |
28244 KB |
Output is correct |
43 |
Correct |
87 ms |
43956 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2392 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2552 KB |
Output is correct |
49 |
Correct |
6 ms |
4700 KB |
Output is correct |
50 |
Correct |
6 ms |
4788 KB |
Output is correct |
51 |
Correct |
8 ms |
4700 KB |
Output is correct |
52 |
Correct |
8 ms |
4696 KB |
Output is correct |
53 |
Correct |
49 ms |
43208 KB |
Output is correct |
54 |
Correct |
105 ms |
44212 KB |
Output is correct |
55 |
Correct |
143 ms |
44884 KB |
Output is correct |
56 |
Correct |
99 ms |
43924 KB |
Output is correct |
57 |
Correct |
174 ms |
44624 KB |
Output is correct |
58 |
Correct |
65 ms |
43344 KB |
Output is correct |
59 |
Correct |
102 ms |
43856 KB |
Output is correct |