#include <bits/stdc++.h>
typedef long long ll;
const ll inf = 1e15;
const ll N = 50001;
const ll K = 5;
ll adj[N][K];
ll n, k;
struct Node {
std::vector<std::vector<ll>> arr;
ll tl = -1, tr = -1;
Node() {}
};
bool operator==(const Node &a, const Node &b) { return a.arr == b.arr; }
class SegmentTree {
public:
std::vector<Node> seg;
ll n;
SegmentTree(ll n) {
this->n = n;
seg.resize(4 * n);
}
Node idt() { return Node(); }
Node f(const Node &a, const Node &b) {
if (a == idt()) {
return b;
}
if (b == idt()) {
return a;
}
Node ret;
ret.tl = a.tl, ret.tr = b.tr;
auto &ans = ret.arr;
ans = std::vector<std::vector<ll>>(k, std::vector<ll>(k, inf));
for (ll s = 0; s < k; ++s) {
for (ll e = 0; e < k; ++e) {
for (ll s1 = 0; s1 < k; ++s1) {
for (ll e1 = 0; e1 < k; ++e1) {
if (k * a.tr + s1 > ::n) {
continue;
}
ans[s][e] =
std::min(ans[s][e],
a.arr[s][s1] + adj[k * a.tr + s1][e1] + b.arr[e1][e]);
}
}
}
}
return ret;
}
void construct(ll v, ll tl, ll tr) {
if (tl == tr) {
seg[v] = Node();
seg[v].tl = tl, seg[v].tr = tr;
seg[v].arr = std::vector<std::vector<ll>>(k, std::vector<ll>(k, inf));
for (ll i = 0; i < k; ++i) {
seg[v].arr[i][i] = 0;
}
} else {
ll tm = (tl + tr) / 2;
construct(2 * v, tl, tm);
construct(2 * v + 1, tm + 1, tr);
seg[v] = f(seg[2 * v], seg[2 * v + 1]);
}
}
void construct() { construct(1, 0, n); }
Node query(ll v, ll tl, ll tr, ll l, ll r) {
if (tl > tr || l > r || tr < l || r < tl) {
return idt();
}
if (l <= tl && tr <= r) {
return seg[v];
}
ll tm = (tl + tr) / 2;
return f(query(2 * v, tl, tm, l, r), query(2 * v + 1, tm + 1, tr, l, r));
}
Node query(ll l, ll r) { return query(1, 0, n, l, r); }
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll m, o;
std::cin >> k >> n >> m >> o;
for (ll i = 0; i < n; ++i) {
for (ll j = 0; j < k; ++j) {
adj[i][j] = inf;
}
}
for (ll i = 0, a, b, t; i < m; ++i) {
std::cin >> a >> b >> t;
adj[a][b - a - (k - (a % k))] = t;
}
SegmentTree tree(n);
tree.construct();
while (o--) {
ll a, b;
std::cin >> a >> b;
auto q = tree.query(a / k, b / k);
ll ans = q.arr[a % k][b % k];
std::cout << (ans == inf ? -1 : ans) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
16492 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
43 ms |
16496 KB |
Output is correct |
9 |
Correct |
41 ms |
16472 KB |
Output is correct |
10 |
Correct |
31 ms |
16472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
22604 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
860 KB |
Output is correct |
8 |
Correct |
9 ms |
856 KB |
Output is correct |
9 |
Correct |
27 ms |
17244 KB |
Output is correct |
10 |
Correct |
87 ms |
29776 KB |
Output is correct |
11 |
Correct |
44 ms |
24408 KB |
Output is correct |
12 |
Correct |
65 ms |
28748 KB |
Output is correct |
13 |
Correct |
88 ms |
30328 KB |
Output is correct |
14 |
Correct |
36 ms |
18000 KB |
Output is correct |
15 |
Correct |
81 ms |
29012 KB |
Output is correct |
16 |
Correct |
89 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
1372 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
22 ms |
16492 KB |
Output is correct |
11 |
Correct |
31 ms |
22624 KB |
Output is correct |
12 |
Correct |
48 ms |
29532 KB |
Output is correct |
13 |
Correct |
48 ms |
29788 KB |
Output is correct |
14 |
Correct |
40 ms |
29264 KB |
Output is correct |
15 |
Correct |
52 ms |
29084 KB |
Output is correct |
16 |
Correct |
51 ms |
29008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
1372 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
22 ms |
16492 KB |
Output is correct |
11 |
Correct |
31 ms |
22624 KB |
Output is correct |
12 |
Correct |
48 ms |
29532 KB |
Output is correct |
13 |
Correct |
48 ms |
29788 KB |
Output is correct |
14 |
Correct |
40 ms |
29264 KB |
Output is correct |
15 |
Correct |
52 ms |
29084 KB |
Output is correct |
16 |
Correct |
51 ms |
29008 KB |
Output is correct |
17 |
Correct |
38 ms |
24148 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
5 ms |
860 KB |
Output is correct |
25 |
Correct |
28 ms |
1480 KB |
Output is correct |
26 |
Correct |
15 ms |
1116 KB |
Output is correct |
27 |
Correct |
24 ms |
17116 KB |
Output is correct |
28 |
Correct |
58 ms |
29520 KB |
Output is correct |
29 |
Correct |
58 ms |
29780 KB |
Output is correct |
30 |
Correct |
54 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
16492 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
43 ms |
16496 KB |
Output is correct |
9 |
Correct |
41 ms |
16472 KB |
Output is correct |
10 |
Correct |
31 ms |
16472 KB |
Output is correct |
11 |
Correct |
57 ms |
22604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
6 ms |
860 KB |
Output is correct |
18 |
Correct |
9 ms |
856 KB |
Output is correct |
19 |
Correct |
27 ms |
17244 KB |
Output is correct |
20 |
Correct |
87 ms |
29776 KB |
Output is correct |
21 |
Correct |
44 ms |
24408 KB |
Output is correct |
22 |
Correct |
65 ms |
28748 KB |
Output is correct |
23 |
Correct |
88 ms |
30328 KB |
Output is correct |
24 |
Correct |
36 ms |
18000 KB |
Output is correct |
25 |
Correct |
81 ms |
29012 KB |
Output is correct |
26 |
Correct |
89 ms |
29264 KB |
Output is correct |
27 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
3 ms |
1372 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
22 ms |
16492 KB |
Output is correct |
37 |
Correct |
31 ms |
22624 KB |
Output is correct |
38 |
Correct |
48 ms |
29532 KB |
Output is correct |
39 |
Correct |
48 ms |
29788 KB |
Output is correct |
40 |
Correct |
40 ms |
29264 KB |
Output is correct |
41 |
Correct |
52 ms |
29084 KB |
Output is correct |
42 |
Correct |
51 ms |
29008 KB |
Output is correct |
43 |
Correct |
38 ms |
24148 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
3 ms |
604 KB |
Output is correct |
50 |
Correct |
5 ms |
860 KB |
Output is correct |
51 |
Correct |
28 ms |
1480 KB |
Output is correct |
52 |
Correct |
15 ms |
1116 KB |
Output is correct |
53 |
Correct |
24 ms |
17116 KB |
Output is correct |
54 |
Correct |
58 ms |
29520 KB |
Output is correct |
55 |
Correct |
58 ms |
29780 KB |
Output is correct |
56 |
Correct |
54 ms |
29264 KB |
Output is correct |
57 |
Correct |
179 ms |
43352 KB |
Output is correct |
58 |
Correct |
36 ms |
17496 KB |
Output is correct |
59 |
Correct |
60 ms |
24408 KB |
Output is correct |