#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
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;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
50200 KB |
Output is correct |
2 |
Correct |
17 ms |
42092 KB |
Output is correct |
3 |
Correct |
17 ms |
42208 KB |
Output is correct |
4 |
Correct |
17 ms |
42160 KB |
Output is correct |
5 |
Correct |
19 ms |
42324 KB |
Output is correct |
6 |
Correct |
23 ms |
42324 KB |
Output is correct |
7 |
Correct |
19 ms |
42324 KB |
Output is correct |
8 |
Correct |
76 ms |
50076 KB |
Output is correct |
9 |
Correct |
72 ms |
49768 KB |
Output is correct |
10 |
Correct |
35 ms |
42340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
52196 KB |
Output is correct |
2 |
Correct |
19 ms |
42316 KB |
Output is correct |
3 |
Correct |
17 ms |
42196 KB |
Output is correct |
4 |
Correct |
17 ms |
42184 KB |
Output is correct |
5 |
Correct |
17 ms |
42196 KB |
Output is correct |
6 |
Correct |
18 ms |
42160 KB |
Output is correct |
7 |
Correct |
23 ms |
42324 KB |
Output is correct |
8 |
Correct |
25 ms |
42472 KB |
Output is correct |
9 |
Correct |
57 ms |
50116 KB |
Output is correct |
10 |
Correct |
164 ms |
54128 KB |
Output is correct |
11 |
Correct |
95 ms |
52588 KB |
Output is correct |
12 |
Correct |
111 ms |
50636 KB |
Output is correct |
13 |
Correct |
161 ms |
52540 KB |
Output is correct |
14 |
Correct |
65 ms |
49736 KB |
Output is correct |
15 |
Correct |
134 ms |
48932 KB |
Output is correct |
16 |
Correct |
137 ms |
48928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
42196 KB |
Output is correct |
2 |
Correct |
17 ms |
42196 KB |
Output is correct |
3 |
Correct |
18 ms |
42196 KB |
Output is correct |
4 |
Correct |
18 ms |
42196 KB |
Output is correct |
5 |
Correct |
17 ms |
42208 KB |
Output is correct |
6 |
Correct |
18 ms |
42288 KB |
Output is correct |
7 |
Correct |
19 ms |
42368 KB |
Output is correct |
8 |
Correct |
21 ms |
42472 KB |
Output is correct |
9 |
Correct |
20 ms |
42456 KB |
Output is correct |
10 |
Correct |
61 ms |
50032 KB |
Output is correct |
11 |
Correct |
60 ms |
52160 KB |
Output is correct |
12 |
Correct |
85 ms |
54012 KB |
Output is correct |
13 |
Correct |
83 ms |
54744 KB |
Output is correct |
14 |
Correct |
70 ms |
52828 KB |
Output is correct |
15 |
Correct |
74 ms |
48876 KB |
Output is correct |
16 |
Correct |
72 ms |
48912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
42196 KB |
Output is correct |
2 |
Correct |
17 ms |
42196 KB |
Output is correct |
3 |
Correct |
18 ms |
42196 KB |
Output is correct |
4 |
Correct |
18 ms |
42196 KB |
Output is correct |
5 |
Correct |
17 ms |
42208 KB |
Output is correct |
6 |
Correct |
18 ms |
42288 KB |
Output is correct |
7 |
Correct |
19 ms |
42368 KB |
Output is correct |
8 |
Correct |
21 ms |
42472 KB |
Output is correct |
9 |
Correct |
20 ms |
42456 KB |
Output is correct |
10 |
Correct |
61 ms |
50032 KB |
Output is correct |
11 |
Correct |
60 ms |
52160 KB |
Output is correct |
12 |
Correct |
85 ms |
54012 KB |
Output is correct |
13 |
Correct |
83 ms |
54744 KB |
Output is correct |
14 |
Correct |
70 ms |
52828 KB |
Output is correct |
15 |
Correct |
74 ms |
48876 KB |
Output is correct |
16 |
Correct |
72 ms |
48912 KB |
Output is correct |
17 |
Correct |
77 ms |
52148 KB |
Output is correct |
18 |
Correct |
18 ms |
42196 KB |
Output is correct |
19 |
Correct |
19 ms |
42100 KB |
Output is correct |
20 |
Correct |
18 ms |
42140 KB |
Output is correct |
21 |
Correct |
18 ms |
42196 KB |
Output is correct |
22 |
Correct |
18 ms |
42188 KB |
Output is correct |
23 |
Correct |
20 ms |
42324 KB |
Output is correct |
24 |
Correct |
22 ms |
42348 KB |
Output is correct |
25 |
Correct |
73 ms |
42508 KB |
Output is correct |
26 |
Correct |
45 ms |
42604 KB |
Output is correct |
27 |
Correct |
53 ms |
50076 KB |
Output is correct |
28 |
Correct |
102 ms |
54016 KB |
Output is correct |
29 |
Correct |
122 ms |
54824 KB |
Output is correct |
30 |
Correct |
93 ms |
52972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
50200 KB |
Output is correct |
2 |
Correct |
17 ms |
42092 KB |
Output is correct |
3 |
Correct |
17 ms |
42208 KB |
Output is correct |
4 |
Correct |
17 ms |
42160 KB |
Output is correct |
5 |
Correct |
19 ms |
42324 KB |
Output is correct |
6 |
Correct |
23 ms |
42324 KB |
Output is correct |
7 |
Correct |
19 ms |
42324 KB |
Output is correct |
8 |
Correct |
76 ms |
50076 KB |
Output is correct |
9 |
Correct |
72 ms |
49768 KB |
Output is correct |
10 |
Correct |
35 ms |
42340 KB |
Output is correct |
11 |
Correct |
84 ms |
52196 KB |
Output is correct |
12 |
Correct |
19 ms |
42316 KB |
Output is correct |
13 |
Correct |
17 ms |
42196 KB |
Output is correct |
14 |
Correct |
17 ms |
42184 KB |
Output is correct |
15 |
Correct |
17 ms |
42196 KB |
Output is correct |
16 |
Correct |
18 ms |
42160 KB |
Output is correct |
17 |
Correct |
23 ms |
42324 KB |
Output is correct |
18 |
Correct |
25 ms |
42472 KB |
Output is correct |
19 |
Correct |
57 ms |
50116 KB |
Output is correct |
20 |
Correct |
164 ms |
54128 KB |
Output is correct |
21 |
Correct |
95 ms |
52588 KB |
Output is correct |
22 |
Correct |
111 ms |
50636 KB |
Output is correct |
23 |
Correct |
161 ms |
52540 KB |
Output is correct |
24 |
Correct |
65 ms |
49736 KB |
Output is correct |
25 |
Correct |
134 ms |
48932 KB |
Output is correct |
26 |
Correct |
137 ms |
48928 KB |
Output is correct |
27 |
Correct |
17 ms |
42196 KB |
Output is correct |
28 |
Correct |
17 ms |
42196 KB |
Output is correct |
29 |
Correct |
18 ms |
42196 KB |
Output is correct |
30 |
Correct |
18 ms |
42196 KB |
Output is correct |
31 |
Correct |
17 ms |
42208 KB |
Output is correct |
32 |
Correct |
18 ms |
42288 KB |
Output is correct |
33 |
Correct |
19 ms |
42368 KB |
Output is correct |
34 |
Correct |
21 ms |
42472 KB |
Output is correct |
35 |
Correct |
20 ms |
42456 KB |
Output is correct |
36 |
Correct |
61 ms |
50032 KB |
Output is correct |
37 |
Correct |
60 ms |
52160 KB |
Output is correct |
38 |
Correct |
85 ms |
54012 KB |
Output is correct |
39 |
Correct |
83 ms |
54744 KB |
Output is correct |
40 |
Correct |
70 ms |
52828 KB |
Output is correct |
41 |
Correct |
74 ms |
48876 KB |
Output is correct |
42 |
Correct |
72 ms |
48912 KB |
Output is correct |
43 |
Correct |
77 ms |
52148 KB |
Output is correct |
44 |
Correct |
18 ms |
42196 KB |
Output is correct |
45 |
Correct |
19 ms |
42100 KB |
Output is correct |
46 |
Correct |
18 ms |
42140 KB |
Output is correct |
47 |
Correct |
18 ms |
42196 KB |
Output is correct |
48 |
Correct |
18 ms |
42188 KB |
Output is correct |
49 |
Correct |
20 ms |
42324 KB |
Output is correct |
50 |
Correct |
22 ms |
42348 KB |
Output is correct |
51 |
Correct |
73 ms |
42508 KB |
Output is correct |
52 |
Correct |
45 ms |
42604 KB |
Output is correct |
53 |
Correct |
53 ms |
50076 KB |
Output is correct |
54 |
Correct |
102 ms |
54016 KB |
Output is correct |
55 |
Correct |
122 ms |
54824 KB |
Output is correct |
56 |
Correct |
93 ms |
52972 KB |
Output is correct |
57 |
Correct |
332 ms |
57232 KB |
Output is correct |
58 |
Correct |
67 ms |
50124 KB |
Output is correct |
59 |
Correct |
105 ms |
52520 KB |
Output is correct |