Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int _n = n, i = 0; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define trav(a, x) for (auto &a : x)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;
#define INF (1ll << 60)

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int H, W, D;
  cin >> H >> W >> D;
  vector<VI> grid(H + 2, VI(W + 2));
  unordered_map<int, PII> M;
  // PII M[H * W + 2];
  FOR(i, 1, H) {
    FOR(j, 1, W) {
      cin >> grid[i][j];
      M[grid[i][j]] = {i, j};
    }
  }
  VI sum(H * W + 2);
  FOR(i, 1, D) {
    sum[i] = 0;
    FOR(j, i + D, H * W) {
      int x1 = M[j - D].first, y1 = M[j - D].second;
      int x2 = M[j].first, y2 = M[j].second;
      int cost = abs(x2 - x1) + abs(y2 - y1);
      sum[j] = sum[j - D] + cost;
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int L, R;
    cin >> L >> R;
    cout << sum[R] - sum[L] << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int _n = n, i = 0; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define trav(a, x) for (auto &a : x)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;
#define INF (1ll << 60)

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int H, W, D;
  cin >> H >> W >> D;
  vector<VI> grid(H + 2, VI(W + 2));
  // unordered_map<int, PII> M;
  PII M[H * W + 2];
  FOR(i, 1, H) {
    FOR(j, 1, W) {
      cin >> grid[i][j];
      M[grid[i][j]] = {i, j};
    }
  }
  VI sum(H * W + 2);
  FOR(i, 1, D) {
    sum[i] = 0;
    FOR(j, i + D, H * W) {
      int x1 = M[j - D].first, y1 = M[j - D].second;
      int x2 = M[j].first, y2 = M[j].second;
      int cost = abs(x2 - x1) + abs(y2 - y1);
      sum[j] = sum[j - D] + cost;
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int L, R;
    cin >> L >> R;
    cout << sum[R] - sum[L] << '\n';
  }
}