Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define reprb(i, a, b) for (ll i = (a)-1; i >= (b); i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(unsigned long long x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll h, w, d;
  cin >> h >> w >> d;
  vvll a(h, vll(w));
  vll x(h * w + 1, 0);
  vll y(h * w + 1, 0);
  vll cost(h * w + 1 - d, 0);
  vll costcs(h * w + 1 - d, 0);
  rep(i, h) rep(j, w) {
    cin >> a[i][j];
    x[a[i][j]] = i;
    y[a[i][j]] = j;
  }
  repr(i, 1, h * w + 1 - d) {
    cost[i] = abs(x[i + d] - x[i]) + abs(y[i + d] - y[i]);
  }
  repr(i, 1, d + 1) { costcs[i] = cost[i]; }
  repr(i, d + 1, h * w + 1 - d) { costcs[i] = costcs[i - d] + cost[i]; }
  dump(cost);
  dump(costcs);
  ll q;
  cin >> q;
  rep(i, q) {
    ll ans = 0;
    ll l, r;
    cin >> l >> r;
    if (l == r)
      ans = 0;
    else if (l - d >= 1)
      ans = costcs[r - d] - costcs[l - d];
    else
      ans = costcs[r - d];
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define reprb(i, a, b) for (ll i = (a)-1; i >= (b); i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(unsigned long long x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll h, w, d;
  cin >> h >> w >> d;
  vvll a(h, vll(w));
  vll x(h * w + 1, 0);
  vll y(h * w + 1, 0);
  vll cost(h * w + 1 - d, 0);
  vll costcs(h * w + 1 - d, 0);
  rep(i, h) rep(j, w) {
    cin >> a[i][j];
    x[a[i][j]] = i;
    y[a[i][j]] = j;
  }
  repr(i, 1, h * w + 1 - d) {
    cost[i] = abs(x[i + d] - x[i]) + abs(y[i + d] - y[i]);
  }
  repr(i, 1, min(d + 1, h * w + 1 - d)) { costcs[i] = cost[i]; }
  repr(i, min(d + 1, h * w + 1 - d), h * w + 1 - d) {
    costcs[i] = costcs[i - d] + cost[i];
  }
  dump(cost);
  dump(costcs);
  ll q;
  cin >> q;
  rep(i, q) {
    ll ans = 0;
    ll l, r;
    cin >> l >> r;
    if (l == r)
      ans = 0;
    else if (l - d >= 1)
      ans = costcs[r - d] - costcs[l - d];
    else
      ans = costcs[r - d];
    cout << ans << endl;
  }
  return 0;
}