#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma optimize("O2")
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define sp << " " <<
#define F(i, s, e) for(int i = s; i < e; i++)
#define R(i, s, e) for(int i = s; i >= e; i--)
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define vb vector<bool>
#define endl '\n'
#define Tree tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update>
#define vpi vector<pii>
const int N = 5e4 + 5;
const int mod = 1e9 + 7;
const int inf = 1e15;
int dat[16][N][5][5], mask[N];
int k, n, m, q;
vvi go(N, vi(5, inf)), rgo(N, vi(5, inf));
void divi(int l, int r, int layer) {
if(l == r) return;
// calculate middle
int mid = (l + r) >> 1;
// calculate dat
// base cases
F(i, 0, k) {
dat[layer][mid][i][i] = 0;
}
F(i, mid+1, r+1) {
F(to, 0, k) {
F(from, 0, k) {
F(middle, 0, k) {
if(rgo[i*k+to][middle] < inf) dat[layer][i][from][to] = min(dat[layer][i][from][to], \
dat[layer][i-1][from][middle] + rgo[i*k+to][middle]);
}
}
}
}
R(i, mid-1, l) {
F(to, 0, k) {
F(from, 0, k) {
F(middle, 0, k) {
if(go[i*k+from][middle] < inf) dat[layer][i][from][to] = min(dat[layer][i][from][to], \
dat[layer][i+1][middle][to] + go[i*k+from][middle]);
}
}
}
}
// calculate mask
F(i, mid+1, r+1) {
mask[i] |= (1 << layer);
}
// branch into left and right segments
divi(l, mid, layer + 1);
divi(mid+1, r, layer + 1);
}
void solve() {
F(i, 0, 16) F(j, 0, N) F(k, 0, 5) F(l, 0, 5) dat[i][j][k][l] = inf;
cin >> k >> n >> m >> q;
F(i, 0, m) {
int u, v, w;
cin >> u >> v >> w;
if(u > v) swap(u, v);
go[u][v % k] = w;
rgo[v][u % k] = w;
}
int blockSize = (n + k - 1) / k;
divi(0, blockSize - 1, 0);
F(i, 0, q) {
int u, v;
cin >> u >> v;
int lu = u / k, lv = v / k;
if(u == v) cout << 0 << endl;
else if(lu >= lv) {
cout << -1 << endl;
} else {
int level = __builtin_ctz(mask[lu] ^ mask[lv]);
int mn = inf;
F(middle, 0, k) {
mn = min(mn, dat[level][lu][u % k][middle] + dat[level][lv][middle][v % k]);
}
if(mn < inf) cout << mn << endl;
else cout << -1 << endl;
}
}
}
int32_t main() {
cin.tie(0); ios_base::sync_with_stdio(0);
#ifdef Local
freopen("io.in", "r", stdin);
freopen("io.out", "w", stdout);
#endif
int t = 1;
//cin >> t;
while(t--) solve();
}
Compilation message
toll.cpp:4: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
4 | #pragma optimize("O2")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
165460 KB |
Output is correct |
2 |
Correct |
19 ms |
164148 KB |
Output is correct |
3 |
Correct |
18 ms |
164188 KB |
Output is correct |
4 |
Correct |
19 ms |
164184 KB |
Output is correct |
5 |
Correct |
19 ms |
164168 KB |
Output is correct |
6 |
Correct |
19 ms |
164184 KB |
Output is correct |
7 |
Correct |
19 ms |
164188 KB |
Output is correct |
8 |
Correct |
43 ms |
165188 KB |
Output is correct |
9 |
Correct |
44 ms |
165240 KB |
Output is correct |
10 |
Correct |
26 ms |
164444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
165952 KB |
Output is correct |
2 |
Correct |
20 ms |
164188 KB |
Output is correct |
3 |
Correct |
19 ms |
164264 KB |
Output is correct |
4 |
Correct |
19 ms |
164188 KB |
Output is correct |
5 |
Correct |
18 ms |
164188 KB |
Output is correct |
6 |
Correct |
21 ms |
164180 KB |
Output is correct |
7 |
Correct |
22 ms |
164184 KB |
Output is correct |
8 |
Correct |
23 ms |
164436 KB |
Output is correct |
9 |
Correct |
41 ms |
165212 KB |
Output is correct |
10 |
Correct |
52 ms |
166680 KB |
Output is correct |
11 |
Correct |
48 ms |
166224 KB |
Output is correct |
12 |
Correct |
48 ms |
165712 KB |
Output is correct |
13 |
Correct |
51 ms |
166612 KB |
Output is correct |
14 |
Correct |
39 ms |
165716 KB |
Output is correct |
15 |
Correct |
57 ms |
165568 KB |
Output is correct |
16 |
Correct |
59 ms |
165496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
164188 KB |
Output is correct |
2 |
Correct |
19 ms |
164188 KB |
Output is correct |
3 |
Correct |
19 ms |
164084 KB |
Output is correct |
4 |
Correct |
19 ms |
164188 KB |
Output is correct |
5 |
Correct |
19 ms |
164184 KB |
Output is correct |
6 |
Correct |
22 ms |
164276 KB |
Output is correct |
7 |
Correct |
19 ms |
164184 KB |
Output is correct |
8 |
Correct |
19 ms |
164188 KB |
Output is correct |
9 |
Correct |
21 ms |
164236 KB |
Output is correct |
10 |
Correct |
45 ms |
165068 KB |
Output is correct |
11 |
Correct |
44 ms |
165968 KB |
Output is correct |
12 |
Correct |
53 ms |
166484 KB |
Output is correct |
13 |
Correct |
54 ms |
166736 KB |
Output is correct |
14 |
Correct |
52 ms |
166224 KB |
Output is correct |
15 |
Correct |
52 ms |
165460 KB |
Output is correct |
16 |
Correct |
46 ms |
165520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
164188 KB |
Output is correct |
2 |
Correct |
19 ms |
164188 KB |
Output is correct |
3 |
Correct |
19 ms |
164084 KB |
Output is correct |
4 |
Correct |
19 ms |
164188 KB |
Output is correct |
5 |
Correct |
19 ms |
164184 KB |
Output is correct |
6 |
Correct |
22 ms |
164276 KB |
Output is correct |
7 |
Correct |
19 ms |
164184 KB |
Output is correct |
8 |
Correct |
19 ms |
164188 KB |
Output is correct |
9 |
Correct |
21 ms |
164236 KB |
Output is correct |
10 |
Correct |
45 ms |
165068 KB |
Output is correct |
11 |
Correct |
44 ms |
165968 KB |
Output is correct |
12 |
Correct |
53 ms |
166484 KB |
Output is correct |
13 |
Correct |
54 ms |
166736 KB |
Output is correct |
14 |
Correct |
52 ms |
166224 KB |
Output is correct |
15 |
Correct |
52 ms |
165460 KB |
Output is correct |
16 |
Correct |
46 ms |
165520 KB |
Output is correct |
17 |
Correct |
55 ms |
165972 KB |
Output is correct |
18 |
Correct |
18 ms |
164188 KB |
Output is correct |
19 |
Correct |
18 ms |
164188 KB |
Output is correct |
20 |
Correct |
19 ms |
164188 KB |
Output is correct |
21 |
Correct |
21 ms |
164188 KB |
Output is correct |
22 |
Correct |
19 ms |
164184 KB |
Output is correct |
23 |
Correct |
19 ms |
164184 KB |
Output is correct |
24 |
Correct |
20 ms |
164188 KB |
Output is correct |
25 |
Correct |
20 ms |
164304 KB |
Output is correct |
26 |
Correct |
23 ms |
164364 KB |
Output is correct |
27 |
Correct |
41 ms |
165312 KB |
Output is correct |
28 |
Correct |
52 ms |
166612 KB |
Output is correct |
29 |
Correct |
55 ms |
166736 KB |
Output is correct |
30 |
Correct |
50 ms |
166224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
165460 KB |
Output is correct |
2 |
Correct |
19 ms |
164148 KB |
Output is correct |
3 |
Correct |
18 ms |
164188 KB |
Output is correct |
4 |
Correct |
19 ms |
164184 KB |
Output is correct |
5 |
Correct |
19 ms |
164168 KB |
Output is correct |
6 |
Correct |
19 ms |
164184 KB |
Output is correct |
7 |
Correct |
19 ms |
164188 KB |
Output is correct |
8 |
Correct |
43 ms |
165188 KB |
Output is correct |
9 |
Correct |
44 ms |
165240 KB |
Output is correct |
10 |
Correct |
26 ms |
164444 KB |
Output is correct |
11 |
Correct |
44 ms |
165952 KB |
Output is correct |
12 |
Correct |
20 ms |
164188 KB |
Output is correct |
13 |
Correct |
19 ms |
164264 KB |
Output is correct |
14 |
Correct |
19 ms |
164188 KB |
Output is correct |
15 |
Correct |
18 ms |
164188 KB |
Output is correct |
16 |
Correct |
21 ms |
164180 KB |
Output is correct |
17 |
Correct |
22 ms |
164184 KB |
Output is correct |
18 |
Correct |
23 ms |
164436 KB |
Output is correct |
19 |
Correct |
41 ms |
165212 KB |
Output is correct |
20 |
Correct |
52 ms |
166680 KB |
Output is correct |
21 |
Correct |
48 ms |
166224 KB |
Output is correct |
22 |
Correct |
48 ms |
165712 KB |
Output is correct |
23 |
Correct |
51 ms |
166612 KB |
Output is correct |
24 |
Correct |
39 ms |
165716 KB |
Output is correct |
25 |
Correct |
57 ms |
165568 KB |
Output is correct |
26 |
Correct |
59 ms |
165496 KB |
Output is correct |
27 |
Correct |
18 ms |
164188 KB |
Output is correct |
28 |
Correct |
19 ms |
164188 KB |
Output is correct |
29 |
Correct |
19 ms |
164084 KB |
Output is correct |
30 |
Correct |
19 ms |
164188 KB |
Output is correct |
31 |
Correct |
19 ms |
164184 KB |
Output is correct |
32 |
Correct |
22 ms |
164276 KB |
Output is correct |
33 |
Correct |
19 ms |
164184 KB |
Output is correct |
34 |
Correct |
19 ms |
164188 KB |
Output is correct |
35 |
Correct |
21 ms |
164236 KB |
Output is correct |
36 |
Correct |
45 ms |
165068 KB |
Output is correct |
37 |
Correct |
44 ms |
165968 KB |
Output is correct |
38 |
Correct |
53 ms |
166484 KB |
Output is correct |
39 |
Correct |
54 ms |
166736 KB |
Output is correct |
40 |
Correct |
52 ms |
166224 KB |
Output is correct |
41 |
Correct |
52 ms |
165460 KB |
Output is correct |
42 |
Correct |
46 ms |
165520 KB |
Output is correct |
43 |
Correct |
55 ms |
165972 KB |
Output is correct |
44 |
Correct |
18 ms |
164188 KB |
Output is correct |
45 |
Correct |
18 ms |
164188 KB |
Output is correct |
46 |
Correct |
19 ms |
164188 KB |
Output is correct |
47 |
Correct |
21 ms |
164188 KB |
Output is correct |
48 |
Correct |
19 ms |
164184 KB |
Output is correct |
49 |
Correct |
19 ms |
164184 KB |
Output is correct |
50 |
Correct |
20 ms |
164188 KB |
Output is correct |
51 |
Correct |
20 ms |
164304 KB |
Output is correct |
52 |
Correct |
23 ms |
164364 KB |
Output is correct |
53 |
Correct |
41 ms |
165312 KB |
Output is correct |
54 |
Correct |
52 ms |
166612 KB |
Output is correct |
55 |
Correct |
55 ms |
166736 KB |
Output is correct |
56 |
Correct |
50 ms |
166224 KB |
Output is correct |
57 |
Correct |
66 ms |
167764 KB |
Output is correct |
58 |
Correct |
39 ms |
165212 KB |
Output is correct |
59 |
Correct |
50 ms |
166188 KB |
Output is correct |