prithivMLmods/Coder-Stat huggingface dataset:
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  pair<int, int> A[H * W + 1];
  rep(i, H) {
    rep(j, W) {
      int tmp;
      cin >> tmp;
      A[tmp] = make_pair(i + 1, j + 1);
    }
  }
  int Q;
  cin >> Q;
  int C[H * W + 1];
  REP(i, 1, D + 1) {
    int j = i;
    int res = 0;
    while (j < H * W + 1) {
      C[j] = res;
      j += D;
      res +=
          abs(A[j].first - A[j - D].first) + abs(A[j].second - A[j - D].second);
    }
  }
  rep(i, Q) {
    int L, R;
    cin >> L >> R;
    //        cout << C[R] << " " << C[L] << endl;
    cout << C[R] - C[L] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  pair<int, int> A[H * W + 1];
  rep(i, H) {
    rep(j, W) {
      int tmp;
      cin >> tmp;
      A[tmp] = make_pair(i + 1, j + 1);
    }
  }
  int Q;
  cin >> Q;
  int C[H * W + 1];
  REP(i, 1, D + 1) {
    int j = i;
    int res = 0;
    while (j < H * W + 1) {
      C[j] = res;
      j += D;
      if (j < H * W + 1) {
        res += abs(A[j].first - A[j - D].first) +
               abs(A[j].second - A[j - D].second);
      }
    }
  }
  rep(i, Q) {
    int L, R;
    cin >> L >> R;
    //        cout << C[R] << " " << C[L] << endl;
    cout << C[R] - C[L] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define MAX 1000000007
using namespace std;

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;

  pair<int, int> a[h * w + 1];
  rep(i, h) {
    rep(j, w) {
      int tmp;
      cin >> tmp;
      a[tmp].first = i + 1;
      a[tmp].second = j + 1;
    }
  }
  vector<vector<int>> cost(d);
  rep(i, d) {
    int num = 0;
    cost[i].push_back(0);
    for (int j = i; j < h * w; j += d) {
      cost[i].push_back(cost[i][num] + abs(a[j].first - a[j + d].first) +
                        abs(a[j].second - a[j + d].second));
      num++;
    }
  }

  /*rep(i,d){
        rep(j,cost[i].size()){
          cout << cost[i][j] << " ";
        }
        cout << endl;
        }*/

  int q;
  cin >> q;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    cout << cost[r % d][(r - r % d) / d] - cost[l % d][(l - l % d) / d] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define MAX 1000000007
using namespace std;

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;

  pair<int, int> a[h * w + 1];
  rep(i, h) {
    rep(j, w) {
      int tmp;
      cin >> tmp;
      a[tmp].first = i + 1;
      a[tmp].second = j + 1;
    }
  }
  vector<vector<int>> cost(d);
  rep(i, d) {
    int num = 0;
    cost[i].push_back(0);
    for (int j = i; j + d <= h * w; j += d) {
      cost[i].push_back(cost[i][num] + abs(a[j].first - a[j + d].first) +
                        abs(a[j].second - a[j + d].second));
      num++;
    }
  }

  /*rep(i,d){
        rep(j,cost[i].size()){
          cout << cost[i][j] << " ";
        }
        cout << endl;
        }*/

  int q;
  cin >> q;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    cout << cost[r % d][(r - r % d) / d] - cost[l % d][(l - l % d) / d] << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

typedef long long ll;

int r, c;
ll d, px[9005], py[9005], dis[9005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int x, y, q;
  while (cin >> r >> c >> d) {
    for (int i = 1; i <= r; i++)
      for (int j = 1; j <= c; j++) {
        cin >> x;
        px[x] = i;
        py[x] = j;
      }

    for (int i = r * c; i >= 1; i--)
      if (i + d > r * c)
        dis[i] = 0;
      else
        dis[i] = dis[i + d] + abs(px[i + d] - px[i]) + abs(py[i + d] - py[i]);

    cin >> q;
    while (q--) {
      cin >> x >> y;
      cout << dis[x] - dis[y] << '\n';
    }
  }
  return 0;
}

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

typedef long long ll;

int r, c;
ll d, px[90005], py[90005], dis[90005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int x, y, q;
  while (cin >> r >> c >> d) {
    for (int i = 1; i <= r; i++)
      for (int j = 1; j <= c; j++) {
        cin >> x;
        px[x] = i;
        py[x] = j;
      }

    for (int i = r * c; i >= 1; i--)
      if (i + d > r * c)
        dis[i] = 0;
      else
        dis[i] = dis[i + d] + abs(px[i + d] - px[i]) + abs(py[i + d] - py[i]);

    cin >> q;
    while (q--) {
      cin >> x >> y;
      cout << dis[x] - dis[y] << '\n';
    }
  }
  return 0;
}
Problem p03426 (C++)

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

pair<int, int> b[100000];
vector<long> c[1000];
int main() {
  int H, W, D;
  cin >> H >> W >> D;
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j) {
      int a;
      cin >> a;
      b[a] = make_pair(i + 1, j + 1);
    }
  for (int i = 1; i <= D; ++i) {
    int x = 0, y = 0;
    if (i == D)
      c[i].push_back(0);
    for (int j = i; j <= H * W; j += D) {
      int s, t;
      tie(s, t) = b[j];
      c[i].push_back(abs(x - s) + abs(y - t));
      x = s, y = t;
    }
    for (int j = 1; j < c[i].size(); ++j)
      c[i][j] += c[i][j - 1];
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int L, R;
    cin >> L >> R;
    int m = L % D ?: D;
    cout << c[m][R / D] - c[m][L / D] << endl;
  }
}


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

pair<int, int> b[100000];
vector<long> c[100000];
int main() {
  int H, W, D;
  cin >> H >> W >> D;
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j) {
      int a;
      cin >> a;
      b[a] = make_pair(i + 1, j + 1);
    }
  for (int i = 1; i <= D; ++i) {
    int x = 0, y = 0;
    if (i == D)
      c[i].push_back(0);
    for (int j = i; j <= H * W; j += D) {
      int s, t;
      tie(s, t) = b[j];
      c[i].push_back(abs(x - s) + abs(y - t));
      x = s, y = t;
    }
    for (int j = 1; j < c[i].size(); ++j)
      c[i][j] += c[i][j - 1];
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int L, R;
    cin >> L >> R;
    int m = L % D ?: D;
    cout << c[m][R / D] - c[m][L / D] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using TP = tuple<ll, ll, ll>;
ll MOD = 1e9 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)1e18;
int xv[4] = {-1, 1, 0, 0};
int yv[4] = {0, 0, -1, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W, D;
  cin >> H >> W >> D;
  vector<P> mp(H * W + 1);
  vector<int> dp(H * W + 2, 0);

  REP(i, H) {
    REP(j, W) {
      int a;
      cin >> a;
      mp[a] = make_pair(j + 1, i + 1);
    }
  }

  for (int i = D; i < H * W + 1; i++) {
    if (dp[i] == 0) {
      dp[i] = abs(mp[i].first - mp[i - D].first) +
              abs(mp[i].second - mp[i - D].second) + dp[i - D];
    }
  }
  //    for(int i = 0; i < H*W+1; i++){
  //        cout << i << "," << dp[i] << endl;;
  //    }
  //    cout << endl;

  int Q;
  cin >> Q;
  REP(i, Q) {
    int L, R;
    cin >> L >> R;
    //        ll ans = 0;
    //        cout << L << "," << R << endl;
    for (int j = L; j < R; j += D) {
      //            cout << j << ":" << mp[j+D].first << "-" << mp[j].first <<
      //            endl; ans += abs(mp[j+D].first - mp[j].first) +
      //            abs(mp[j+D].second - mp[j].second);
    }
    //        cout << dp[R] - dp[L] << ":" << ans << endl;
    cout << dp[R] - dp[L] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using TP = tuple<ll, ll, ll>;
ll MOD = 1e9 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)1e18;
int xv[4] = {-1, 1, 0, 0};
int yv[4] = {0, 0, -1, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W, D;
  cin >> H >> W >> D;
  vector<P> mp(H * W + 1);
  vector<int> dp(H * W + 2, 0);

  REP(i, H) {
    REP(j, W) {
      int a;
      cin >> a;
      mp[a] = make_pair(j + 1, i + 1);
    }
  }

  for (int i = D; i < H * W + 1; i++) {
    if (dp[i] == 0) {
      dp[i] = abs(mp[i].first - mp[i - D].first) +
              abs(mp[i].second - mp[i - D].second) + dp[i - D];
    }
  }
  //    for(int i = 0; i < H*W+1; i++){
  //        cout << i << "," << dp[i] << endl;;
  //    }
  //    cout << endl;

  int Q;
  cin >> Q;
  REP(i, Q) {
    int L, R;
    cin >> L >> R;
    //        ll ans = 0;
    //        cout << L << "," << R << endl;
    //        for(int j = L; j < R; j+=D){
    //            cout << j << ":" << mp[j+D].first << "-" << mp[j].first <<
    //            endl; ans += abs(mp[j+D].first - mp[j].first) +
    //            abs(mp[j+D].second - mp[j].second);
    //        }
    //        cout << dp[R] - dp[L] << ":" << ans << endl;
    cout << dp[R] - dp[L] << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int x[9005], y[9005], dp[9005];
int main() {
  int h, w, d;
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int p;
      cin >> p;
      x[p] = i;
      y[p] = j;
    }
  for (int i = 0; i <= h * w; i++)
    if (i >= d) {
      dp[i] += dp[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d]);
    }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    cout << dp[b] - dp[a] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int x[90005], y[90005], dp[90005];
int main() {
  int h, w, d;
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int p;
      cin >> p;
      x[p] = i;
      y[p] = j;
    }
  for (int i = 0; i <= h * w; i++)
    if (i >= d) {
      dp[i] += dp[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d]);
    }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    cout << dp[b] - dp[a] << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/* Coding space */
int a[100000] = {};
int main() {
  int h, w, d;
  cin >> h >> w >> d;
  map<int, tii> m;
  map<int, map<int, int>> ans;
  rep(i, h) rep(j, w) {
    int in;
    cin >> in;
    m[in] = tii{i, j};
  }

  for (int i = 1; i <= d; i++) {
    int s = i;
    while (s <= h * w) {
      int sx, sy;
      tie(sx, sy) = m[s];
      int nx, ny;
      tie(nx, ny) = m[s + d];
      s += d;
      a[s] = abs(sx - nx) + abs(sy - ny);
      a[s] += a[s - d];
    }
  }

  int q;
  cin >> q;
  rep(i, q) {
    int s, g;
    cin >> s >> g;
    cout << a[g] - a[s] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/* Coding space */
int a[100000] = {};
int main() {
  int h, w, d;
  cin >> h >> w >> d;
  map<int, tii> m;
  map<int, map<int, int>> ans;
  rep(i, h) rep(j, w) {
    int in;
    cin >> in;
    m[in] = tii{i, j};
  }

  for (int i = 1; i <= d; i++) {
    int s = i;
    while (s + d <= h * w) {
      int sx, sy;
      tie(sx, sy) = m[s];
      int nx, ny;
      tie(nx, ny) = m[s + d];
      s += d;
      a[s] = abs(sx - nx) + abs(sy - ny);
      a[s] += a[s - d];
    }
  }

  int q;
  cin >> q;
  rep(i, q) {
    int s, g;
    cin >> s >> g;
    cout << a[g] - a[s] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <utility>
#include <vector>
using namespace std;
#define LL long long

void solve(long long H, long long W, long long D,
           std::vector<std::vector<long long>> A, long long Q,
           std::vector<long long> L, std::vector<long long> R) {
  vector<pair<LL, LL>> flatten(H * W + 1);
  for (LL i = 0; i < H; i++) {
    for (LL j = 0; j < W; j++) {
      flatten[A[i][j]] = make_pair(i + 1, j + 1);
    }
  }
  vector<vector<LL>> arr(D, vector<LL>((H * W + D) / D, 0));
  for (LL i = 0; i < D; i++) {
    LL acc = 0;
    for (LL j = 1; j < (H * W + D) / D; j++) {
      LL prev = D * (j - 1) + i, curr = D * j + i;
      acc += (abs(flatten[prev].first - flatten[curr].first) +
              abs(flatten[prev].second - flatten[curr].second));
      arr[i][j] = acc;
    }
  }

  for (LL i = 0; i < Q; i++) {
    auto j = R[i] % D;
    cout << arr[j][R[i] / D] - arr[j][L[i] / D] << endl;
  }
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;

  scanf("%lld", &W);
  long long D;
  scanf("%lld", &D);
  std::vector<std::vector<long long>> A(H, std::vector<long long>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%lld", &A[i][j]);
    }
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> L(Q);
  std::vector<long long> R(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &L[i]);
    scanf("%lld", &R[i]);
  }
  solve(H, W, D, std::move(A), Q, std::move(L), std::move(R));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <utility>
#include <vector>
using namespace std;
#define LL long long

void solve(long long H, long long W, long long D,
           std::vector<std::vector<long long>> A, long long Q,
           std::vector<long long> L, std::vector<long long> R) {
  vector<pair<LL, LL>> flatten(H * W + 1);
  for (LL i = 0; i < H; i++) {
    for (LL j = 0; j < W; j++) {
      flatten[A[i][j]] = make_pair(i + 1, j + 1);
    }
  }
  vector<vector<LL>> arr(D, vector<LL>((H * W + D) / D, 0));
  for (LL i = 0; i < D; i++) {
    LL acc = 0;
    for (LL j = 1; j < (H * W + D) / D; j++) {
      LL prev = D * (j - 1) + i, curr = D * j + i;
      if (curr > H * W)
        continue;
      acc += (abs(flatten[prev].first - flatten[curr].first) +
              abs(flatten[prev].second - flatten[curr].second));
      arr[i][j] = acc;
    }
  }

  for (LL i = 0; i < Q; i++) {
    auto j = R[i] % D;
    cout << arr[j][R[i] / D] - arr[j][L[i] / D] << endl;
  }
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;

  scanf("%lld", &W);
  long long D;
  scanf("%lld", &D);
  std::vector<std::vector<long long>> A(H, std::vector<long long>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%lld", &A[i][j]);
    }
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> L(Q);
  std::vector<long long> R(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &L[i]);
    scanf("%lld", &R[i]);
  }
  solve(H, W, D, std::move(A), Q, std::move(L), std::move(R));
  return 0;
}
Problem p03426 (C++)

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

long long N, H, W, D, x[309], y[309], dp[1000009];

int main() {
  cin >> H >> W >> D;
  for (int i = 1; i <= H * W; i++) {
    int p;
    cin >> p;
    x[p] = (i - 1) / W;
    y[p] = (i - 1) % W;
  }
  N = H * W;

  for (int i = 1; i <= N; i++) {
    if (i <= D)
      dp[i] = 0;
    else {
      dp[i] = dp[i - D] + abs(x[i] - x[i - D]) + abs(y[i] - y[i - D]);
    }
  }
  int Q;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    int L, R;
    cin >> L >> R;
    cout << dp[R] - dp[L] << endl;
  }
  return 0;
}


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

long long N, H, W, D, x[100009], y[100009], dp[1000009];

int main() {
  cin >> H >> W >> D;
  for (int i = 1; i <= H * W; i++) {
    int p;
    cin >> p;
    x[p] = (i - 1) / W;
    y[p] = (i - 1) % W;
  }
  N = H * W;

  for (int i = 1; i <= N; i++) {
    if (i <= D)
      dp[i] = 0;
    else {
      dp[i] = dp[i - D] + abs(x[i] - x[i - D]) + abs(y[i] - y[i - D]);
    }
  }
  int Q;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    int L, R;
    cin >> L >> R;
    cout << dp[R] - dp[L] << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define ALLR(X) (X).rbegin(), (X).rend()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define F first
#define S second

using namespace std;

template <class T> void max_self(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> void min_self(T &a, const T &b) {
  if (a > b)
    a = b;
}
typedef long long LL;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = (1e-9);

int main() {
  FAST_IO

  int h, w, d;
  cin >> h >> w >> d;

  pair<int, int> pts[9001];
  int prefS[9001];

  memset(prefS, 0, sizeof prefS);
  for (int row = 1; row <= h; ++row) {
    for (int col = 1; col <= w; ++col) {
      int x;
      cin >> x;
      pts[x] = MP(row, col);
    }
  }

  for (int start = d + 1; start <= h * w; ++start) {
    prefS[start] = prefS[start - d] + abs(pts[start - d].F - pts[start].F) +
                   abs(pts[start - d].S - pts[start].S);
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int left, right;
    cin >> left >> right;
    cout << prefS[right] - prefS[left] << "\n";
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define ALLR(X) (X).rbegin(), (X).rend()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define F first
#define S second

using namespace std;

template <class T> void max_self(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> void min_self(T &a, const T &b) {
  if (a > b)
    a = b;
}
typedef long long LL;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = (1e-9);

int main() {
  FAST_IO

  int h, w, d;
  cin >> h >> w >> d;

  pair<int, int> pts[90001];
  int prefS[90001];

  memset(prefS, 0, sizeof prefS);
  for (int row = 1; row <= h; ++row) {
    for (int col = 1; col <= w; ++col) {
      int x;
      cin >> x;
      pts[x] = MP(row, col);
    }
  }

  for (int start = d + 1; start <= h * w; ++start) {
    prefS[start] = prefS[start - d] + abs(pts[start - d].F - pts[start].F) +
                   abs(pts[start - d].S - pts[start].S);
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int left, right;
    cin >> left >> right;
    cout << prefS[right] - prefS[left] << "\n";
  }

  return 0;
}
Problem p03426 (C++)

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

using namespace std;
#define pb push_back
const int Nmax = 100000 + 5;
int n, Fi[Nmax], Fj[Nmax], m, d, q;
vector<int> v[Nmax];
int main() {
  cin >> n >> m >> d;
  for (int i = 1; i <= n; ++i)
    for (int j = 1, x; j <= m; ++j) {
      cin >> x;
      Fi[x] = i;
      Fj[x] = j;
    }
  for (int r = 1; r <= d; ++r) {
    int cc = 0, li = Fi[r], lj = Fj[r], rr = r;
    while (Fi[rr]) {
      cc += abs(li - Fi[rr]) + abs(lj - Fj[rr]);
      li = Fi[rr];
      lj = Fj[rr];
      v[r].pb(cc);
      rr += d;
    }
  }
  cin >> q;
  for (int i = 1, x, y; i <= q; ++i) {
    cin >> x >> y;
    int rr = x % d;
    if (!rr)
      rr = d, y--, x--;
    int ans = v[rr][y / d];
    if (x / d != 0)
      ans -= v[rr][x / d];
    cout << ans << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
const int Nmax = 100000 + 5;
int n, Fi[Nmax], Fj[Nmax], m, d, q;
vector<int> v[Nmax];
int main() {
  cin >> n >> m >> d;
  for (int i = 1; i <= n; ++i)
    for (int j = 1, x; j <= m; ++j) {
      cin >> x;
      Fi[x] = i;
      Fj[x] = j;
    }
  for (int r = 1; r <= d; ++r) {
    int cc = 0, li = Fi[r], lj = Fj[r], rr = r;
    while (rr <= 300 * 300 && Fi[rr]) {
      cc += abs(li - Fi[rr]) + abs(lj - Fj[rr]);
      li = Fi[rr];
      lj = Fj[rr];
      v[r].pb(cc);
      rr += d;
    }
  }
  cin >> q;
  for (int i = 1, x, y; i <= q; ++i) {
    cin >> x >> y;
    int rr = x % d;
    if (!rr)
      rr = d, y--, x--;
    int ans = v[rr][y / d];
    if (x / d != 0)
      ans -= v[rr][x / d];
    cout << ans << '\n';
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)

int main() {
  ll H, W, D;

  cin >> H >> W >> D;

  map<int, pair<ll, ll>> m;

  ll a[H][W];
  rep(y, H) {
    rep(x, W) {
      cin >> a[y][x];
      m[a[y][x]] = mkp(y, x);
    }
  }

  vector<ll> imos(H * W, 0);

  for (int i = D; i <= H * W; i++) {
    pair<int, int> cp = m[i - D];
    pair<int, int> np = m[i];
    imos[i] =
        imos[i - D] + abs(cp.first - np.first) + abs(cp.second - np.second);
  }

  int q;
  cin >> q;

  rep(i, q) {
    int L, R;
    cin >> L >> R;

    cout << imos[R] - imos[L] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)

int main() {
  ll H, W, D;

  cin >> H >> W >> D;

  map<int, pair<ll, ll>> m;

  ll a[H][W];
  rep(y, H) {
    rep(x, W) {
      cin >> a[y][x];
      m[a[y][x]] = mkp(y, x);
    }
  }

  vector<ll> imos(H * W + 1, 0);

  for (int i = D; i <= H * W; i++) {
    pair<int, int> cp = m[i - D];
    pair<int, int> np = m[i];
    imos[i] =
        imos[i - D] + abs(cp.first - np.first) + abs(cp.second - np.second);
  }

  int q;
  cin >> q;

  rep(i, q) {
    int L, R;
    cin >> L >> R;

    cout << imos[R] - imos[L] << endl;
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll MOD = 1000000007;
constexpr ll INF = 1050000000;
constexpr ll LONGINF = 1050000000000000000;

int main() {
  int h, w, d;
  cin >> h >> w >> d;

  map<int, pair<int, int>> mp;

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

  vector<int> ruiseki(h * w + 1, 0);

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

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    int ans = 0;
    while (l != r) {
      int old = l;
      int New = l + d;
      ans += abs(mp[old].first - mp[New].first) +
             abs(mp[old].second - mp[New].second);
      l = New;
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll MOD = 1000000007;
constexpr ll INF = 1050000000;
constexpr ll LONGINF = 1050000000000000000;

int main() {
  int h, w, d;
  cin >> h >> w >> d;

  map<int, pair<int, int>> mp;

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

  vector<int> ruiseki(h * w + 1, 0);

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

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << ruiseki[r] - ruiseki[l] << endl;
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
int main(void) {
  int H, W, D;
  scanf("%d%d%d", &H, &W, &D);
  int A;
  int i, j;
  int ax[90001], ay[90001];
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      scanf("%d", &A);
      ax[A] = i;
      ay[A] = j;
    }
  }
  int Q;
  scanf("%d", &Q);
  int L, R;
  int ans;
  int d[100001];
  d[0] = 0;
  for (j = 0; j <= Q; j += D) {

    d[j + D] = d[j] + abs(ax[j + D] - ax[j]) + abs(ay[j + D] - ay[j]);
  }
  for (i = 0; i < Q; i++) {
    scanf("%d%d", &L, &R);
    printf("%d\n", d[R] - d[L]);
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
int main(void) {
  int H, W, D;
  scanf("%d%d%d", &H, &W, &D);
  int A;
  int i, j;
  int ax[90001], ay[90001];
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      scanf("%d", &A);
      ax[A] = i;
      ay[A] = j;
    }
  }
  int Q;
  scanf("%d", &Q);
  int L, R;
  int ans;
  int d[90001];
  for (i = 0; i < D; i++) {
    d[i] = 0;
  }
  for (j = 0; j + D <= H * W; j++) {
    d[j + D] = d[j] + abs(ax[j + D] - ax[j]) + abs(ay[j + D] - ay[j]);
  }
  for (i = 0; i < Q; i++) {
    scanf("%d%d", &L, &R);
    printf("%d\n", d[R] - d[L]);
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll a[330][330];
tuple<ll, ll, ll> g[100000];
ll l[110000], r[110000];
ll z[200000];
int main() {
  ll h, w, d, ans = 0;
  cin >> h >> w >> d;
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> a[i][j];
      get<1>(g[a[i][j]]) = i;
      get<2>(g[a[i][j]]) = j;
    }
  }
  ll q;
  cin >> q;
  rep(i, 0, q) cin >> l[i] >> r[i];

  rep(x, 1, d + 1) {
    ll npx, npy;
    ll pos = x;
    npx = get<1>(g[pos]);
    npy = get<2>(g[pos]);
    while (pos <= h * w) {
      pos += d;
      z[pos] =
          z[pos - d] + labs(npx - get<1>(g[pos])) + labs(npy - get<2>(g[pos]));
      npx = get<1>(g[pos]);
      npy = get<2>(g[pos]);
    }
  }

  rep(x, 0, q) { cout << z[r[x]] - z[l[x]] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll a[330][330];
tuple<ll, ll, ll> g[200000];
ll l[110000], r[110000];
ll z[200000];
int main() {
  ll h, w, d, ans = 0;
  cin >> h >> w >> d;
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> a[i][j];
      get<1>(g[a[i][j]]) = i;
      get<2>(g[a[i][j]]) = j;
    }
  }
  ll q;
  cin >> q;
  rep(i, 0, q) cin >> l[i] >> r[i];

  rep(x, 1, d + 1) {
    ll npx, npy;
    ll pos = x;
    npx = get<1>(g[pos]);
    npy = get<2>(g[pos]);
    while (pos <= h * w) {
      pos += d;
      z[pos] =
          z[pos - d] + labs(npx - get<1>(g[pos])) + labs(npy - get<2>(g[pos]));
      npx = get<1>(g[pos]);
      npy = get<2>(g[pos]);
    }
  }

  rep(x, 0, q) { cout << z[r[x]] - z[l[x]] << endl; }
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> p;
typedef complex<double> com;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const ll INF = 1e18;

int main() {
  int n, m, d;
  cin >> n >> m >> d;
  vector<p> a(n * m);
  rep(i, 0, n) {
    rep(j, 0, m) {
      int p;
      cin >> p;
      p--;
      a[p] = {i, j};
    }
  }
  vector<int> b(n * m, 0);
  rep(i, 0, d) { b[i] = a[i].first + a[i].second; }
  rep(i, d, n * m) {
    b[i] = b[i - d] + abs(a[i].first - a[i - d].first) +
           abs(a[i].second - a[i - d].second);
  }
  int q;
  cin >> q;
  rep(i, 0, q) {
    int p1, p2;
    cin >> p1 >> p2;
    p1--;
    p2--;
    ll ans = 0;
    while (p1 != p2) {
      ans += b[p1 + d] - b[p1];
      p1 += d;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> p;
typedef complex<double> com;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const ll INF = 1e18;

int main() {
  int n, m, d;
  cin >> n >> m >> d;
  vector<p> a(n * m);
  rep(i, 0, n) {
    rep(j, 0, m) {
      int p;
      cin >> p;
      p--;
      a[p] = {i, j};
    }
  }
  vector<int> b(n * m, 0);
  rep(i, 0, d) { b[i] = a[i].first + a[i].second; }
  rep(i, d, n * m) {
    b[i] = b[i - d] + abs(a[i].first - a[i - d].first) +
           abs(a[i].second - a[i - d].second);
  }
  int q;
  cin >> q;
  rep(i, 0, q) {
    int p1, p2;
    cin >> p1 >> p2;
    p1--;
    p2--;
    cout << b[p2] - b[p1] << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using PII = pair<int, int>;

int main() {
  int h, w, d, tmp;
  cin >> h >> w >> d;

  vector<vector<int>> a(h, vector<int>(w, 0));
  vector<PII> val2point(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> tmp;
      tmp--;
      val2point[tmp] = make_pair(i, j);
      a[i][j] = tmp;
    }
  }

  vector<long long> cumul(h * w + d, -1);
  for (int st = 0; st < d; st++) {
    cumul[st] = 0;

    for (int i = st; i + d < cumul.size(); i += d) {
      auto from = val2point[i];
      auto to = val2point[i + d];

      cumul[i + d] =
          cumul[i] + abs(to.first - from.first) + abs(to.second - from.second);
    }
  }

  int q, l, r;
  cin >> q;
  vector<long long> ans(q);
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    l--;
    r--;
    ans[i] = cumul[r] - cumul[l];
  }

  for (auto x : ans) {
    cout << x << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using PII = pair<int, int>;

int main() {
  int h, w, d, tmp;
  cin >> h >> w >> d;

  vector<vector<int>> a(h, vector<int>(w, 0));
  vector<PII> val2point(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> tmp;
      tmp--;
      val2point[tmp] = make_pair(i, j);
      a[i][j] = tmp;
    }
  }

  vector<long long> cumul(h * w, -1);
  for (int st = 0; st < d; st++) {
    cumul[st] = 0;

    for (int i = st; i + d < cumul.size(); i += d) {
      auto from = val2point[i];
      auto to = val2point[i + d];

      cumul[i + d] =
          cumul[i] + abs(to.first - from.first) + abs(to.second - from.second);
    }
  }

  int q, l, r;
  cin >> q;
  vector<long long> ans(q);
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    l--;
    r--;
    ans[i] = cumul[r] - cumul[l];
  }

  for (auto x : ans) {
    cout << x << endl;
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int px[90000];
int py[90000];
vector<int> ret[90000];
int mp(int a, int b) { return abs(px[a] - px[b]) + abs(py[a] - py[b]); }
int main(void) {
  ios::sync_with_stdio(false);
  int H, W, D, Q;
  cin >> H >> W >> D;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      int a;
      cin >> a;
      a--;
      px[a] = x;
      py[a] = y;
    }
  }
  for (int i = 0; i < D; i++) {
    int j = i;
    ret[i].push_back(0);
    while (j < H * W) {
      ret[i].push_back(ret[i][j / D] + mp(j, j + D));
      j += D;
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    int rem = l % D;
    cout << ret[rem][r / D] - ret[rem][l / D] << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int px[90000];
int py[90000];
vector<int> ret[90000];
int mp(int a, int b) { return abs(px[a] - px[b]) + abs(py[a] - py[b]); }
int main(void) {
  ios::sync_with_stdio(false);
  int H, W, D, Q;
  cin >> H >> W >> D;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      int a;
      cin >> a;
      a--;
      px[a] = x;
      py[a] = y;
    }
  }
  for (int i = 0; i < D; i++) {
    int j = i;
    ret[i].push_back(0);
    while (j + D < H * W) {
      ret[i].push_back(ret[i][j / D] + mp(j, j + D));
      j += D;
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    int rem = l % D;
    cout << ret[rem][r / D] - ret[rem][l / D] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T, class U> inline bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MAX = 110000;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<int>> a(h, vector<int>(w));
  map<int, pair<int, int>> mp;
  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);
    }
  }

  long long cost[MAX];
  fill(cost, cost + MAX, 0);
  for (int i = 1; i <= h * w; i++) {
    pair<int, int> a = mp[i], b = mp[i + d];
    int c = abs(a.first - b.first) + abs(a.second - b.second);
    cost[i + d] = cost[i] + c;
  }

  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << cost[r] - cost[l] << "\n";
  }
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T, class U> inline bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MAX = 220000;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<int>> a(h, vector<int>(w));
  map<int, pair<int, int>> mp;
  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);
    }
  }

  long long cost[MAX];
  fill(cost, cost + MAX, 0);
  for (int i = 1; i <= h * w; i++) {
    pair<int, int> a = mp[i], b = mp[i + d];
    int c = abs(a.first - b.first) + abs(a.second - b.second);
    cost[i + d] = cost[i] + c;
  }

  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << cost[r] - cost[l] << "\n";
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vvi a(h, vi(w));
  vector<P> mp(h * w);
  rep(i, h) {
    rep(j, w) {
      cin >> a[i][j];
      mp[a[i][j]] = P(i, j);
    }
  }

  int q;
  cin >> q;
  vi r(q), l(q);
  rep(i, q) { cin >> l[i] >> r[i]; }

  vi sum(h * w + 1);
  rep(i, h * w - d) {
    ++i;
    sum[i + d] = sum[i] + abs(mp[i + d].first - mp[i].first) +
                 abs(mp[i + d].second - mp[i].second);
    --i;
  }

  rep(i, q) { cout << sum[r[i]] - sum[l[i]] << '\n'; }
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vvi a(h, vi(w));
  vector<P> mp(h * w + 1);
  rep(i, h) {
    rep(j, w) {
      cin >> a[i][j];
      mp[a[i][j]] = P(i, j);
    }
  }

  int q;
  cin >> q;
  vi r(q), l(q);
  rep(i, q) { cin >> l[i] >> r[i]; }

  vi sum(h * w + 1);
  rep(i, h * w - d) {
    ++i;
    sum[i + d] = sum[i] + abs(mp[i + d].first - mp[i].first) +
                 abs(mp[i + d].second - mp[i].second);
    --i;
  }

  rep(i, q) { cout << sum[r[i]] - sum[l[i]] << '\n'; }
}
Problem p03426 (C++)

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

#define COUT(x) cout << #x << " = " << (x) << ", "
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;
}
typedef long long ll;
const int MOD = 1000000007;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<pair<int, int>> Grid(H * W + 1);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int a;
      cin >> a;
      Grid[a].first = i + 1;
      Grid[a].second = j + 1;
    }
  }
  vector<ll> Sum(H * W * 1);
  for (int i = 1; i <= H * W; i++) {
    if (i <= D) {
      Sum[i] = 0;
    } else {
      Sum[i] = Sum[i - D] + abs(Grid[i].first - Grid[i - D].first) +
               abs(Grid[i].second - Grid[i - D].second);
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    int res = Sum[r] - Sum[l];
    cout << res << endl;
  }
}


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

#define COUT(x) cout << #x << " = " << (x) << ", "
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;
}
typedef long long ll;
const int MOD = 1000000007;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<pair<int, int>> Grid(H * W + 1);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int a;
      cin >> a;
      Grid[a].first = i + 1;
      Grid[a].second = j + 1;
    }
  }
  vector<ll> Sum(H * W + 1);
  for (int i = 1; i <= H * W; i++) {
    if (i <= D) {
      Sum[i] = 0;
    } else {
      Sum[i] = Sum[i - D] + abs(Grid[i].first - Grid[i - D].first) +
               abs(Grid[i].second - Grid[i - D].second);
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    int res = Sum[r] - Sum[l];
    cout << res << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int H, W, D;

int abs(int a) noexcept;
int mdist(pair<int, int> a, pair<int, int> b);

int main() {
  cin >> H >> W >> D;
  vector<pair<int, int>> A(H * W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int tmp;
      cin >> tmp;
      A[tmp - 1] = make_pair(i, j);
    }
  }
  vector<vector<int>> Dists(D, vector<int>((H * W) / D + 1, 0));
  for (int k = 0; k < D; k++) {
    pair<int, int> prevP = A[k];
    Dists[k][0] = 0;
    for (int i = 1; i < (H * W) / D + 1; i++) {
      pair<int, int> tmpP = A[k + D * i];
      Dists[k][i] = Dists[k][i - 1] + mdist(tmpP, prevP);
      prevP = tmpP;
    }
  }
  int Q;
  cin >> Q;
  vector<pair<int, int>> T(Q); // L, R
  for (int i = 0; i < Q; i++) {
    cin >> T[i].first >> T[i].second;
    T[i].first--;
    T[i].second--;
  }
  for (int i = 0; i < Q; i++) {
    int surp = T[i].first % D;
    int ln = T[i].first / D, rn = T[i].second / D;
    cout << Dists[surp][rn] - Dists[surp][ln] << '\n';
  }
  return 0;
}

int abs(int a) noexcept { return (a > 0 ? a : -a); }

int mdist(pair<int, int> a, pair<int, int> b) {
  return abs(b.first - a.first) + abs(b.second - a.second);
}


Fixed submission:
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int H, W, D;

int abs(int a) noexcept;
int mdist(pair<int, int> a, pair<int, int> b);

int main() {
  cin >> H >> W >> D;
  vector<pair<int, int>> A(H * W * 2, make_pair(1000, 1000));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int tmp;
      cin >> tmp;
      A[tmp - 1] = make_pair(i, j);
    }
  }
  vector<vector<int>> Dists(D, vector<int>((H * W) / D + 1, 0));
  for (int k = 0; k < D; k++) {
    pair<int, int> prevP = A[k];
    Dists[k][0] = 0;
    for (int i = 1; i < (H * W) / D + 1; i++) {
      pair<int, int> tmpP = A[k + D * i];
      Dists[k][i] = Dists[k][i - 1] + mdist(tmpP, prevP);
      prevP = tmpP;
    }
  }
  int Q;
  cin >> Q;
  vector<pair<int, int>> T(Q); // L, R
  for (int i = 0; i < Q; i++) {
    cin >> T[i].first >> T[i].second;
    T[i].first--;
    T[i].second--;
  }
  for (int i = 0; i < Q; i++) {
    int surp = T[i].first % D;
    int ln = T[i].first / D, rn = T[i].second / D;
    cout << Dists[surp][rn] - Dists[surp][ln] << '\n';
  }
  return 0;
}

int abs(int a) noexcept { return (a > 0 ? a : -a); }

int mdist(pair<int, int> a, pair<int, int> b) {
  return abs(b.first - a.first) + abs(b.second - a.second);
}