This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e17;
int k;
struct node{
bool dummy;
vector<vector<ll>> A, B;
node(){
dummy = 1;
A.assign(k, vector<ll>(k, 0));
B = vector<vector<ll>>(k, vector<ll>(k, inf));
for(int i = 0; i < k; i++) B[i][i] = 0;
}
node(vector<vector<ll>> _A){
dummy = 0;
A = _A;
B.assign(k, vector<ll>(k, inf));
for(int i = 0; i < k; i++) B[i][i] = 0;
}
};
node operator+(node a, node b){
if(a.dummy) return b;
if(b.dummy) return a;
node rt;
rt.dummy = 0;
rt.A = a.A, rt.B = vector<vector<ll>>(k, vector<ll>(k, inf));
for(int i = 0; i < k; i++) for(int j = 0; j < k; j++)
for(int l = 0; l < k; l++) for(int m = 0; m < k; m++)
rt.B[i][j] = min(rt.B[i][j], a.B[i][l]+b.B[m][j]+b.A[l][m]);
return rt;
}
struct segtree{
int k;
vector<node> v;
segtree(int n){
k = 1;
while(k < n) k*=2;
k*=2;
v.resize(k, node());
}
void upd(int in, vector<vector<ll>> x){
in+=k/2;
v[in] = node(x);
in/=2;
while(in > 0){
v[in] = v[2*in]+v[2*in+1];
in/=2;
}
}
node get(int l, int r, int nd, int a, int b){
if(b < l || a > r) return node();
if(a >= l && b <= r) return v[nd];
int md = (a+b)/2;
return get(l, r, 2*nd, a, md) + get(l, r, 2*nd+1, md+1, b);
}
node get(int l, int r){
return get(l, r, 1, 0, k/2-1);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, o; cin >> k >> n >> m >> o;
vector<vector<vector<ll>>> div((n+k-1)/k, vector<vector<ll>>(k, vector<ll>(k, inf)));
segtree seg((n+k-1)/k);
for(int i = 0; i < m; i++){
int a, b; ll w; cin >> a >> b >> w;
div[b/k][a%k][b%k] = w;
}
for(int i = 0; i < div.size(); i++) seg.upd(i, div[i]);
while(o--){
int a, b; cin >> a >> b;
auto nd = seg.get(a/k, b/k);
ll ans = nd.B[a%k][b%k];
if(ans >= inf) ans = -1;
cout << ans << "\n";
}
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::vector<long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < div.size(); i++) seg.upd(i, div[i]);
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |