Problem p03426 (C++)

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

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
typedef long long ll;

int h, w, d;
int a[310][310];
int q;
int l[100010], r[100010];
vector<int> v(h *w + 1, 0);
map<int, pair<int, int>> mp;
int main() {
  cin >> h >> w >> d;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      mp[a[i][j]] = make_pair(i, j);
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }

  for (int i = d + 1; i <= h * w; i++) {
    v[i] = abs(mp[i].first - mp[i - d].first) +
           abs(mp[i].second - mp[i - d].second) + v[i - d];
  }

  for (int i = 0; i < q; i++) {
    long long cnt = 0;
    cnt = v[r[i]] - v[l[i]];
    cout << cnt << endl;
  }

  return 0;
}

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

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
typedef long long ll;

int h, w, d;
int a[310][310];
int q;
int l[100010], r[100010];
vector<int> v(300 * 300 + 1, 0);
map<int, pair<int, int>> mp;
int main() {
  cin >> h >> w >> d;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      mp[a[i][j]] = make_pair(i, j);
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }

  for (int i = d + 1; i <= h * w; i++) {
    v[i] = abs(mp[i].first - mp[i - d].first) +
           abs(mp[i].second - mp[i - d].second) + v[i - d];
  }

  for (int i = 0; i < q; i++) {
    long long cnt = 0;
    cnt = v[r[i]] - v[l[i]];
    cout << cnt << endl;
  }

  return 0;
}