Submission #933433

#TimeUsernameProblemLanguageResultExecution timeMemory
933433LucaIlieToll (BOI17_toll)C++17
100 / 100
215 ms18544 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...