#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
struct min_matrix{
int ysize;
int xsize;
vector<vector<int>> data;
min_matrix(){}
min_matrix(int x, int y, int init) : ysize(y), xsize(x), data(ysize){
for (auto &i : data)
i = vector<int>(xsize, init);
}
min_matrix operator*(const min_matrix& other) const{
min_matrix result(other.xsize, ysize, inf);
for (int from = 0; from < xsize; from++)
{
for (int to = 0; to < xsize; to++)
{
for (int mid = 0; mid < xsize; mid++)
{
result.data[to][from] = min(result.data[to][from], data[mid][from] + other.data[to][mid]);
}
}
}
return result;
}
};
template<class T>
struct sparse_table
{
vector<vector<T>> data;
function<T(const T&, const T&)> op;
sparse_table(){}
sparse_table(function<T(const T&, const T&)> op, int sz, T* arr) : op(op){
data.emplace_back(vector(arr, arr+sz));
int block_size = 2;
int j = 1;
while (block_size < sz){
data.emplace_back(sz);
for (int begin = 0; begin < sz; begin+=2*block_size)
{
int center = min(sz-1, begin+block_size-1);
data[j][center] = arr[center];
for (int i = center - 1; i >= begin; i--)
{
data[j][i] = op(arr[i], data[j][i+1]);
}
if (center != sz - 1)
data[j][center+1] = arr[center+1];
for (int i = center+2; i < min(center+block_size+1, sz); i++)
{
data[j][i] = op(data[j][i-1], arr[i]);
}
}
block_size *= 2;
j++;
}
}
min_matrix query(int l, int r){
if (l == r)
return data[0][l];
int layer = -1;
int t = l ^ r;
while (t){
layer++;
t >>= 1;
}
return op(data[layer][l], data[layer][r]);
}
};
void solve(){
int k, n, m, q;
cin >> k >> n >> m >> q;
vector<min_matrix> data_for_table(n/k, min_matrix(k, k, inf));
for (int i = 0; i < m; i++)
{
int a, b, cost;
cin >> a >> b >> cost;
data_for_table[a/k].data[b%k][a%k] = cost;
}
sparse_table<min_matrix> table([](const min_matrix& l, const min_matrix& r){return l*r;}, data_for_table.size(), data_for_table.data());
for (int i = 0; i < q; i++)
{
int a, b;
cin >> a >> b;
if (b/k <= a/k){
cout << "-1\n";
continue;
}
int result = table.query(a/k, b/k-1).data[b%k][a%k];
if (result == inf){
result = -1;
}
cout << result << '\n';
}
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
int t = 1;
for (int i = 0; i < t; i++)
{
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
81232 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 |
2 ms |
1488 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
103 ms |
81028 KB |
Output is correct |
9 |
Correct |
115 ms |
80976 KB |
Output is correct |
10 |
Correct |
83 ms |
80208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
64588 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
4 ms |
1372 KB |
Output is correct |
8 |
Correct |
4 ms |
1372 KB |
Output is correct |
9 |
Correct |
93 ms |
81036 KB |
Output is correct |
10 |
Correct |
86 ms |
56912 KB |
Output is correct |
11 |
Correct |
84 ms |
64832 KB |
Output is correct |
12 |
Correct |
75 ms |
55888 KB |
Output is correct |
13 |
Correct |
64 ms |
29012 KB |
Output is correct |
14 |
Correct |
53 ms |
32340 KB |
Output is correct |
15 |
Correct |
54 ms |
27924 KB |
Output is correct |
16 |
Correct |
51 ms |
27728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 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 |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1112 KB |
Output is correct |
8 |
Correct |
3 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
1088 KB |
Output is correct |
10 |
Correct |
88 ms |
80960 KB |
Output is correct |
11 |
Correct |
113 ms |
64592 KB |
Output is correct |
12 |
Correct |
86 ms |
56920 KB |
Output is correct |
13 |
Correct |
101 ms |
57172 KB |
Output is correct |
14 |
Correct |
85 ms |
56512 KB |
Output is correct |
15 |
Correct |
67 ms |
27928 KB |
Output is correct |
16 |
Correct |
53 ms |
27848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 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 |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1112 KB |
Output is correct |
8 |
Correct |
3 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
1088 KB |
Output is correct |
10 |
Correct |
88 ms |
80960 KB |
Output is correct |
11 |
Correct |
113 ms |
64592 KB |
Output is correct |
12 |
Correct |
86 ms |
56920 KB |
Output is correct |
13 |
Correct |
101 ms |
57172 KB |
Output is correct |
14 |
Correct |
85 ms |
56512 KB |
Output is correct |
15 |
Correct |
67 ms |
27928 KB |
Output is correct |
16 |
Correct |
53 ms |
27848 KB |
Output is correct |
17 |
Correct |
90 ms |
64516 KB |
Output is correct |
18 |
Correct |
0 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
3 ms |
1368 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
100 ms |
81004 KB |
Output is correct |
28 |
Correct |
93 ms |
56916 KB |
Output is correct |
29 |
Correct |
107 ms |
57216 KB |
Output is correct |
30 |
Correct |
99 ms |
56392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
81232 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 |
2 ms |
1488 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
103 ms |
81028 KB |
Output is correct |
9 |
Correct |
115 ms |
80976 KB |
Output is correct |
10 |
Correct |
83 ms |
80208 KB |
Output is correct |
11 |
Correct |
85 ms |
64588 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
452 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
1372 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
19 |
Correct |
93 ms |
81036 KB |
Output is correct |
20 |
Correct |
86 ms |
56912 KB |
Output is correct |
21 |
Correct |
84 ms |
64832 KB |
Output is correct |
22 |
Correct |
75 ms |
55888 KB |
Output is correct |
23 |
Correct |
64 ms |
29012 KB |
Output is correct |
24 |
Correct |
53 ms |
32340 KB |
Output is correct |
25 |
Correct |
54 ms |
27924 KB |
Output is correct |
26 |
Correct |
51 ms |
27728 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
456 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 |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1112 KB |
Output is correct |
34 |
Correct |
3 ms |
980 KB |
Output is correct |
35 |
Correct |
2 ms |
1088 KB |
Output is correct |
36 |
Correct |
88 ms |
80960 KB |
Output is correct |
37 |
Correct |
113 ms |
64592 KB |
Output is correct |
38 |
Correct |
86 ms |
56920 KB |
Output is correct |
39 |
Correct |
101 ms |
57172 KB |
Output is correct |
40 |
Correct |
85 ms |
56512 KB |
Output is correct |
41 |
Correct |
67 ms |
27928 KB |
Output is correct |
42 |
Correct |
53 ms |
27848 KB |
Output is correct |
43 |
Correct |
90 ms |
64516 KB |
Output is correct |
44 |
Correct |
0 ms |
452 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
3 ms |
1368 KB |
Output is correct |
50 |
Correct |
2 ms |
1116 KB |
Output is correct |
51 |
Correct |
3 ms |
1116 KB |
Output is correct |
52 |
Correct |
3 ms |
1116 KB |
Output is correct |
53 |
Correct |
100 ms |
81004 KB |
Output is correct |
54 |
Correct |
93 ms |
56916 KB |
Output is correct |
55 |
Correct |
107 ms |
57216 KB |
Output is correct |
56 |
Correct |
99 ms |
56392 KB |
Output is correct |
57 |
Correct |
110 ms |
53688 KB |
Output is correct |
58 |
Correct |
104 ms |
81228 KB |
Output is correct |
59 |
Correct |
96 ms |
64860 KB |
Output is correct |