#include <bits/stdc++.h>
using namespace std;
const int MAX_K = 5;
const int MAX_N = 5e4;
const int INF = 1e9;
int k;
struct matrix {
int n;
vector<vector<int>> mat;
void init( int _n ) {
n = _n;
mat.resize( n );
for ( int i = 0; i < n; i++ ) {
mat[i].resize( n );
for ( int j = 0; j < n; j++ )
mat[i][j] = INF;
}
}
matrix operator + ( matrix b ) {
matrix a = *this;
matrix c;
c.init( n );
for ( int i = 0; i < n; i++ ) {
for ( int j = 0; j < n; j++ ) {
for ( int k = 0; k < n; k++ )
c.mat[i][j] = min( c.mat[i][j], a.mat[i][k] + b.mat[k][j] );
}
}
return c;
}
};
matrix buckets[MAX_N];
struct SEG_TREE {
int n;
vector<matrix> segTree;
void init( int v, int l, int r ) {
if ( l == r ) {
segTree[v] = buckets[l];
return;
}
int mid = (l + r) / 2;
init( v * 2 + 1, l, mid );
init( v * 2 + 2, mid + 1, r );
segTree[v] = segTree[v * 2 + 1] + segTree[v * 2 + 2];
}
void init( int _n ) {
n = _n;
segTree.resize( 4 * n );
init( 0, 0, n - 1 );
}
matrix query( int v, int l, int r, int lq, int rq ) {
if ( lq <= l && r <= rq )
return segTree[v];
int mid = (l + r) / 2;
if ( mid < lq )
return query( v * 2 + 2, mid + 1, r, lq, rq );
if ( mid + 1 > rq )
return query( v * 2 + 1, l, mid, lq, rq );
return query( v * 2 + 1, l, mid, lq, rq ) + query( v * 2 + 2, mid + 1, r, lq, rq );
}
matrix query( int l, int r ) {
return query( 0, 0, n - 1, l, r );
}
} toll;
int main() {
int k, n, m, o;
cin >> k >> n >> m >> o;
int b = 0;
for ( int i = 0; i < n; i += k ) {
buckets[i / k].init( k );
b++;
}
for ( int i = 0; i < m; i++ ) {
int a, b, c;
cin >> a >> b >> c;
buckets[a / k].mat[a % k][b % k] = c;
}
toll.init( b );
while ( o-- ) {
int l, r;
cin >> l >> r;
int ans = (l / k >= r / k ? INF : toll.query( l / k, r / k - 1 ).mat[l % k][r % k]);
cout << (ans == INF ? -1 : ans) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
17744 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
5 ms |
2140 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
4 ms |
2140 KB |
Output is correct |
8 |
Correct |
90 ms |
17676 KB |
Output is correct |
9 |
Correct |
97 ms |
17544 KB |
Output is correct |
10 |
Correct |
63 ms |
17500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
14420 KB |
Output is correct |
2 |
Correct |
2 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
23 ms |
2412 KB |
Output is correct |
8 |
Correct |
26 ms |
2636 KB |
Output is correct |
9 |
Correct |
82 ms |
18544 KB |
Output is correct |
10 |
Correct |
137 ms |
14848 KB |
Output is correct |
11 |
Correct |
146 ms |
16080 KB |
Output is correct |
12 |
Correct |
101 ms |
13932 KB |
Output is correct |
13 |
Correct |
126 ms |
10440 KB |
Output is correct |
14 |
Correct |
78 ms |
9864 KB |
Output is correct |
15 |
Correct |
78 ms |
9048 KB |
Output is correct |
16 |
Correct |
70 ms |
9044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
2 ms |
2140 KB |
Output is correct |
7 |
Correct |
2 ms |
2140 KB |
Output is correct |
8 |
Correct |
4 ms |
2140 KB |
Output is correct |
9 |
Correct |
3 ms |
2392 KB |
Output is correct |
10 |
Correct |
52 ms |
17492 KB |
Output is correct |
11 |
Correct |
85 ms |
14320 KB |
Output is correct |
12 |
Correct |
104 ms |
12520 KB |
Output is correct |
13 |
Correct |
111 ms |
12540 KB |
Output is correct |
14 |
Correct |
86 ms |
12524 KB |
Output is correct |
15 |
Correct |
58 ms |
7760 KB |
Output is correct |
16 |
Correct |
59 ms |
7764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
2 ms |
2140 KB |
Output is correct |
7 |
Correct |
2 ms |
2140 KB |
Output is correct |
8 |
Correct |
4 ms |
2140 KB |
Output is correct |
9 |
Correct |
3 ms |
2392 KB |
Output is correct |
10 |
Correct |
52 ms |
17492 KB |
Output is correct |
11 |
Correct |
85 ms |
14320 KB |
Output is correct |
12 |
Correct |
104 ms |
12520 KB |
Output is correct |
13 |
Correct |
111 ms |
12540 KB |
Output is correct |
14 |
Correct |
86 ms |
12524 KB |
Output is correct |
15 |
Correct |
58 ms |
7760 KB |
Output is correct |
16 |
Correct |
59 ms |
7764 KB |
Output is correct |
17 |
Correct |
98 ms |
14592 KB |
Output is correct |
18 |
Correct |
1 ms |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
1880 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
10 ms |
2276 KB |
Output is correct |
24 |
Correct |
11 ms |
2140 KB |
Output is correct |
25 |
Correct |
16 ms |
2140 KB |
Output is correct |
26 |
Correct |
14 ms |
2596 KB |
Output is correct |
27 |
Correct |
71 ms |
18280 KB |
Output is correct |
28 |
Correct |
125 ms |
14816 KB |
Output is correct |
29 |
Correct |
161 ms |
14928 KB |
Output is correct |
30 |
Correct |
102 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
17744 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
5 ms |
2140 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
4 ms |
2140 KB |
Output is correct |
8 |
Correct |
90 ms |
17676 KB |
Output is correct |
9 |
Correct |
97 ms |
17544 KB |
Output is correct |
10 |
Correct |
63 ms |
17500 KB |
Output is correct |
11 |
Correct |
117 ms |
14420 KB |
Output is correct |
12 |
Correct |
2 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
23 ms |
2412 KB |
Output is correct |
18 |
Correct |
26 ms |
2636 KB |
Output is correct |
19 |
Correct |
82 ms |
18544 KB |
Output is correct |
20 |
Correct |
137 ms |
14848 KB |
Output is correct |
21 |
Correct |
146 ms |
16080 KB |
Output is correct |
22 |
Correct |
101 ms |
13932 KB |
Output is correct |
23 |
Correct |
126 ms |
10440 KB |
Output is correct |
24 |
Correct |
78 ms |
9864 KB |
Output is correct |
25 |
Correct |
78 ms |
9048 KB |
Output is correct |
26 |
Correct |
70 ms |
9044 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
1 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1880 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
2 ms |
2140 KB |
Output is correct |
34 |
Correct |
4 ms |
2140 KB |
Output is correct |
35 |
Correct |
3 ms |
2392 KB |
Output is correct |
36 |
Correct |
52 ms |
17492 KB |
Output is correct |
37 |
Correct |
85 ms |
14320 KB |
Output is correct |
38 |
Correct |
104 ms |
12520 KB |
Output is correct |
39 |
Correct |
111 ms |
12540 KB |
Output is correct |
40 |
Correct |
86 ms |
12524 KB |
Output is correct |
41 |
Correct |
58 ms |
7760 KB |
Output is correct |
42 |
Correct |
59 ms |
7764 KB |
Output is correct |
43 |
Correct |
98 ms |
14592 KB |
Output is correct |
44 |
Correct |
1 ms |
1880 KB |
Output is correct |
45 |
Correct |
1 ms |
1880 KB |
Output is correct |
46 |
Correct |
1 ms |
1884 KB |
Output is correct |
47 |
Correct |
1 ms |
1884 KB |
Output is correct |
48 |
Correct |
1 ms |
1884 KB |
Output is correct |
49 |
Correct |
10 ms |
2276 KB |
Output is correct |
50 |
Correct |
11 ms |
2140 KB |
Output is correct |
51 |
Correct |
16 ms |
2140 KB |
Output is correct |
52 |
Correct |
14 ms |
2596 KB |
Output is correct |
53 |
Correct |
71 ms |
18280 KB |
Output is correct |
54 |
Correct |
125 ms |
14816 KB |
Output is correct |
55 |
Correct |
161 ms |
14928 KB |
Output is correct |
56 |
Correct |
102 ms |
14416 KB |
Output is correct |
57 |
Correct |
215 ms |
15696 KB |
Output is correct |
58 |
Correct |
95 ms |
18512 KB |
Output is correct |
59 |
Correct |
123 ms |
16372 KB |
Output is correct |