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 ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define int long long
int k, n, m, q;
const int N = 5e4 + 5;
unordered_map<int, int> val[N];
struct node {
int c[5][5];
node() {
for(int i = 0; i < 5; ++i) {
for(int j = 0; j < 5; ++j) {
c[i][j] = 1e12;
}
}
}
} t[4 * N];
node merge(node a, node b, int bl) {
node c;
for(int starti = 0; starti < k; ++starti) {
for(int endi = 0; endi < k; ++endi) {
for(int startj = 0; startj < k; ++startj) {
for(int endj = 0; endj < k; ++endj) {
int cost = a.c[starti][endi] + b.c[startj][endj];
if(val[bl * k + endi].find((bl + 1) * k + startj) != val[bl * k + endi].end()) {
cost += val[bl * k + endi][((bl + 1) * k + startj)];
c.c[starti][endj] = min(c.c[starti][endj], cost);
}
}
}
}
}
return c;
}
void build(int i, int l, int r) {
if(l > r) return;
if(l == r) {
for(int j = 0; j < 5; ++j) {
t[i].c[j][j] = 0;
}
return;
}
int mid = l + r >> 1;
//mid -> finalul la [l, mid];
//mid + 1 -> inceputul la [mid + 1, r];
build(2 * i, l, mid);
build(2 * i + 1, mid + 1, r);
t[i] = merge(t[2 * i], t[2 * i + 1], mid);
for(int j = 0; j < k; ++j) {
for(int f = 0; f < k; ++f) {
//cout << t[i].c[j][f] << " ";
}
// cout << "\n";
}
}
node neutral;
node query(int i, int l, int r, int tl, int tr) {
if(l > tr || r < tl) return neutral;
if(l >= tl && r <= tr) return t[i];
int mid = l + r >> 1;
if(mid < tl) {
//[l, mid]
//[tl, tr]
return query(2 * i + 1, mid + 1, r, tl, tr);
} else if(mid + 1 > tr){
//[mid + 1, r]
//[tl, tr]
return query(2 * i, l, mid, tl, tr);
}
node left = query(2 * i, l, mid, tl, tr);
node right = query(2 * i + 1, mid + 1, r, tl, tr);
return merge(left, right, mid);
}
void solve() {
for(int i = 0; i < 5; ++i) {
for(int j = 0; j < 5; ++j) {
neutral.c[i][j] = 0;
}
}
cin >> k >> n >> m >> q;
for(int i = 0; i < m; ++i) {
int a, b, t; cin >> a >> b >> t;
if(val[a].find(b) == val[a].end()) {
val[a][b] = t;
} else {
val[a][b] = min(val[a][b], t);
}
}
build(1, 0, n / k);
while(q--) {
int a, b; cin >> a >> b;
node f = query(1, 0, n / k, a / k, b / k);
for(int i = 0; i < k; ++i) {
for(int j = 0; j < k; ++j) {
//cout << f.c[i][j] << "\n";
}
}
if(f.c[a % k][b % k] == (ll)1e12) cout << "-1\n";
else cout << f.c[a % k][b % k] << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
Compilation message (stderr)
toll.cpp: In function 'void build(long long int, long long int, long long int)':
toll.cpp:53:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = l + r >> 1;
| ~~^~~
toll.cpp: In function 'node query(long long int, long long int, long long int, long long int, long long int)':
toll.cpp:70:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int mid = l + r >> 1;
| ~~^~~
# | 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... |