#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 / l >= 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 |
92 ms |
17668 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
99 ms |
14336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
17668 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |