#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct adjmat {
ll adj[5][5];
adjmat() {
for (int i = 0 ; i < 5; i++){
for (int j = 0; j < 5; j++) {
adj[i][j] = 1e12;
}
}
}
};
adjmat combine(adjmat a, adjmat b) {
adjmat cur;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (a.adj[i][j] != 0) {
for (int k = 0; k < 5; k++) {
cur.adj[i][k] = min(cur.adj[i][k], a.adj[i][j] + b.adj[j][k]);
}
}
}
}
return cur;
}
struct node {
int l, r;
adjmat adj;
node *lc, *rc;
node(int _l, int _r) {
l = _l, r = _r;
if (l == r) return;
int mid = (l + r) / 2;
lc = new node(l, mid);
rc = new node(mid + 1, r);
}
void upd(int group, int ql, int qr, ll cost) {
if (l == r) {
adj.adj[ql][qr] = min(adj.adj[ql][qr], cost);
return;
}
int mid = (l + r) / 2;
if (group <= mid) lc->upd(group, ql, qr, cost);
else rc->upd(group, ql, qr, cost);
adj = combine(lc->adj, rc->adj);
}
adjmat query(int ba, int a, int bb, int b) {
if (ba <= l && r <= bb) return adj;
int mid = (l + r) / 2;
if (bb <= mid) return lc->query(ba, a, bb, b);
if (ba > mid) return rc->query(ba, a, bb, b);
return combine(lc->query(ba, a, bb, b), rc->query(ba, a, bb, b));
}
};
int main() {
int k, n, m, o; cin >> k >> n >> m >> o;
node *root = new node(0, n);
for (int i = 0; i < m; i++) {
int a, b, t; cin >> a >> b >> t;
int ga = a / k, gb = b / k;
root->upd(ga, a % k, b % k, t);
}
for (int i = 0; i < o; i++) {
int a, b; cin >> a >> b;
if (a / k >= b/ k) {
cout << -1 << "\n"; continue;
}
assert(a/k <= b/k-1);
adjmat bruh = root->query(a / k, a % k, b / k - 1, b % k);
cout <<( bruh.adj[a % k][b %k] >= 1e10 ? -1 : bruh.adj[a % k][b % k]) << "\n";
}
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:83:25: warning: unused variable 'gb' [-Wunused-variable]
83 | int ga = a / k, gb = b / k;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
331 ms |
24768 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
768 KB |
Output is correct |
8 |
Correct |
302 ms |
24644 KB |
Output is correct |
9 |
Correct |
280 ms |
24652 KB |
Output is correct |
10 |
Correct |
66 ms |
23952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
546 ms |
25408 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
34 ms |
852 KB |
Output is correct |
8 |
Correct |
33 ms |
852 KB |
Output is correct |
9 |
Correct |
257 ms |
24640 KB |
Output is correct |
10 |
Correct |
628 ms |
26052 KB |
Output is correct |
11 |
Correct |
474 ms |
25536 KB |
Output is correct |
12 |
Correct |
362 ms |
25088 KB |
Output is correct |
13 |
Correct |
588 ms |
16788 KB |
Output is correct |
14 |
Correct |
381 ms |
15760 KB |
Output is correct |
15 |
Correct |
285 ms |
15508 KB |
Output is correct |
16 |
Correct |
310 ms |
15508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
696 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
13 ms |
820 KB |
Output is correct |
9 |
Correct |
10 ms |
724 KB |
Output is correct |
10 |
Correct |
236 ms |
24512 KB |
Output is correct |
11 |
Correct |
451 ms |
25284 KB |
Output is correct |
12 |
Correct |
566 ms |
25932 KB |
Output is correct |
13 |
Correct |
645 ms |
26188 KB |
Output is correct |
14 |
Correct |
462 ms |
25536 KB |
Output is correct |
15 |
Correct |
304 ms |
15552 KB |
Output is correct |
16 |
Correct |
286 ms |
15436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
696 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
13 ms |
820 KB |
Output is correct |
9 |
Correct |
10 ms |
724 KB |
Output is correct |
10 |
Correct |
236 ms |
24512 KB |
Output is correct |
11 |
Correct |
451 ms |
25284 KB |
Output is correct |
12 |
Correct |
566 ms |
25932 KB |
Output is correct |
13 |
Correct |
645 ms |
26188 KB |
Output is correct |
14 |
Correct |
462 ms |
25536 KB |
Output is correct |
15 |
Correct |
304 ms |
15552 KB |
Output is correct |
16 |
Correct |
286 ms |
15436 KB |
Output is correct |
17 |
Correct |
456 ms |
25444 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
300 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
14 ms |
700 KB |
Output is correct |
24 |
Correct |
16 ms |
724 KB |
Output is correct |
25 |
Correct |
22 ms |
840 KB |
Output is correct |
26 |
Correct |
17 ms |
724 KB |
Output is correct |
27 |
Correct |
262 ms |
24612 KB |
Output is correct |
28 |
Correct |
583 ms |
25924 KB |
Output is correct |
29 |
Correct |
642 ms |
26176 KB |
Output is correct |
30 |
Correct |
492 ms |
25608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
331 ms |
24768 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
768 KB |
Output is correct |
8 |
Correct |
302 ms |
24644 KB |
Output is correct |
9 |
Correct |
280 ms |
24652 KB |
Output is correct |
10 |
Correct |
66 ms |
23952 KB |
Output is correct |
11 |
Correct |
546 ms |
25408 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
34 ms |
852 KB |
Output is correct |
18 |
Correct |
33 ms |
852 KB |
Output is correct |
19 |
Correct |
257 ms |
24640 KB |
Output is correct |
20 |
Correct |
628 ms |
26052 KB |
Output is correct |
21 |
Correct |
474 ms |
25536 KB |
Output is correct |
22 |
Correct |
362 ms |
25088 KB |
Output is correct |
23 |
Correct |
588 ms |
16788 KB |
Output is correct |
24 |
Correct |
381 ms |
15760 KB |
Output is correct |
25 |
Correct |
285 ms |
15508 KB |
Output is correct |
26 |
Correct |
310 ms |
15508 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
3 ms |
696 KB |
Output is correct |
33 |
Correct |
6 ms |
724 KB |
Output is correct |
34 |
Correct |
13 ms |
820 KB |
Output is correct |
35 |
Correct |
10 ms |
724 KB |
Output is correct |
36 |
Correct |
236 ms |
24512 KB |
Output is correct |
37 |
Correct |
451 ms |
25284 KB |
Output is correct |
38 |
Correct |
566 ms |
25932 KB |
Output is correct |
39 |
Correct |
645 ms |
26188 KB |
Output is correct |
40 |
Correct |
462 ms |
25536 KB |
Output is correct |
41 |
Correct |
304 ms |
15552 KB |
Output is correct |
42 |
Correct |
286 ms |
15436 KB |
Output is correct |
43 |
Correct |
456 ms |
25444 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
300 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
14 ms |
700 KB |
Output is correct |
50 |
Correct |
16 ms |
724 KB |
Output is correct |
51 |
Correct |
22 ms |
840 KB |
Output is correct |
52 |
Correct |
17 ms |
724 KB |
Output is correct |
53 |
Correct |
262 ms |
24612 KB |
Output is correct |
54 |
Correct |
583 ms |
25924 KB |
Output is correct |
55 |
Correct |
642 ms |
26176 KB |
Output is correct |
56 |
Correct |
492 ms |
25608 KB |
Output is correct |
57 |
Correct |
880 ms |
27072 KB |
Output is correct |
58 |
Correct |
361 ms |
24656 KB |
Output is correct |
59 |
Correct |
463 ms |
25536 KB |
Output is correct |