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 <iostream>
#include <vector>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
typedef int64_t ll;
int read() {
int n = 0; bool b = 0; char c = getchar();
for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-');
for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0');
if (b) return -n;
return n;
}
int k;
struct mat {
vec<vec<int>> t = vec<vec<int>>(k, vec<int>(k, 1e9));
mat operator +(const mat& other) const {
mat res;
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
for (int x = 0; x < k; ++x) {
res.t[i][j] = min(res.t[i][j], t[i][x] + other.t[x][j]);
}
}
}
return res;
}
};
int n, m, o, base;
mat zero;
vec<mat> st;
void init() {
for (int i = base-1; i > 0; --i) {
st[i] = st[2*i] + st[2*i+1];
}
}
mat get(int l, int r) {
mat sum = zero;
for (l += base, r += base; l < r; l /= 2, r /= 2) {
if (l & 1) sum = st[l++] + sum;
if (r & 1) sum = sum + st[--r];
}
return sum;
}
int main() {
df("debug mode\n");
#ifndef DEBUG
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
cin >> k >> n >> m >> o;
n /= k;
zero = mat();
foru (i, k) zero.t[i][i] = 0;
base = 1;
while (base <= n) base *= 2;
st.resize(2*base);
foru (i, m) {
int a, b, t;
cin >> a >> b >> t;
st[base + a/k].t[a%k][b%k] = t;
}
init();
foru (i, o) {
int a, b;
cin >> a >> b;
int res = get(a/k, b/k).t[a%k][b%k];
if (res == 1e9) cout << "-1\n";
else cout << res << "\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... |