-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc089_d.cpp
51 lines (42 loc) · 875 Bytes
/
abc089_d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int h, w, d;
cin >> h >> w >> d;
using t3 = tuple<int,int,int>;
vector<vector<t3>> cords(d);
for (int i = 0; i < h; i++) {
for(int j = 0, ij; j < w; j++) {
cin >> ij;
cords[ij%d].push_back({ij, i, j});
}
}
vector<vector<int>> psums(d);
for (int i = 0; i < d; i++) {
sort(cords[i].begin(), cords[i].end());
auto &psum = psums[i];
int px, py;
for (auto [_, x, y] : cords[i]) {
if (psum.empty()) {
psum.push_back(0);
}
else {
psum.push_back(psum.back() + abs(x-px) + abs(y-py));
}
px = x, py = y;
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
auto idx = l % d;
l = (l+d-1)/d, r = (r+d-1)/d;
l--, r--;
cout << psums[idx][r] - (l ? psums[idx][l] : 0) << '\n';
}
}
// AC, prefix sum