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>
using namespace std;
#define f first
#define s second
// #define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 2e5 + 5, inf = 1e9;
int t,n,k,a[N], m, q, x;
struct nod{
int x[5][5];
};
nod zer_vii, inf_vii, tree[N];
nod merge(nod a, nod b) {
nod res;
res = inf_vii;
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
for (int m = 0; m < k; m++) {
res.x[i][j] = min(res.x[i][j], a.x[i][m] + b.x[m][j]);
}
}
}
return res;
}
void build(int node, int le, int ri) {
tree[node] = inf_vii;
if (le == ri) {
return ;
}
int mid = (le + ri) / 2;
build(2 * node, le, mid);
build(2 * node + 1, mid + 1, ri);
}
void update(int node, int le, int ri, int idxa, int rema, int idxb, int remb, int val) {
if (le > idxa || ri < idxa) return ;
if (le == ri) {
tree[node].x[rema][remb] = min(tree[node].x[rema][remb], val);
return ;
}
int mid = (le + ri) / 2;
update(2 * node, le, mid, idxa, rema, idxb, remb, val);
update(2 * node + 1, mid + 1, ri, idxa, rema, idxb, remb, val);
tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
}
nod getans(int node, int le, int ri, int start, int end) {
if (le > end || ri < start) return zer_vii;
if (le >= start && ri <= end) {
return tree[node];
}
int mid = (le + ri) / 2;
nod p1, p2, res;
p1 = getans(2 * node, le, mid, start, end); p2 = getans(2 * node + 1, mid + 1, ri, start, end);
res = merge(p1, p2);
return res;
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>k>>n>>m>>q;
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
zer_vii.x[i][j] = inf; inf_vii.x[i][j] = inf;
}
}
for (int i = 0; i < k; i++) zer_vii.x[i][i] = 0;
build(1, 0, (n - 1) / k);
for (int i = 1; i <= m; i++) {
int a, b, x;
cin>>a>>b>>x;
update(1, 0, (n - 1) / k, a / k, a % k, b / k, b % k, x);
}
while (q--) {
int a, b;
cin>>a>>b;
nod ans;
ans = getans(1, 0, (n - 1) / k, a / k, b / k - 1);
cout<<(ans.x[a % k][b % k] < inf ? ans.x[a % k][b % k] : -1)<<"\n";
}
}
Compilation message (stderr)
toll.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
57 | main() {
| ^~~~
# | 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... |