Problem p03426 (C++)

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

#define debug(x)                                                               \
  cout << "DEBUG"                                                              \
       << " " << #x << ":" << x << '\n'

// ↓0-originか1-originでn回繰り返し
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)     // 0-indexed昇順
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)   // 1-indexed昇順
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) // 0-indexed降順
#define rrep1(i, n) for (int i = ((int)(n)); i >= 1; i--)  // 1-indexed降順
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii; // 2次元配列
typedef vector<ll> vll;
typedef pair<int, int> PII;

template <typename T> using vec = vector<T>;

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ld PI = acos(-1.0L);
const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
const int mod = 1000000007;
// const int mod = 998244353;

/*FUNCs=================================================*/

// ABC089 - D

/*MAIN==================================================*/
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin cout 高速化
  // cout << fixed << setprecision(10);

  int h, w, d;
  cin >> h >> w >> d;
  vii a(h, vi(w));
  vec<PII> id(h * w + 1);
  rep(i, h) {
    rep(j, w) {
      cin >> a[i][j];
      id[a[i][j]] = make_pair(i, j);
    }
  }

  // 前処理
  // x -> x+dの遷移しかないことを利用して累積和をとる。"特殊な取り方!"
  vi dist(h * w + 1);
  for (int i = 0; i < h * w + 1; ++i) {
    dist[i + d] = dist[i] + abs(id[i].first - id[i + d].first) +
                  abs(id[i].second - id[i + d].second);
  }

  // query
  int q;
  cin >> q;
  vi l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  for (int i = 0; i < q; ++i) {
    cout << dist[r[i]] - dist[l[i]] << '\n';
  }
}

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

#define debug(x)                                                               \
  cout << "DEBUG"                                                              \
       << " " << #x << ":" << x << '\n'

// ↓0-originか1-originでn回繰り返し
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)     // 0-indexed昇順
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)   // 1-indexed昇順
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) // 0-indexed降順
#define rrep1(i, n) for (int i = ((int)(n)); i >= 1; i--)  // 1-indexed降順
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii; // 2次元配列
typedef vector<ll> vll;
typedef pair<int, int> PII;

template <typename T> using vec = vector<T>;

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ld PI = acos(-1.0L);
const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
const int mod = 1000000007;
// const int mod = 998244353;

/*FUNCs=================================================*/

// ABC089 - D

/*MAIN==================================================*/
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin cout 高速化
  // cout << fixed << setprecision(10);

  int h, w, d;
  cin >> h >> w >> d;
  vii a(h, vi(w));
  vec<PII> id(h * w + 1);
  rep(i, h) {
    rep(j, w) {
      cin >> a[i][j];
      id[a[i][j]] = make_pair(i, j);
    }
  }

  // 前処理
  // x -> x+dの遷移しかないことを利用して累積和をとる。"特殊な取り方!"
  vi dist(h * w + 1);
  for (int i = 0; i < h * w + 1 - d; ++i) {
    dist[i + d] = dist[i] + abs(id[i].first - id[i + d].first) +
                  abs(id[i].second - id[i + d].second);
  }

  // query
  int q;
  cin >> q;
  vi l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  for (int i = 0; i < q; ++i) {
    cout << dist[r[i]] - dist[l[i]] << '\n';
  }
}