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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

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

using namespace std;

typedef long long ll;
typedef pair<int, int> Pii;
typedef vector<int> vint;
const ll INF = LONG_LONG_MAX;
const int MAX_N = 112345;

int main() {
  // insert code here...    return 0;
  //////
  // input from txt
  /*
   std::ifstream in("input.txt");
   std::cin.rdbuf(in.rdbuf());
   std::ofstream out("output.txt");
   std::cout.rdbuf(out.rdbuf());
   ////
   */
  int H, W, D;
  cin >> H >> W >> D;
  vector<Pii> points(H * W + 1);
  repeat(i, H) {
    repeat(j, W) {
      int A;
      cin >> A;
      points[A] = make_pair(j, i);
    }
  }

  vint sum(H * W + 1, 0);
  repeat(i, H * W - D + 1) {
    sum[i + D + 1] = sum[i + 1] +
                     abs(points[i + D + 1].first - points[i + 1].first) +
                     abs(points[i + D + 1].second - points[i + 1].second);
  }
  int Q;
  cin >> Q;
  vint ans(Q, 0);
  repeat(q, Q) {
    int L, R;
    cin >> L >> R;
    ans[q] = sum[R] - sum[L];
  }
  repeat(q, Q) { cout << ans[q] << endl; }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

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

using namespace std;

typedef long long ll;
typedef pair<int, int> Pii;
typedef vector<int> vint;
const ll INF = LONG_LONG_MAX;
const int MAX_N = 112345;

int main() {
  // insert code here...    return 0;
  //////
  // input from txt
  /*
   std::ifstream in("input.txt");
   std::cin.rdbuf(in.rdbuf());
   std::ofstream out("output.txt");
   std::cout.rdbuf(out.rdbuf());
   ////
   */
  int H, W, D;
  cin >> H >> W >> D;
  vector<Pii> points(H * W + 1);
  repeat(i, H) {
    repeat(j, W) {
      int A;
      cin >> A;
      points[A] = make_pair(j, i);
    }
  }

  vint sum(H * W + 10, 0);
  repeat(i, H * W - D + 1) {
    sum[i + D + 1] = sum[i + 1] +
                     abs(points[i + D + 1].first - points[i + 1].first) +
                     abs(points[i + D + 1].second - points[i + 1].second);
  }
  int Q;
  cin >> Q;
  vint ans(Q, 0);
  repeat(q, Q) {
    int L, R;
    cin >> L >> R;
    ans[q] = sum[R] - sum[L];
  }
  repeat(q, Q) { cout << ans[q] << endl; }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
  int H, W, D, Q;
  cin >> H >> W >> D;
  vector<vector<ll>> A(H, vector<ll>(W));
  vector<pair<ll, ll>> ps(H * W + 9300);
  vector<ll> sum(H * W + 9300, 0);
  rep(i, H) {
    rep(j, W) {
      cin >> A[i][j];
      ps[A[i][j]] = {i, j};
    }
  }

  rep(j, H * W) {
    sum[j + D] = sum[j] + abs(ps[j + D].first - ps[j].first) +
                 abs(ps[j + D].second - ps[j].second);
  }

  cin >> Q;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    cout << sum[r] - sum[l] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
  int H, W, D, Q;
  cin >> H >> W >> D;
  vector<vector<ll>> A(H, vector<ll>(W));
  vector<pair<ll, ll>> ps(H * W + 93000);
  vector<ll> sum(H * W + 93000, 0);
  rep(i, H) {
    rep(j, W) {
      cin >> A[i][j];
      ps[A[i][j]] = {i, j};
    }
  }

  rep(j, H * W) {
    sum[j + D] = sum[j] + abs(ps[j + D].first - ps[j].first) +
                 abs(ps[j + D].second - ps[j].second);
  }

  cin >> Q;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    cout << sum[r] - sum[l] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll GCD(ll a, ll b) { return a ? GCD(b % a, a) : b; }
int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<int>> a(h, vector<int>(w));
  map<int, pair<int, int>> m;
  map<pair<int, int>, int> m2;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      a[i][j]--;
      m[a[i][j]] = make_pair(i, j);
    }
  }

  int q;
  cin >> q;
  vector<int> l(q), r(q);
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }
  vector<vector<ll>> sums(d, vector<ll>((h * w / d) + d));
  vector<bool> visit(h * w, false);
  for (int i = 0; i < d; i++) {
    if (visit[i])
      continue;
    for (int j = 0; i + (j + 1) * d < h * w; j++) {
      // cout<<i+(j+1)*d<<" ";
      pair<int, int> now = m[i + j * d];
      pair<int, int> next = m[i + (j + 1) * d];
      sums[i % d][j + 1] = (sums[i % d][j] + (abs(now.first - next.first) +
                                              abs(now.second - next.second)));
    }
  }
  for (int i = 0; i < q; i++) {
    // cout<<sums[r[i]%d][(r[i]/d)]<<endl;
    ll tmp = sums[r[i] % d][(r[i] / d)] - sums[r[i] % d][(l[i] / d)];
    cout << tmp << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll GCD(ll a, ll b) { return a ? GCD(b % a, a) : b; }
int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<int>> a(h, vector<int>(w));
  map<int, pair<int, int>> m;
  map<pair<int, int>, int> m2;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      a[i][j]--;
      m[a[i][j]] = make_pair(i, j);
    }
  }

  int q;
  cin >> q;
  vector<int> l(q), r(q);
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }
  vector<vector<ll>> sums(d, vector<ll>((h * w / d) + 1));
  vector<bool> visit(h * w, false);
  for (int i = 0; i < d; i++) {
    if (visit[i])
      continue;
    for (int j = 0; i + (j + 1) * d < h * w; j++) {
      // cout<<i+(j+1)*d<<" ";
      pair<int, int> now = m[i + j * d];
      pair<int, int> next = m[i + (j + 1) * d];
      sums[i % d][j + 1] = (sums[i % d][j] + (abs(now.first - next.first) +
                                              abs(now.second - next.second)));
    }
  }
  for (int i = 0; i < q; i++) {
    // cout<<sums[r[i]%d][(r[i]/d)]<<endl;
    ll tmp = sums[r[i] % d][(r[i] / d)] - sums[r[i] % d][(l[i] / d)];
    cout << tmp << endl;
  }
}
Problem p03426 (C++)

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

#define CK(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long

const int INF = 1e9;
const long long LLINF = 1e18;

using namespace std;

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};

/*==================================================*/
/*variable*/
int H, W, D, Q;
int A[310][310], L[10101], R[10101];

map<int, pair<ll, ll>> m;
ll dp[90010]; // dp[i][j]: 数値 i から i + D*j へのコスト
/*==================================================*/
/*function*/

/*==================================================*/

int main() {
  cin >> H >> W >> D;
  REP(i, 0, H) {
    REP(j, 0, W) {
      cin >> A[i][j];
      m[A[i][j]] = make_pair((ll)i, (ll)j);
    }
  }
  cin >> Q;
  REP(i, 0, Q) { cin >> L[i] >> R[i]; }

  for (int i = 1; i < H * W + 1 - D; i++) {
    dp[i + D] = abs(m[i].F - m[i + D].F) + abs(m[i].S - m[i + D].S);
  }
  for (int i = D + 1; i < H * W + 1; i++) {
    dp[i] += dp[i - D];
  }

  REP(i, 0, Q) { cout << dp[R[i]] - dp[L[i]] << endl; }

  return 0;
}


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

#define CK(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long

const int INF = 1e9;
const long long LLINF = 1e18;

using namespace std;

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};

/*==================================================*/
/*variable*/
int H, W, D, Q;
int A[310][310], L[101010], R[101010];

map<int, pair<ll, ll>> m;
ll dp[90010]; // dp[i][j]: 数値 i から i + D*j へのコスト
/*==================================================*/
/*function*/

/*==================================================*/

int main() {
  cin >> H >> W >> D;
  REP(i, 0, H) {
    REP(j, 0, W) {
      cin >> A[i][j];
      m[A[i][j]] = make_pair((ll)i, (ll)j);
    }
  }
  cin >> Q;
  REP(i, 0, Q) { cin >> L[i] >> R[i]; }

  for (int i = 1; i < H * W + 1 - D; i++) {
    dp[i + D] = abs(m[i].F - m[i + D].F) + abs(m[i].S - m[i + D].S);
  }
  for (int i = D + 1; i < H * W + 1; i++) {
    dp[i] += dp[i - D];
  }

  REP(i, 0, Q) { cout << dp[R[i]] - dp[L[i]] << endl; }

  return 0;
}
Problem p03426 (C++)

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

using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pi;
typedef pair<pair<ll, ll>, ll> pii;
vector<ll> vec;
vector<vector<ll>> vec2;
ll MOD = 1000000007;
ll INF = 1145141919454519;

int main() {

  ll H, W, D;
  cin >> H >> W >> D;

  map<ll, pi> mp;

  for (ll row = 1; row <= H; row++) {
    for (ll col = 1; col <= W; col++) {
      ll t;
      cin >> t;
      mp[t] = make_pair(row, col);
    }
  }

  // 配るDP的な発想
  vector<ll> dp(H * W + 19, 0);

  // 準備
  for (ll i = 1; i <= H * W; i++) {
    ll next = i + D;
    dp[i + D] = dp[i] + abs(mp[i].first - mp[i + D].first) +
                abs(mp[i].second - mp[i + D].second);
  }

  ll Q;
  cin >> Q;

  vector<ll> ans;
  for (ll i = 0; i < Q; i++) {
    ll a, b;
    cin >> a >> b;
    ans.push_back(dp[b] - dp[a]);
  }

  for (ll i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pi;
typedef pair<pair<ll, ll>, ll> pii;
vector<ll> vec;
vector<vector<ll>> vec2;
ll MOD = 1000000007;
ll INF = 1145141919454519;

int main() {

  ll H, W, D;
  cin >> H >> W >> D;

  map<ll, pi> mp;

  for (ll row = 1; row <= H; row++) {
    for (ll col = 1; col <= W; col++) {
      ll t;
      cin >> t;
      mp[t] = make_pair(row, col);
    }
  }

  // 配るDP的な発想
  vector<ll> dp(H * W + 19, 0);

  // 準備
  for (ll i = 1; i <= H * W; i++) {
    ll next = i + D;
    if (i + D > H * W)
      continue;
    dp[i + D] = dp[i] + abs(mp[i].first - mp[i + D].first) +
                abs(mp[i].second - mp[i + D].second);
  }

  ll Q;
  cin >> Q;

  vector<ll> ans;
  for (ll i = 0; i < Q; i++) {
    ll a, b;
    cin >> a >> b;
    ans.push_back(dp[b] - dp[a]);
  }

  for (ll i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int H, W, D, Q;
  cin >> H >> W >> D;
  int A[H][W];
  map<int, P> M;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      M[A[i][j]].first = i, M[A[i][j]].second = j;
    }
  }
  cin >> Q;
  int L[Q], R[Q];
  ll r[H * W + 1];
  for (int i = 1; i <= D; i++) {
    for (int j = i; j <= H * W; j++) {
      if (j <= D) {
        r[j] = 0;
      } else {
        r[j] = r[j - D] + abs(M[j].first - M[j - D].first) +
               abs(M[j].second - M[j - D].second);
      }
    }
  }
  int ans[Q];
  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    ans[i] = r[R] - r[L];
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int H, W, D, Q;
  cin >> H >> W >> D;
  int A[H][W];
  map<int, P> M;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      M[A[i][j]].first = i, M[A[i][j]].second = j;
    }
  }
  cin >> Q;
  int L[Q], R[Q];
  ll r[H * W + 1];
  for (int i = 1; i <= H * W; i++) {
    if (i <= D) {
      r[i] = 0;
    } else {
      r[i] = r[i - D] + abs(M[i].first - M[i - D].first) +
             abs(M[i].second - M[i - D].second);
    }
  }
  int ans[Q];
  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    ans[i] = r[R] - r[L];
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p03426 (C++)

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

using ll = long long;

int main() {
  int H, W, D;
  std::cin >> H >> W >> D;
  int N = H * W;
  std::vector<int> i_s(N), j_s(N);
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      int a;
      std::cin >> a;
      --a;
      i_s[a] = i;
      j_s[a] = j;
    }
  }
  std::vector<std::vector<ll>> anss(D);
  for (int st = 0; st < D; ++st) {
    ll ans = 0;
    anss[st].push_back(ans);

    int L = st;
    int i = i_s[L], j = j_s[L];
    while (L < N) {
      L += D;
      int new_i = i_s[L], new_j = j_s[L];
      ans += std::abs(new_i - i) + std::abs(new_j - j);
      i = new_i;
      j = new_j;
      anss[st].push_back(ans);
    }
  }

  int Q;
  std::cin >> Q;
  while (Q--) {
    int L, R;
    std::cin >> L >> R;
    --L, --R;
    int st = L % D;
    std::cout << anss[st][(R - st) / D] - anss[st][(L - st) / D] << std::endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using ll = long long;

int main() {
  int H, W, D;
  std::cin >> H >> W >> D;
  int N = H * W;
  std::vector<int> i_s(N), j_s(N);
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      int a;
      std::cin >> a;
      --a;
      i_s[a] = i;
      j_s[a] = j;
    }
  }
  std::vector<std::vector<ll>> anss(D);
  for (int st = 0; st < D; ++st) {
    ll ans = 0;
    anss[st].push_back(ans);

    int L = st;
    int i = i_s[L], j = j_s[L];
    while (L + D < N) {
      L += D;
      int new_i = i_s[L], new_j = j_s[L];
      ans += std::abs(new_i - i) + std::abs(new_j - j);
      i = new_i;
      j = new_j;
      anss[st].push_back(ans);
    }
  }

  int Q;
  std::cin >> Q;
  while (Q--) {
    int L, R;
    std::cin >> L >> R;
    --L, --R;
    int st = L % D;
    std::cout << anss[st][(R - st) / D] - anss[st][(L - st) / D] << std::endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(a) (a).begin(), (a).end()

int H, W, D, Q;

struct Point {
  int x, y;
};

map<int, Point> m;

int main() {
  cin >> H >> W >> D;

  rep(i, H) rep(j, W) {
    int A;
    cin >> A;
    A--;
    m[A] = Point{i, j};
  }

  vector<vector<int>> S(D, vector<int>((H * W) / D + 1, 0));
  rep(r, D) {
    for (int i = 0; r + i * D < H * W; i++) {
      int x = r + i * D;
      S[r][i + 1] =
          S[r][i] + abs(m[x + D].x - m[x].x) + abs(m[x + D].y - m[x].y);
    }
  }

  cin >> Q;
  rep(i, Q) {
    int L, R;
    cin >> L >> R;
    L--, R--;
    int r = L % D;
    cout << S[r][R / D] - S[r][L / D] << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(a) (a).begin(), (a).end()

int H, W, D, Q;

struct Point {
  int x, y;
};

map<int, Point> m;

int main() {
  cin >> H >> W >> D;

  rep(i, H) rep(j, W) {
    int A;
    cin >> A;
    A--;
    m[A] = Point{i, j};
  }

  vector<vector<int>> S(D, vector<int>((H * W) / D + 1, 0));
  rep(r, D) {
    for (int i = 0; r + (i + 1) * D < H * W; i++) {
      int x = r + i * D;
      S[r][i + 1] =
          S[r][i] + abs(m[x + D].x - m[x].x) + abs(m[x + D].y - m[x].y);
    }
  }

  cin >> Q;
  rep(i, Q) {
    int L, R;
    cin >> L >> R;
    L--, R--;
    int r = L % D;
    cout << S[r][R / D] - S[r][L / D] << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

int s[90001];

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

  int x[301], y[301];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int a;
      cin >> a;

      x[a] = j;
      y[a] = i;
    }
  }

  for (int i = d + 1; i <= h * w; i++) {
    s[i] = s[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 l, r;
    cin >> l >> r;
    cout << s[r] - s[l] << endl;
  }

  return 0;
}

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

int s[90001];

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

  int x[90001], y[90001];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int a;
      cin >> a;

      x[a] = j;
      y[a] = i;
    }
  }

  for (int i = d + 1; i <= h * w; i++) {
    s[i] = s[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 l, r;
    cin >> l >> r;
    cout << s[r] - s[l] << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

inline int dis(pair<int, int> x, pair<int, int> y) {
  return abs(x.first - y.first) + abs(x.second - y.second);
}

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;
  vector<pair<int, int>> ra(h * w);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int a;
      cin >> a;
      a--;
      ra[a] = pair<int, int>(i, j);
    }

  vector<vector<int>> tran(h * w, vector<int>(20));

  for (int x = 0; x + d < h * w; x++) {
    tran[x][0] = dis(ra[x], ra[x + d]);
  }
  for (int i = 1; i < 20; i++) {
    for (int x = 0; x + d * (1 << i) < h * w; x++) {
      tran[x][i] = tran[x][i - 1] + tran[x + d * (1 << i - 1)][i - 1];
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    long res = 0;
    int n = (r - l) / d;
    for (int i = 0; (1 << i) <= n; i++) {
      if (!((1 << i) & n))
        continue;
      res += tran[l][i];
      l += d * (1 << i);
    }
    cout << res << endl;
  }
}


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

inline int dis(pair<int, int> x, pair<int, int> y) {
  return abs(x.first - y.first) + abs(x.second - y.second);
}

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;
  vector<pair<int, int>> ra(h * w);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int a;
      cin >> a;
      a--;
      ra[a] = pair<int, int>(i, j);
    }

  vector<vector<int>> tran(h * w, vector<int>(20));

  for (int x = 0; x + d < h * w; x++) {
    tran[x][0] = dis(ra[x], ra[x + d]);
  }
  for (int i = 1; d * (1 << i - 1) < h * w; i++) {
    for (int x = 0; x + d * (1 << i - 1) < h * w; x++) {
      tran[x][i] = tran[x][i - 1] + tran[x + d * (1 << i - 1)][i - 1];
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    long res = 0;
    int n = (r - l) / d;
    for (int i = 0; (1 << i) <= n; i++) {
      if (!((1 << i) & n))
        continue;
      res += tran[l][i];
      l += d * (1 << i);
    }
    cout << res << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= 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;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
ll H, W, D;
ll A[400][400];
ll Q;
ll L[101010];
ll R[101010];

using Pii = pair<int, int>;
map<int, Pii> mp;

ll sum[90101];

int main() {
  cin >> H >> W >> D;
  repe(i, 1, H) {
    repe(j, 1, W) {
      cin >> A[i][j];
      mp[A[i][j]] = make_pair(i, j);
    }
  }
  cin >> Q;
  rep(i, 0, Q) { cin >> L[i] >> R[i]; }

  for (int i = 1; i <= D; ++i) {
    ll cur_num = i;
    while (cur_num <= H * W) {
      auto cur = mp[cur_num];
      cur_num += D;
      auto next = mp[cur_num];
      sum[cur_num] = sum[cur_num - D] + abs(cur.first - next.first) +
                     abs(cur.second - next.second);
    }
  }

  rep(i, 0, Q) {
    ll l = L[i];
    ll r = R[i];

    ll cur_num = l;
    ll ans = sum[r] - sum[l];

    cout << ans << "\n";
  }
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= 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;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
ll H, W, D;
ll A[400][400];
ll Q;
ll L[101010];
ll R[101010];

using Pii = pair<int, int>;
map<int, Pii> mp;

ll sum[201010];

int main() {
  cin >> H >> W >> D;
  repe(i, 1, H) {
    repe(j, 1, W) {
      cin >> A[i][j];
      mp[A[i][j]] = make_pair(i, j);
    }
  }
  cin >> Q;
  rep(i, 0, Q) { cin >> L[i] >> R[i]; }

  for (int i = 1; i <= D; ++i) {
    ll cur_num = i;
    while (cur_num <= H * W) {
      auto cur = mp[cur_num];
      cur_num += D;
      auto next = mp[cur_num];
      sum[cur_num] = sum[cur_num - D] + abs(cur.first - next.first) +
                     abs(cur.second - next.second);
    }
  }

  rep(i, 0, Q) {
    ll l = L[i];
    ll r = R[i];

    ll cur_num = l;
    ll ans = sum[r] - sum[l];

    cout << ans << "\n";
  }
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll MOD = 1e9 + 7;
// LLONG_MIN

int main(void) {
  ll H, W, D, X[90010], Y[90010], next[90010] = {}, A, Q, L[100010], R[100010],
                                  ans, r;
  scanf("%lld%lld%lld", &H, &W, &D);
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      scanf("%lld", &A);
      Y[A] = i;
      X[A] = j;
    }
  }
  scanf("%lld", &Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%lld%lld", &L[i], &R[i]);
  }
  for (int i = 1; i <= H * W - D; ++i)
    next[i] = abs(X[i + D] - X[i]) + abs(Y[i + D] - Y[i]);
  for (int i = 0; i < Q; ++i) {
    ans = 0;
    r = (R[i] - L[i]) / D;
    for (int j = 0; j < r; ++j) {
      ans += next[L[i] + j * D];
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll MOD = 1e9 + 7;
// LLONG_MIN

int main(void) {
  ll H, W, D, X[90010], Y[90010], next[90010] = {}, A, Q, L[100010], R[100010],
                                  ans, r;
  scanf("%lld%lld%lld", &H, &W, &D);
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      scanf("%lld", &A);
      Y[A] = i;
      X[A] = j;
    }
  }
  scanf("%lld", &Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%lld%lld", &L[i], &R[i]);
  }
  for (int i = 1; i <= H * W - D; ++i)
    next[i + D] = abs(X[i + D] - X[i]) + abs(Y[i + D] - Y[i]) + next[i];
  for (int i = 0; i < Q; ++i)
    printf("%lld\n", next[R[i]] - next[L[i]]);
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

long long MOD = 1000000007;

struct Point {
  int x;
  int y;
};

int main() {

  int H, W, D;
  cin >> H >> W >> D;
  vector<Point> V(H * W + 1);
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      int a;
      cin >> a;
      Point p;
      p.x = x;
      p.y = y;
      V[a] = p;
    }
  }

  vector<int> S(H * W + 1, 0);
  for (int i = 1; i <= H * W; i++) {
    for (int j = 1; i + j * D <= H * W; j++) {
      S[i + j * D] = S[i + (j - 1) * D] +
                     abs(V[i + j * D].x - V[i + (j - 1) * D].x) +
                     abs(V[i + j * D].y - V[i + (j - 1) * D].y);
    }
  }

  vector<long long> A;
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    A.push_back(S[r] - S[l]);
  }
  for (int i = 0; i < Q; i++) {
    cout << A[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

long long MOD = 1000000007;

struct Point {
  int x;
  int y;
};

int main() {

  int H, W, D;
  cin >> H >> W >> D;
  vector<Point> V(H * W + 1);
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      int a;
      cin >> a;
      Point p;
      p.x = x;
      p.y = y;
      V[a] = p;
    }
  }

  vector<int> S(H * W + 1, 0);
  for (int i = 1; i <= D; i++) {
    for (int j = 1; i + j * D <= H * W; j++) {
      S[i + j * D] = S[i + (j - 1) * D] +
                     abs(V[i + j * D].x - V[i + (j - 1) * D].x) +
                     abs(V[i + j * D].y - V[i + (j - 1) * D].y);
    }
  }

  vector<long long> A;
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    A.push_back(S[r] - S[l]);
  }
  for (int i = 0; i < Q; i++) {
    cout << A[i] << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

using namespace std;

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

  vector<pair<int, int>> locate(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int input;
      scanf("%d", &input);
      input--;
      locate[input].first = i;
      locate[input].second = j;
    }
  }

  int q;
  cin >> q;

  vector<int> l(q);
  vector<int> r(q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &l[i], &r[i]);
    l[i]--;
    r[i]--;
  }

  vector<int> mp_sum(h * w);
  for (int i = 0; i < d; i++) {
    mp_sum[i] = 0;
    for (int j = i; j < h * w; j = j + d) {
      int mp = abs(locate[j].first - locate[j + d].first) +
               abs(locate[j].second - locate[j + d].second);
      mp_sum[j + d] = mp_sum[j] + mp;
    }
  }

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

  return 0;
}

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

using namespace std;

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

  vector<pair<int, int>> locate(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int input;
      scanf("%d", &input);
      input--;
      locate[input].first = i;
      locate[input].second = j;
    }
  }

  int q;
  cin >> q;

  vector<int> l(q);
  vector<int> r(q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &l[i], &r[i]);
    l[i]--;
    r[i]--;
  }

  vector<int> mp_sum(h * w);
  for (int i = 0; i < d; i++) {
    mp_sum[i] = 0;
    for (int j = i; j + d < h * w; j = j + d) {
      int mp = abs(locate[j].first - locate[j + d].first) +
               abs(locate[j].second - locate[j + d].second);
      mp_sum[j + d] = mp_sum[j] + mp;
    }
  }

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

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

int H, W, D;
int a[310][310];
int b[90010], c[90010];
int d[90010];
int Q;
int l[100010], r[100010];

int main() {

  cin >> H >> W >> D;

  for (int i = 0; i < H; i++) {
    for (int j = 0; i < W; j++)
      cin >> a[i][j];
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> l[i] >> r[i];
  }
  // ランダム配列の場合

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      b[a[i][j]] = i, c[a[i][j]] = j;
    }
  }

  for (int i = 1; i < 90000; i++) {
    d[i + D] = d[i] + abs(b[i + D] - b[i]) + abs(c[i + D] - c[i]);
  }

  for (int i = 0; i < Q; i++) {
    cout << d[r[i]] - d[l[i]] << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

int H, W, D;
int a[310][310];
int b[90010], c[90010];
int d[90010];
int Q;
int l[100010], r[100010];

int main() {

  cin >> H >> W >> D;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cin >> a[i][j];
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> l[i] >> r[i];
  }
  // ランダム配列の場合

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      b[a[i][j]] = i, c[a[i][j]] = j;
    }
  }

  for (int i = 1; i < 90000; i++) {
    d[i + D] = d[i] + abs(b[i + D] - b[i]) + abs(c[i + D] - c[i]);
  }

  for (int i = 0; i < Q; i++) {
    cout << d[r[i]] - d[l[i]] << endl;
  }
}
Problem p03426 (Python)

Buggy submission (status: Runtime Error):
H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]

d = {}
for y in range(H):
    Ay = A[y]
    for x in range(W):
        d[Ay[x]] = (y, x)

t = [[0] * ((H * W + D - 1) // D) for _ in range(D)]
for i in range(D):
    ti = t[i]
    j = 0
    if i == 0:
        j = 1
    while i + (j + 1) * D <= H * W:
        y1, x1 = d[i + j * D]
        y2, x2 = d[i + (j + 1) * D]
        ti[j + 1] = abs(y1 - y2) + abs(x1 - x2)
        j += 1
    for j in range(len(ti) - 1):
        ti[j + 1] += ti[j]

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    i = L % D
    print(t[i][(R - i) // D] - t[i][(L - i) // D])


Fixed submission:
H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]

d = {}
for y in range(H):
    Ay = A[y]
    for x in range(W):
        d[Ay[x]] = (y, x)

t = [[0] * ((H * W + D - 1) // D + 1) for _ in range(D)]
for i in range(D):
    ti = t[i]
    j = 0
    if i == 0:
        j = 1
    while i + (j + 1) * D <= H * W:
        y1, x1 = d[i + j * D]
        y2, x2 = d[i + (j + 1) * D]
        ti[j + 1] = abs(y1 - y2) + abs(x1 - x2)
        j += 1
    for j in range(len(ti) - 1):
        ti[j + 1] += ti[j]

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    i = L % D
    print(t[i][(R - i) // D] - t[i][(L - i) // D])
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1e9;
int h, w, d;
vector<vector<int>> c(300, vector<int>(300, inf));
map<int, pair<int, int>> p;

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      p[c[i][j]] = {i, j};
    }
  }
}

int main() {
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  init();
  vector<int> dist(301, 0);
  for (int i = 1; i + d <= h * w; i++) {
    dist[i + d] = dist[i] + abs(p[i].first - p[i + d].first) +
                  abs(p[i].second - p[i + d].second);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    int sum = 0;
    sum = dist[r] - dist[l];
    cout << sum << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1e9;
int h, w, d;
vector<vector<int>> c(300, vector<int>(300, inf));
map<int, pair<int, int>> p;

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      p[c[i][j]] = {i, j};
    }
  }
}

int main() {
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  init();
  vector<int> dist(h * w + 1, 0);
  for (int i = 1; i + d <= h * w; i++) {
    dist[i + d] = dist[i] + abs(p[i].first - p[i + d].first) +
                  abs(p[i].second - p[i + d].second);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    int sum = 0;
    sum = dist[r] - dist[l];
    cout << sum << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

const int N = 300 + 10;

typedef long long LL;
int h, w, a[N][N], d;
int f[N][20];

struct Node {
  int r, c;
};

Node node[N * N];

int dist(int i, int j) {
  return abs(node[i].r - node[j].r) + abs(node[i].c - node[j].c);
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      node[a[i][j]].r = i;
      node[a[i][j]].c = j;
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int cur = a[i][j];
      f[cur][0] = -1;
      if (cur + d <= w * h)
        f[cur][0] = dist(cur, cur + d);
      // cout << a[i][j] << ' ' << f[cur][0] << endl;
    }

  for (int pw = 1; pw <= 17; pw++)
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        int cur = a[i][j];
        f[cur][pw] = -1;
        if ((1ll << pw) * d + cur <= w * h) {
          int rhs = (1 << (pw - 1)) * d + cur;
          f[cur][pw] = f[cur][pw - 1] + f[rhs][pw - 1];
        }
      }

  int Q;
  cin >> Q;
  while (Q--) {
    int l, r;
    cin >> l >> r;
    int cnt = (r - l) / d, ans = 0, t = l;
    for (int pw = 17; pw >= 0; pw--)
      if ((1 << pw) <= cnt) {
        ans += f[t][pw];
        t += (1 << pw) * d;
        cnt -= (1 << pw);
      }
    cout << ans << '\n';
  }
  return 0;
}

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

const int N = 300 + 10;

typedef long long LL;
int h, w, a[N][N], d;
int f[N * N][20];

struct Node {
  int r, c;
};

Node node[N * N];

int dist(int i, int j) {
  return abs(node[i].r - node[j].r) + abs(node[i].c - node[j].c);
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      node[a[i][j]].r = i;
      node[a[i][j]].c = j;
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int cur = a[i][j];
      f[cur][0] = -1;
      if (cur + d <= w * h)
        f[cur][0] = dist(cur, cur + d);
      // cout << a[i][j] << ' ' << f[cur][0] << endl;
    }

  for (int pw = 1; pw <= 17; pw++)
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        int cur = a[i][j];
        f[cur][pw] = -1;
        if ((1ll << pw) * d + cur <= w * h) {
          int rhs = (1 << (pw - 1)) * d + cur;
          f[cur][pw] = f[cur][pw - 1] + f[rhs][pw - 1];
        }
      }

  int Q;
  cin >> Q;
  while (Q--) {
    int l, r;
    cin >> l >> r;
    int cnt = (r - l) / d, ans = 0, t = l;
    for (int pw = 17; pw >= 0; pw--)
      if ((1 << pw) <= cnt) {
        ans += f[t][pw];
        t += (1 << pw) * d;
        cnt -= (1 << pw);
      }
    cout << ans << '\n';
  }
  return 0;
}
Problem p03426 (C++)

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

typedef long long ll;
const int maxn = 310;
int px[maxn], py[maxn], d[maxn * maxn];

int main() {
  int h, w, D;
  cin >> h >> w >> D;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      int a;
      cin >> a;
      px[a] = i;
      py[a] = j;
    }
  for (int i = D + 1; i <= h * w; i++)
    d[i] = d[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D]);
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << abs(d[r] - d[l]) << endl;
  }
  return 0;
}


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

typedef long long ll;
const int maxn = 310;
int px[maxn * maxn], py[maxn * maxn], d[maxn * maxn];

int main() {
  int h, w, D;
  cin >> h >> w >> D;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      int a;
      cin >> a;
      px[a] = i;
      py[a] = j;
    }
  for (int i = D + 1; i <= h * w; i++)
    d[i] = d[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D]);
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << abs(d[r] - d[l]) << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int H, W, D;
  cin >> H >> W >> D;

  int Y[300 * 300 + 1];
  int X[300 * 300 + 1];
  int tmp;

  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      cin >> tmp;
      Y[tmp] = h + 1;
      X[tmp] = w + 1;
    }
  }

  int Q, L, R;
  cin >> Q;

  int C[300 * 300 + 1] = {};

  for (int q = 0; q < Q; q++) {
    cin >> L >> R;
    if (L == R)
      cout << 0 << endl;
    else {
      if (C[R] == 0) {
        int A = R % D;
        if (A == 0)
          A = D;
        int B;
        int cost = 0;
        while (A < W * H) {
          B = A + D;
          cost += abs(Y[B] - Y[A]) + abs(X[B] - X[A]);
          A = B;
          C[A] = cost;
        }
      }
      cout << C[R] - C[L] << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int H, W, D;
  cin >> H >> W >> D;

  int Y[300 * 300 + 1];
  int X[300 * 300 + 1];
  int tmp;

  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      cin >> tmp;
      Y[tmp] = h + 1;
      X[tmp] = w + 1;
    }
  }

  int Q, L, R;
  cin >> Q;

  int C[300 * 300 + 1] = {};

  for (int q = 0; q < Q; q++) {
    cin >> L >> R;
    if (L == R)
      cout << 0 << endl;
    else {
      if (C[R] == 0) {
        int A = R % D;
        if (A == 0)
          A = D;
        int B;
        int cost = 0;
        while (A + D <= W * H) {
          B = A + D;
          cost += abs(Y[B] - Y[A]) + abs(X[B] - X[A]);
          A = B;
          C[A] = cost;
        }
      }
      cout << C[R] - C[L] << endl;
    }
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;
const int MAXH = 301;
const int MAXW = 301;
const int MAXQ = 1e5 + 10;

int a[MAXH][MAXW];
P field[MAXH * MAXW];
bool visited[MAXH * MAXW];
vector<int> cost[MAXH * MAXW];
int l[MAXQ], r[MAXQ];

int h, w, d, q;
void solve() {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      field[a[y][x] - 1] = P(x, y);
    }
  }

  for (int i = 0; i <= w * h; i++) {
    if (!visited[i]) {
      cost[i][0] = 0;
      for (int j = 1; i + j * d <= w * h; j++) {
        visited[i + j * d] = true;
        P &p1 = field[i + (j - 1) * d];
        P &p2 = field[i + j * d];
        cost[i].push_back(cost[i][j - 1] + abs(p1.first - p2.first) +
                          abs(p1.second - p2.second));
      }
    }
  }

  for (int i = 0; i < q; i++) {
    l[i]--;
    r[i]--;
    int start = l[i] % d;
    cout << (cost[start][(r[i] - start) / d] - cost[start][(l[i] - start) / d])
         << endl;
  }
}

int main() {
  cin >> h >> w >> d;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      cin >> a[y][x];
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }
  solve();
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;
const int MAXH = 301;
const int MAXW = 301;
const int MAXQ = 1e5 + 10;

int a[MAXH][MAXW];
P field[MAXH * MAXW];
bool visited[MAXH * MAXW];
vector<int> cost[MAXH * MAXW];
int l[MAXQ], r[MAXQ];

int h, w, d, q;
void solve() {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      field[a[y][x] - 1] = P(x, y);
    }
  }

  for (int i = 0; i <= w * h; i++) {
    if (!visited[i]) {
      cost[i].push_back(0);
      for (int j = 1; i + j * d <= w * h; j++) {
        visited[i + j * d] = true;
        P &p1 = field[i + (j - 1) * d];
        P &p2 = field[i + j * d];
        cost[i].push_back(cost[i][j - 1] + abs(p1.first - p2.first) +
                          abs(p1.second - p2.second));
      }
    }
  }

  for (int i = 0; i < q; i++) {
    l[i]--;
    r[i]--;
    int start = l[i] % d;
    cout << (cost[start][(r[i] - start) / d] - cost[start][(l[i] - start) / d])
         << endl;
  }
}

int main() {
  cin >> h >> w >> d;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      cin >> a[y][x];
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }
  solve();
}
Problem p03426 (C++)

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

int main() {
  int h, w, d, a;
  std::cin >> h >> w >> d;
  std::vector<std::pair<int, int>> xys(h * w + 1);
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; ++x) {
      std::cin >> a;
      xys[a] = {x, y};
    }
  }
  std::vector<int> total(h * w + 1);
  for (int di = 0; di < d; ++di) {
    total[di] = 0;
    for (int k = 1; k * d <= h * w; ++k) {
      int cur = di + k * d;
      int prev = cur - d;
      int power = std::abs(xys[cur].first - xys[prev].first) +
                  std::abs(xys[cur].second - xys[prev].second);
      total[cur] = total[prev] + power;
    }
  }
  int q, l, r;
  std::cin >> q;
  for (int qi = 0; qi < q; ++qi) {
    std::cin >> l >> r;
    std::cout << total[r] - total[l] << std::endl;
  }
  return 0;
}

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

int main() {
  int h, w, d, a;
  std::cin >> h >> w >> d;
  std::vector<std::pair<int, int>> xys(h * w + 1);
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; ++x) {
      std::cin >> a;
      xys[a] = {x, y};
    }
  }
  std::vector<int> total(h * w + 1);
  for (int di = 0; di < d; ++di) {
    total[di] = 0;
    for (int k = 1; di + k * d <= h * w; ++k) {
      int cur = di + k * d;
      int prev = cur - d;
      int power = std::abs(xys[cur].first - xys[prev].first) +
                  std::abs(xys[cur].second - xys[prev].second);
      total[cur] = total[prev] + power;
    }
  }
  int q, l, r;
  std::cin >> q;
  for (int qi = 0; qi < q; ++qi) {
    std::cin >> l >> r;
    std::cout << total[r] - total[l] << std::endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << endl
#define all(x) begin(x), end(x)
#define _ << ' ' <<
using namespace std;
using ll = long long;

int a[300][300];
pair<int, int> f[300 * 300 + 1];

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

  vector<vector<int>> rsq(d);
  for (int i = 1; i <= h * w; ++i) {
    if (i + d > h * w)
      continue;
    int p, q, r, s;
    tie(p, q) = f[i];
    tie(r, s) = f[i + d];
    rsq[i % d].push_back(abs(p - r) + abs(q - s));
  }
  for (vector<int> &v : rsq)
    for (int i = 1; i < v.size(); ++i)
      v[i] += v[i - 1];

  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    int step = (r - l) / d;
    auto sum = [](vector<int> &v, int l, int r) {
      int ret = v[r];
      if (l)
        ret -= v[l - 1];
      return ret;
    };
    cout << sum(rsq[l % d], (l - 1) / d, (l - 1) / d + step - 1) << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << endl
#define all(x) begin(x), end(x)
#define _ << ' ' <<
using namespace std;
using ll = long long;

int a[300][300];
pair<int, int> f[300 * 300 + 1];

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

  vector<vector<int>> rsq(d);
  for (int i = 1; i <= h * w; ++i) {
    if (i + d > h * w)
      continue;
    int p, q, r, s;
    tie(p, q) = f[i];
    tie(r, s) = f[i + d];
    rsq[i % d].push_back(abs(p - r) + abs(q - s));
  }
  for (vector<int> &v : rsq)
    for (int i = 1; i < v.size(); ++i)
      v[i] += v[i - 1];

  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    int step = (r - l) / d;
    auto sum = [](vector<int> &v, int l, int r) {
      int ret = v[r];
      if (l)
        ret -= v[l - 1];
      return ret;
    };
    cout << (step ? sum(rsq[l % d], (l - 1) / d, (l - 1) / d + step - 1) : 0)
         << '\n';
  }
}