This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 5e4+5, maxk = 5;
int k, n, m, o;
vector <vector <int> > seg[maxn*4];
vector <pii> adj[maxn];
vector <vector <int> > merge(vector <vector <int> > l, vector <vector <int> > r) {
vector <vector <int> > res;
vector <int> v;
v.resize(k, inf);
for (int i = 0; i < k; i++) res.pb(v);
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
for (int u = 0; u < k; u++) smin(res[i][j], l[i][u]+r[u][j]);
}
}
return res;
}
void build(int l = 0, int r = (n-1)/k+1, int id = 1) {
if (l+1 == r) {
vector <int> v;
v.resize(k, inf);
for (int i = 0; i < k; i++) seg[id].pb(v);
for (int i = 0; i < k; i++) {
for (pii j : adj[l*k+i]) seg[id][i][j.F] = j.S;
}
return;
}
int mid = (l+r)/2;
build(l, mid, id*2), build(mid, r, id*2+1);
seg[id] = merge(seg[id*2], seg[id*2+1]);
}
vector <vector <int> > get(int s, int e, int l = 0, int r = (n-1)/k+1, int id = 1) {
if (s <= l and r <= e) {
for (int i = 0; i < k; i++) return seg[id];
}
int mid = (l+r)/2;
if (e <= mid) return get(s, e, l, mid, id*2);
if (s >= mid) return get(s, e, mid, r, id*2+1);
return merge(get(s, e, l, mid, id*2), get(s, e, mid, r, id*2+1));
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> k >> n >> m >> o;
while (m--) {
int a, b, c; cin >> a >> b >> c; adj[a].pb(mp(b%k, c));
}
build();
while (o--) {
int a, b; cin >> a >> b;
if (a/k == b/k) cout << -1 << '\n';
else {
int ans = get(a/k, b/k)[a%k][b%k];
cout << (ans == inf ? -1 : ans) << '\n';
}
}
return 0;
}
# | 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... |