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

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

int main() {
  int N;
  cin >> N;

  string S;
  vector<long long> ch_num(5);
  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S.at(0) == 'M')
      ch_num.at(0)++;
    else if (S.at(0) == 'A')
      ch_num.at(1)++;
    else if (S.at(0) == 'R')
      ch_num.at(2)++;
    else if (S.at(0) == 'C')
      ch_num.at(3)++;
    else if (S.at(0) == 'H')
      ch_num.at(4)++;
  }

  long long ans = 0;
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      for (int k = j + 1; k < N; k++) {
        ans += ch_num.at(i) * ch_num.at(j) * ch_num.at(k);
      }
    }
  }

  cout << ans << endl;
}


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

int main() {
  int N;
  cin >> N;

  string S;
  vector<long long> ch_num(5);
  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S.at(0) == 'M')
      ch_num.at(0)++;
    else if (S.at(0) == 'A')
      ch_num.at(1)++;
    else if (S.at(0) == 'R')
      ch_num.at(2)++;
    else if (S.at(0) == 'C')
      ch_num.at(3)++;
    else if (S.at(0) == 'H')
      ch_num.at(4)++;
  }

  long long ans = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = i + 1; j < 4; j++) {
      for (int k = j + 1; k < 5; k++) {
        ans += ch_num.at(i) * ch_num.at(j) * ch_num.at(k);
      }
    }
  }

  cout << ans << endl;
}
Problem p03425 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  long long A[5] = {0, 0, 0, 0, 0};
  cin >> N;
  vector<string> S(N);
  for (size_t i = 0; i < N; i++) {
    cin >> S.at(i);
    if (S.at(i).at(0) == 'M') {
      A[0]++;
    } else if (S.at(i).at(0) == 'A') {
      A[1]++;
    } else if (S.at(i).at(0) == 'R') {
      A[2]++;
    } else if (S.at(i).at(0) == 'C') {
      A[3]++;
    } else if (S.at(i).at(0) == 'H') {
      A[4]++;
    }
  }
  long long ans = 0;
  for (size_t i = 0; i < N; i++) {
    for (size_t j = i + 1; j < N; j++) {
      for (size_t k = j + 1; k < N; k++) {
        ans += (A[i]) * A[j] * A[k];
      }
    }
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  long long A[5] = {0, 0, 0, 0, 0};
  cin >> N;
  vector<string> S(N);
  for (size_t i = 0; i < N; i++) {
    cin >> S.at(i);
    if (S.at(i).at(0) == 'M') {
      A[0]++;
    } else if (S.at(i).at(0) == 'A') {
      A[1]++;
    } else if (S.at(i).at(0) == 'R') {
      A[2]++;
    } else if (S.at(i).at(0) == 'C') {
      A[3]++;
    } else if (S.at(i).at(0) == 'H') {
      A[4]++;
    }
  }
  long long ans = 0;
  for (size_t i = 0; i < 5; i++) {
    for (size_t j = i + 1; j < 5; j++) {
      for (size_t k = j + 1; k < 5; k++) {
        ans += A[i] * A[j] * A[k];
      }
    }
  }

  cout << ans;
}
Problem p03425 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 ll INF = 1LL << 60;

int main() {
  vector<ll> F(5, 0);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    if (s.front() == 'M')
      F[0]++;
    if (s.front() == 'A')
      F[1]++;
    if (s.front() == 'R')
      F[2]++;
    if (s.front() == 'C')
      F[3]++;
    if (s.front() == 'H')
      F[4]++;
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      for (int k = j + 1; k < N; k++) {
        ans += F[i] * F[j] * F[k];
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 ll INF = 1LL << 60;

int main() {
  vector<ll> F(5, 0);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    if (s.front() == 'M')
      F[0]++;
    if (s.front() == 'A')
      F[1]++;
    if (s.front() == 'R')
      F[2]++;
    if (s.front() == 'C')
      F[3]++;
    if (s.front() == 'H')
      F[4]++;
  }

  ll ans = 0;
  for (int i = 0; i < 5; i++) {
    for (int j = i + 1; j < 5; j++) {
      for (int k = j + 1; k < 5; k++) {
        ans += F[i] * F[j] * F[k];
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03426 (C++)

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

#define ABS(x) (((x) > 0) ? (x) : -(x))

using namespace std;

int main(int argc, char *argv[]) {
  int H, W, D;
  cin >> H >> W >> D;

  int xs[90001];
  int ys[90001];

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      int A;
      cin >> A;
      xs[A] = x;
      ys[A] = y;
    }
  }

  int ds[90001] = {};

  for (int a = 1; a < H * W; a++) {
    int dx = xs[a + D] - xs[a];
    int dy = ys[a + D] - ys[a];
    ds[a + D] = ABS(dx) + ABS(dy) + ds[a];
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    cout << (ds[R] - ds[L]) << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>

#define ABS(x) (((x) > 0) ? (x) : -(x))

using namespace std;

int main(int argc, char *argv[]) {
  int H, W, D;
  cin >> H >> W >> D;

  int xs[90001];
  int ys[90001];

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      int A;
      cin >> A;
      xs[A] = x;
      ys[A] = y;
    }
  }

  int ds[90001] = {};

  for (int a = 1; a <= H * W - D; a++) {
    int dx = xs[a + D] - xs[a];
    int dy = ys[a + D] - ys[a];
    ds[a + D] = ABS(dx) + ABS(dy) + ds[a];
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    cout << (ds[R] - ds[L]) << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;
  vector<pair<int, int>> v(h * w);
  rep(i, h) rep(j, w) {
    int a;
    cin >> a;
    v[a] = make_pair(i, j);
  }
  int q;
  cin >> q;
  vector<int> dist(h * w + 1);
  for (int i = d + 1; i <= h * w; i++) {
    dist[i] = dist[i - d] + abs(v[i].first - v[i - d].first) +
              abs(v[i].second - v[i - d].second);
  }
  vector<pair<int, int>> Q(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    Q[i] = make_pair(l, r);
  }
  rep(i, q) {
    int ans = dist[Q[i].second] - dist[Q[i].first];
    cout << ans << endl;
  }
  return 0;
}

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

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;
  vector<pair<int, int>> v(h * w + 1);
  rep(i, h) rep(j, w) {
    int a;
    cin >> a;
    v[a] = make_pair(i, j);
  }
  int q;
  cin >> q;
  vector<int> dist(h * w + 1);
  for (int i = d + 1; i <= h * w; i++) {
    dist[i] = dist[i - d] + abs(v[i].first - v[i - d].first) +
              abs(v[i].second - v[i - d].second);
  }
  vector<pair<int, int>> Q(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    Q[i] = make_pair(l, r);
  }
  rep(i, q) {
    int ans = dist[Q[i].second] - dist[Q[i].first];
    cout << ans << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
struct NODE {
  int x;
  int y;
} flag[100005];

int a[305][305], c[100005];

int main() {
  int h, w, d, q;
  scanf("%d %d %d", &h, &w, &d);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      scanf("%d", &a[i][j]);
      flag[a[i][j]].x = i;
      flag[a[i][j]].y = j;
    }
  int pp = h * w;
  int tt = pp / d;
  for (int i = 1; i <= tt; i++) {
    int qwe = i;
    while (qwe <= pp) {
      qwe += d;
      if (qwe > pp)
        break;
      c[qwe] = abs(flag[qwe].x - flag[qwe - d].x) +
               abs(flag[qwe].y - flag[qwe - d].y) + c[qwe - d];
    }
  }
  for (int i = tt * d + 1; i <= pp; i++) {
    c[i] = c[i - d] + abs(flag[i].x - flag[i - d].x) +
           abs(flag[i].y - flag[i - d].y);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    int p = c[r] - c[l];
    printf("%d\n", p);
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
struct NODE {
  int x;
  int y;
} flag[100005];

int a[305][305], c[100005];

int main() {
  int h, w, d, q;
  scanf("%d %d %d", &h, &w, &d);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      scanf("%d", &a[i][j]);
      flag[a[i][j]].x = i;
      flag[a[i][j]].y = j;
    }
  int pp = h * w;
  int tt = pp / d;
  for (int i = 1; i <= d; i++) {
    int qwe = i;
    while (qwe <= pp) {
      qwe += d;
      if (qwe > pp)
        break;
      c[qwe] = abs(flag[qwe].x - flag[qwe - d].x) +
               abs(flag[qwe].y - flag[qwe - d].y) + c[qwe - d];
    }
  }
  for (int i = tt * d + 1; i <= pp; i++) {
    c[i] = c[i - d] + abs(flag[i].x - flag[i - d].x) +
           abs(flag[i].y - flag[i - d].y);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    int p = c[r] - c[l];
    printf("%d\n", p);
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//----------------------------------------------------------------------

#define MAX 110000

ll dp[MAX];

int main(void) {
  pll a[MAX];
  ll h, w, d, L, R, temp2;
  cin >> h >> w >> d;

  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      cin >> temp;
      a[temp].first = i;
      a[temp].second = j;
    }
  }

  cin >> n;

  FOR(i, 1, d + 1) {
    temp = i;
    dp[temp] = 0;
    temp2 = temp + d;
    while (temp < h * w) {
      dp[temp2] = llabs(a[temp2].first - a[temp].first) +
                  llabs(a[temp2].second - a[temp].second) + dp[temp];
      temp = temp2;
      temp2 += d;
    }
  }

  FOR(i, 0, n) {
    cin >> L;
    cin >> R;
    ans = dp[R] - dp[L];
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//----------------------------------------------------------------------

#define MAX 210000

ll dp[MAX];

int main(void) {
  pll a[MAX];
  ll h, w, d, L, R, temp2;
  cin >> h >> w >> d;

  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      cin >> temp;
      a[temp].first = i;
      a[temp].second = j;
    }
  }

  cin >> n;

  FOR(i, 1, d + 1) {
    temp = i;
    dp[temp] = 0;
    temp2 = temp + d;
    while (temp < h * w) {
      dp[temp2] = llabs(a[temp2].first - a[temp].first) +
                  llabs(a[temp2].second - a[temp].second) + dp[temp];
      temp = temp2;
      temp2 += d;
    }
  }

  FOR(i, 0, n) {
    cin >> L;
    cin >> R;
    ans = dp[R] - dp[L];
    cout << ans << endl;
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#pragma GCC optimize "-O3"
#pragma GCC target "tune=native"
using namespace std;
const int maxn = 333;
const int N = maxn * maxn;
vector<tuple<int, int, int>> p[maxn];
vector<int> sum[maxn];
int main() {
  int w, h, d;
  cin >> w >> h >> d;
  for (int e = 0; e < w; e++)
    for (int f = 0; f < h; f++) {
      int v;
      cin >> v;
      p[v % d].push_back(make_tuple(v, e, f));
    }
  for (int e = 0; e < d; e++) {
    sort(p[e].begin(), p[e].end());
    sum[e].assign(p[e].size(), 0);
    for (int f = 1; f < p[e].size(); f++)
      sum[e][f] = sum[e][f - 1] + abs(get<1>(p[e][f - 1]) - get<1>(p[e][f])) +
                  abs(get<2>(p[e][f - 1]) - get<2>(p[e][f]));
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    int pp = l % d;
    int pl = lower_bound(p[pp].begin(), p[pp].end(), make_tuple(l, -1, -1)) -
             p[pp].begin();
    int pr = lower_bound(p[pp].begin(), p[pp].end(), make_tuple(r, -1, -1)) -
             p[pp].begin();
    cout << sum[pp][pr] - sum[pp][pl] << endl;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#pragma GCC optimize "-O3"
#pragma GCC target "tune=native"
using namespace std;
const int maxn = 333;
const int N = maxn * maxn;
vector<tuple<int, int, int>> p[N];
vector<int> sum[N];
int main() {
  int w, h, d;
  cin >> w >> h >> d;
  for (int e = 0; e < w; e++)
    for (int f = 0; f < h; f++) {
      int v;
      cin >> v;
      p[v % d].push_back(make_tuple(v, e, f));
    }
  for (int e = 0; e < d; e++) {
    sort(p[e].begin(), p[e].end());
    sum[e].assign(p[e].size(), 0);
    for (int f = 1; f < p[e].size(); f++)
      sum[e][f] = sum[e][f - 1] + abs(get<1>(p[e][f - 1]) - get<1>(p[e][f])) +
                  abs(get<2>(p[e][f - 1]) - get<2>(p[e][f]));
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    int pp = l % d;
    int pl = lower_bound(p[pp].begin(), p[pp].end(), make_tuple(l, -1, -1)) -
             p[pp].begin();
    int pr = lower_bound(p[pp].begin(), p[pp].end(), make_tuple(r, -1, -1)) -
             p[pp].begin();
    cout << sum[pp][pr] - sum[pp][pl] << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 // 10^14
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll H, W, D;
  cin >> H >> W >> D;
  vector<vector<ll>> A(H, vector<ll>(W, 0));
  map<ll, pair<ll, ll>> P;
  rep(i, H) {
    rep(j, W) {
      cin >> A.at(i).at(j);
      A.at(i).at(j)--;
      P[A.at(i).at(j)] = make_pair(i, j);
    }
  }
  vector<vector<ll>> S(D, vector<ll>(H * W, 0));
  rep(i, D) {
    ll j = 0;
    while (D * (j + 1) + i < H * W) {
      ll x = P.at(D * j + i).first;
      ll y = P.at(D * j + i).second;
      ll nx = P.at(D * (j + 1) + i).first;
      ll ny = P.at(D * (j + 1) + i).second;
      S.at(i).at(j + 1) = S.at(i).at(j) + abs(nx - x) + abs(ny - y);
      j++;
    }
  }
  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll L, R;
    cin >> L >> R;
    L--;
    R--;
    ll k = L % D;
    ll j1 = L / D;
    ll j2 = R / D;
    cout << S.at(k).at(j2) - S.at(k).at(j1) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 // 10^14
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll H, W, D;
  cin >> H >> W >> D;
  vector<vector<ll>> A(H, vector<ll>(W, 0));
  map<ll, pair<ll, ll>> P;
  rep(i, H) {
    rep(j, W) {
      cin >> A.at(i).at(j);
      A.at(i).at(j)--;
      P[A.at(i).at(j)] = make_pair(i, j);
    }
  }
  vector<vector<ll>> S(D, vector<ll>(H * W / D + 3, 0));
  rep(i, D) {
    ll j = 0;
    while (D * (j + 1) + i < H * W) {
      ll x = P.at(D * j + i).first;
      ll y = P.at(D * j + i).second;
      ll nx = P.at(D * (j + 1) + i).first;
      ll ny = P.at(D * (j + 1) + i).second;
      S.at(i).at(j + 1) = S.at(i).at(j) + abs(nx - x) + abs(ny - y);
      j++;
    }
  }
  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll L, R;
    cin >> L >> R;
    L--;
    R--;
    ll k = L % D;
    ll j1 = L / D;
    ll j2 = R / D;
    cout << S.at(k).at(j2) - S.at(k).at(j1) << endl;
  }
}
Problem p03426 (C++)

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

#define INF 1 << 29
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)

using namespace std;

typedef pair<int, int> pii;
typedef long long int ll;

void gen_sosuu(int sosuu[]) {
  REP(i, 100010) { sosuu[i] = true; }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i, 2, sqrt(100000) + 1) {
    if (sosuu[i] == false)
      continue;
    for (int j = 2; i * j <= 100000; j++) {
      // printf("%d %d %d\n",i*j,i,j);
      sosuu[i * j] = false;
    }
  }
}
int h, w, d;
// 0から始める場合と1から始める場合
ll cost[9010] = {0};
map<int, pii> mp;
int q;

int main() {
  scanf("%d %d %d", &h, &w, &d);
  REP(i, h) REP(j, w) {
    int a;
    scanf("%d", &a);
    mp[a] = pii(i, j);
  }
  FOR(i, 1, d + 1) for (int j = i + d; j < h * w + 1; j += d) {
    int p_i = mp[j - d].first;
    int p_j = mp[j - d].second;
    int n_i = mp[j].first;
    int n_j = mp[j].second;
    cost[j] = cost[j - d] + abs(p_i - n_i) + abs(p_j - n_j);
  }
  scanf("%d", &q);
  REP(i, q) {
    int s, g;
    scanf("%d %d", &s, &g);
    printf("%lld\n", cost[g] - cost[s]);
  }
}


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

#define INF 1 << 29
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)

using namespace std;

typedef pair<int, int> pii;
typedef long long int ll;

void gen_sosuu(int sosuu[]) {
  REP(i, 100010) { sosuu[i] = true; }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i, 2, sqrt(100000) + 1) {
    if (sosuu[i] == false)
      continue;
    for (int j = 2; i * j <= 100000; j++) {
      // printf("%d %d %d\n",i*j,i,j);
      sosuu[i * j] = false;
    }
  }
}
int h, w, d;
// 0から始める場合と1から始める場合
ll cost[90100] = {0};
map<int, pii> mp;
int q;

int main() {
  scanf("%d %d %d", &h, &w, &d);
  REP(i, h) REP(j, w) {
    int a;
    scanf("%d", &a);
    mp[a] = pii(i, j);
  }
  FOR(i, 1, d + 1) for (int j = i + d; j < h * w + 1; j += d) {
    int p_i = mp[j - d].first;
    int p_j = mp[j - d].second;
    int n_i = mp[j].first;
    int n_j = mp[j].second;
    cost[j] = cost[j - d] + abs(p_i - n_i) + abs(p_j - n_j);
  }
  scanf("%d", &q);
  REP(i, q) {
    int s, g;
    scanf("%d %d", &s, &g);
    printf("%lld\n", cost[g] - cost[s]);
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = mod * mod;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)

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

  int A[H + 10][W + 10];
  int a;
  rep(i, H) rep(j, W) cin >> A[i][j];

  int Q;
  cin >> Q;

  int L[Q + 10], R[Q + 10];
  rep(i, Q) cin >> L[i] >> R[i];

  map<int, pair<int, int>> field;
  rep(i, H) rep(j, W) field[A[i][j]] = make_pair(i, j);

  int accum[D + 10][H * W + 10];
  rep(i, D) rep(j, H * W) accum[i][j] = 0;

  rep(i, D) {
    int x;
    if (i == 0)
      x = D;
    else
      x = i;
    while (x + D <= H * W) {
      accum[i][x / D + 1] = accum[i][x / D] +
                            abs(field[x].first - field[x + D].first) +
                            abs(field[x].second - field[x + D].second);
      x += D;
    }
  }

  rep(q, Q) {
    int x = L[q], y = R[q];
    int ans = accum[x % D][y / D] - accum[x % D][x / D];
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = mod * mod;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)

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

  int A[H + 10][W + 10];
  int a;
  rep(i, H) rep(j, W) cin >> A[i][j];

  int Q;
  cin >> Q;

  int L[Q + 10], R[Q + 10];
  rep(i, Q) cin >> L[i] >> R[i];

  map<int, pair<int, int>> field;
  rep(i, H) rep(j, W) field[A[i][j]] = make_pair(i, j);

  int accum[D + 10][H * W / D + 10];
  rep(i, D) rep(j, H * W / D) accum[i][j] = 0;

  rep(i, D) {
    int x;
    if (i == 0)
      x = D;
    else
      x = i;
    while (x + D <= H * W) {
      accum[i][x / D + 1] = accum[i][x / D] +
                            abs(field[x].first - field[x + D].first) +
                            abs(field[x].second - field[x + D].second);
      x += D;
    }
  }

  rep(q, Q) {
    int x = L[q], y = R[q];
    int ans = accum[x % D][y / D] - accum[x % D][x / D];
    cout << ans << endl;
  }
}
Problem p03426 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int sum[1000];

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<vector<int>> A(H, vector<int>(W));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> A[i][j];
    }
  }
  int Q;
  cin >> Q;
  vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> L[i] >> R[i];
  }

  // キーに対応する座標を格納するコンテナ
  vector<P> coord(H * W + 1);
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      coord[A[i][j]] = P(i + 1, j + 1);
    }
  }

  for (int i = H * W - D + 1; i <= H * W; ++i) {
    int now = i;
    while (now > D) {
      int temp = sum[now];
      P p1 = coord[now];
      now -= D;
      P p2 = coord[now];

      sum[now] = temp + abs(p2.first - p1.first) + abs(p2.second - p1.second);
    }
  }

  for (int i = 0; i < Q; ++i) {
    cout << sum[L[i]] - sum[R[i]] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int sum[100000];

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<vector<int>> A(H, vector<int>(W));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> A[i][j];
    }
  }
  int Q;
  cin >> Q;
  vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> L[i] >> R[i];
  }

  // キーに対応する座標を格納するコンテナ
  vector<P> coord(H * W + 1);
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      coord[A[i][j]] = P(i + 1, j + 1);
    }
  }

  for (int i = H * W - D + 1; i <= H * W; ++i) {
    int now = i;
    while (now > D) {
      int temp = sum[now];
      P p1 = coord[now];
      now -= D;
      P p2 = coord[now];

      sum[now] = temp + abs(p2.first - p1.first) + abs(p2.second - p1.second);
    }
  }

  for (int i = 0; i < Q; ++i) {
    cout << sum[L[i]] - sum[R[i]] << endl;
  }
}
Problem p03426 (C++)

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

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

  vector<ll> sum(h * w + d + d);

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

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

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

  vector<ll> sum(h * w + d + d);

  for (int i = 0; i < d; i++) {
    for (int j = i; j + d < h * w; j += d) {
      sum[j + d] = sum[j] + abs(p[j + d].first - p[j].first) +
                   abs(p[j + d].second - p[j].second);
    }
  }
  // cout<<sum.size()<<endl;
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << sum[r] - sum[l] << endl;
  }
  for (int i = 1; i < h * w + d; i += d) {
    // cout<<sum[i]<<endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
using P = pair<int, int>;
using T = tuple<long, long, long>;

long long MOD = 1000000007;
long long dp[100005][4];

int A[300][300];

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  map<int, P> mp;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      int a;
      cin >> a;
      mp[a] = make_pair(i + 1, j + 1);
    }
  }
  int Q;
  cin >> Q;
  vector<P> LR(Q);
  for (int i = 0; i < Q; ++i)
    cin >> LR[i].first >> LR[i].second;

  vector<int> distance(H * W + 1);
  for (int i = 0; i < D; ++i) {
    for (int j = H * W - i; 0 < j; --j) {
      if (j + D <= H * W) {
        distance[j] = distance[j + D] + abs(mp[j + D].first - mp[j].first) +
                      abs(mp[j + D].second - mp[j].second);
      } else {
        distance[j] = 0;
      }
    }
  }

  for (int i = 0; i < Q; ++i) {
    cout << distance[LR[i].first] - distance[LR[i].second] << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
using P = pair<int, int>;
using T = tuple<long, long, long>;

long long MOD = 1000000007;
long long dp[100005][4];

int A[300][300];

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  map<int, P> mp;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      int a;
      cin >> a;
      mp[a] = make_pair(i + 1, j + 1);
    }
  }
  int Q;
  cin >> Q;
  vector<P> LR(Q);
  for (int i = 0; i < Q; ++i)
    cin >> LR[i].first >> LR[i].second;

  vector<int> distance(H * W + 1);
  for (int j = H * W; 0 < j; --j) {
    if (j + D <= H * W) {
      distance[j] = distance[j + D] + abs(mp[j + D].first - mp[j].first) +
                    abs(mp[j + D].second - mp[j].second);
    } else {
      distance[j] = 0;
    }
  }

  for (int i = 0; i < Q; ++i) {
    cout << distance[LR[i].first] - distance[LR[i].second] << endl;
  }
}
Problem p03426 (C++)

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

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define REPO(i, n) for (int i = 1; i <= n; i++)
#define ll long long
#define INF 1999999999
#define MINF -1999999999
#define INF64 1999999999999999999
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007

ll h, w, d, r[444514], s[400][400];
vector<pair<ll, ll>> p(114514);
int main() {
  cin >> h >> w >> d;
  REP(i, h) REP(j, w) cin >> s[i][j];
  REP(i, h) {
    REP(j, w) { p[s[i][j]] = make_pair(i, j); }
  }
  REPO(i, h * w) {
    r[i + d] = r[i] + abs(p[i].first - p[i + d].first) +
               abs(p[i].second - p[i + d].second);
  }
  ll q;
  cin >> q;
  REP(i, q) {
    ll a, b;
    cin >> a >> b;
    cout << r[b] - r[a] << endl;
  }
}


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

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define REPO(i, n) for (int i = 1; i <= n; i++)
#define ll long long
#define INF 1999999999
#define MINF -1999999999
#define INF64 1999999999999999999
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007

ll h, w, d, r[444514], s[400][400];
vector<pair<ll, ll>> p(114514);
int main() {
  cin >> h >> w >> d;
  REP(i, h) REP(j, w) cin >> s[i][j];
  REP(i, h) {
    REP(j, w) { p[s[i][j]] = make_pair(i, j); }
  }
  REPO(i, h * w) {
    if (i + d <= h * w) {
      r[i + d] = r[i] + abs(p[i].first - p[i + d].first) +
                 abs(p[i].second - p[i + d].second);
    }
  }
  ll q;
  cin >> q;
  REP(i, q) {
    ll a, b;
    cin >> a >> b;
    cout << r[b] - r[a] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1000000007
ll H[302], W[302], man[90002], rui[90002];

int main() {
  ll h, w, d;
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ll z;
      cin >> z;
      z--;
      H[z] = i;
      W[z] = j;
    }
  }
  for (int i = 0; i < d; i++) {
    ll j = 1;
    while (i + d * j < h * w) {
      ll now = i + d * (j - 1);
      ll next = i + d * j;
      man[next] = abs(H[now] - H[next]) + abs(W[now] - W[next]);
      rui[next] = rui[now] + man[next];
      j++;
    }
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << rui[r] - rui[l] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1000000007
ll H[90002], W[90002], man[90002], rui[90002];

int main() {
  ll h, w, d;
  cin >> h >> w >> d;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ll z;
      cin >> z;
      z--;
      H[z] = i;
      W[z] = j;
    }
  }
  for (int i = 0; i < d; i++) {
    ll j = 1;
    while (i + d * j < h * w) {
      ll now = i + d * (j - 1);
      ll next = i + d * j;
      man[next] = abs(H[now] - H[next]) + abs(W[now] - W[next]);
      rui[next] = rui[now] + man[next];
      j++;
    }
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << rui[r] - rui[l] << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sys/time.h>
using namespace std;
typedef long long ll;
inline int ri() {
  register int x = 0;
  register bool f = 0;
  register char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = 1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  };
  return f ? -x : x;
}
void wi(ll x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    wi(x / 10);
  putchar('0' + x % 10);
}
const int N = 305 * 305;
struct data {
  int n, x, y;
  bool operator<(const data &t) const { return n < t.n; }
} q[N];
ll f[N * 3];
inline ll calc(int i, int j) {
  return abs(q[i].x - q[j].x) + abs(q[i].y - q[j].y);
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  int n = ri(), m = ri(), d = ri(), i, j, cc = 0, x, y;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      q[++cc].n = ri();
      q[cc].x = i;
      q[cc].y = j;
    }
  sort(q + 1, q + 1 + cc);
  for (i = cc - 1; i >= 1; i--)
    f[i] = f[i + d] + calc(i, i + d);
  int t = ri();
  while (t--) {
    x = ri(), y = ri();
    wi(f[x] - f[y]);
    putchar('\n');
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sys/time.h>
using namespace std;
typedef long long ll;
inline int ri() {
  register int x = 0;
  register bool f = 0;
  register char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = 1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  };
  return f ? -x : x;
}
void wi(ll x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    wi(x / 10);
  putchar('0' + x % 10);
}
const int N = 305 * 305;
struct data {
  int n, x, y;
  bool operator<(const data &t) const { return n < t.n; }
} q[N];
ll f[N * 3];
inline ll calc(int i, int j) {
  return abs(q[i].x - q[j].x) + abs(q[i].y - q[j].y);
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  int n = ri(), m = ri(), d = ri(), i, j, cc = 0, x, y;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      q[++cc].n = ri();
      q[cc].x = i;
      q[cc].y = j;
    }
  sort(q + 1, q + 1 + cc);
  for (i = cc - 1; i >= 1; i--)
    if (i + d <= cc)
      f[i] = f[i + d] + calc(i, i + d);
  int t = ri();
  while (t--) {
    x = ri(), y = ri();
    wi(f[x] - f[y]);
    putchar('\n');
  }
  return 0;
}
Problem p03426 (C++)

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

int s[305], px[305], py[305];

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;
      px[a] = i;
      py[a] = j;
    }
  }

  for (int i = d + 1; i <= h * w; i++)
    s[i] = s[i - d] + abs(px[i] - px[i - d]) + abs(py[i] - py[i - d]);

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    printf("%d\n", s[r] - s[l]);
  }

  return 0;
}

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

int s[305 * 305], px[305 * 305], py[305 * 305];

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;
      px[a] = i;
      py[a] = j;
    }
  }

  for (int i = d + 1; i <= h * w; i++)
    s[i] = s[i - d] + abs(px[i] - px[i - d]) + abs(py[i] - py[i - d]);

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    printf("%d\n", s[r] - s[l]);
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <set>

#include <string>

#include <cstring>

#include <cmath>

#include <map>

#include <stack>

#define N 1005

using namespace std;

typedef long long ll;

int a[305][305];

struct node

{

  int x;

  int y;

} p[90005];

int ans[90005];

int main()

{

  int h, w, d, len;

  scanf("%d%d%d", &h, &w, &d);

  for (int i = 1; i <= h; i++)

    for (int j = 1; j <= w; j++) {

      scanf("%d", &a[i][j]);

      int n = a[i][j];

      p[n].x = i;

      p[n].y = j;
    }

  int q;

  len = h * w;

  scanf("%d", &q);

  for (int i = 1; i <= d; i++)

  {

    int l = i, r = len, sum = 0;

    ans[i] = 0;

    while (l < r)

    {

      sum = sum + abs(p[l].x - p[l + d].x) + abs(p[l].y - p[l + d].y);

      l = l + d;

      ans[l] = sum;
    }
  }

  while (q--)

  {

    int l, r;

    scanf("%d%d", &l, &r);

    printf("%d\n", ans[r] - ans[l]);
  }
}


Fixed submission:

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <set>

#include <string>

#include <cstring>

#include <cmath>

#include <map>

#include <stack>

#define N 1005

using namespace std;

typedef long long ll;

int a[305][305];

struct node

{

  int x;

  int y;

} p[90005];

int ans[90005];

int main()

{

  int h, w, d, len;

  scanf("%d%d%d", &h, &w, &d);

  for (int i = 1; i <= h; i++)

    for (int j = 1; j <= w; j++) {

      scanf("%d", &a[i][j]);

      int n = a[i][j];

      p[n].x = i;

      p[n].y = j;
    }

  int q;

  len = h * w;

  scanf("%d", &q);

  for (int i = 1; i <= d; i++)

  {

    int l = i, r = len, sum = 0;

    ans[i] = 0;

    while (l <= r - d)

    {

      sum = sum + abs(p[l].x - p[l + d].x) + abs(p[l].y - p[l + d].y);

      l = l + d;

      ans[l] = sum;
    }
  }

  while (q--)

  {

    int l, r;

    scanf("%d%d", &l, &r);

    printf("%d\n", ans[r] - ans[l]);
  }
}
Problem p03426 (C++)

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

using namespace std;

int red, stup;
int d;
int grid[305][305];
pair<int, int> x[305 * 305];
int ans[305 * 305];

int main() {
  cin >> red >> stup >> d;
  for (int i = 0; i < red; i++) {
    for (int j = 0; j < stup; j++) {
      cin >> grid[i][j];
      x[grid[i][j]].first = i + 1;
      x[grid[i][j]].second = j + 1;
    }
  }
  for (int i = 1; i <= d; i++) {
    ans[i] = 0;
  }
  for (int i = 1; i <= red * stup; i++) {
    ans[i + d] = ans[i] + abs(x[i + d].first - x[i].first) +
                 abs(x[i + d].second - x[i].second);
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    if (l == r)
      cout << 0 << endl;
    else {
      cout << ans[r] - ans[l] << endl;
    }
  }
}

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

using namespace std;

int red, stup;
int d;
int grid[305][305];
pair<int, int> x[305 * 305];
int ans[305 * 305];

int main() {
  cin >> red >> stup >> d;
  for (int i = 0; i < red; i++) {
    for (int j = 0; j < stup; j++) {
      cin >> grid[i][j];
      x[grid[i][j]].first = i + 1;
      x[grid[i][j]].second = j + 1;
    }
  }
  for (int i = 1; i <= d; i++) {
    ans[i] = 0;
  }
  for (int i = 1; i <= red * stup - d; i++) {
    ans[i + d] = ans[i] + abs(x[i + d].first - x[i].first) +
                 abs(x[i + d].second - x[i].second);
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    if (l == r)
      cout << 0 << endl;
    else {
      cout << ans[r] - ans[l] << endl;
    }
  }
}
Problem p03426 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define FORr(i, n, m) for (ll i = n; i > (ll)m; i--)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define cinv(v, n, m) FOR(i, n, m) cin >> v.at(i)
#define coutv(v, n, m) FOR(i, n, m) cout << v.at(i) << " "
#define cout(n) cout << fixed << setprecision(n)
int main() {
  ll H, W, D;
  cin >> H >> W >> D;
  vector<P> v(H * W + 1);

  ll in;
  FOR(i, 0, H) {
    FOR(j, 0, W) {
      cin >> in;
      v[in] = P(i, j);
    }
  }

  vector<ll> key(H * W + 1, 0);
  ll x;
  FOR(i, D, H * W + 1) {
    key[i] =
        abs(v[i].first - v[i - D].first) + abs(v[i].second - v[i - D].second);
  }
  FOR(i, 0, D) {
    x = 0;
    while (i + x + D <= H * W + 1) {
      key[i + x + D] += key[i + x];
      x += D;
    }
  }

  ll Q;
  cin >> Q;
  // FOR(i,0,H*W+1){
  //   cout<<key[i]<<" ";
  // }
  // cout<<endl;
  vector<ll> av(Q), bv(Q);
  FOR(i, 0, Q) { cin >> av[i] >> bv[i]; }
  FOR(i, 0, Q) { cout << key[bv[i]] - key[av[i]] << endl; }
}


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

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define FORr(i, n, m) for (ll i = n; i > (ll)m; i--)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define cinv(v, n, m) FOR(i, n, m) cin >> v.at(i)
#define coutv(v, n, m) FOR(i, n, m) cout << v.at(i) << " "
#define cout(n) cout << fixed << setprecision(n)
int main() {
  ll H, W, D;
  cin >> H >> W >> D;
  vector<P> v(H * W + 1);

  ll in;
  FOR(i, 0, H) {
    FOR(j, 0, W) {
      cin >> in;
      v[in] = P(i, j);
    }
  }

  vector<ll> key(H * W + 1, 0);
  ll x;
  FOR(i, D, H * W + 1) {
    key[i] =
        abs(v[i].first - v[i - D].first) + abs(v[i].second - v[i - D].second);
  }
  FOR(i, 0, D) {
    x = 0;
    while (i + x + D < H * W + 1) {
      key[i + x + D] += key[i + x];
      x += D;
    }
  }

  ll Q;
  cin >> Q;
  // FOR(i,0,H*W+1){
  //   cout<<key[i]<<" ";
  // }
  // cout<<endl;
  vector<ll> av(Q), bv(Q);
  FOR(i, 0, Q) { cin >> av[i] >> bv[i]; }
  FOR(i, 0, Q) { cout << key[bv[i]] - key[av[i]] << endl; }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define inf 500000000
#define IOS ios_base::sync_with_stdio(0)
#define meM(y, a) memset(y, a, sizeof y)
#define sC(a) scanf("%d", &a)
#define alL(a) a.begin(), a.end()
#define prinT(a, sz)                                                           \
  cout << a[0];                                                                \
  for (int i = 1; i < sz; i++)                                                 \
    cout << " " << a[i];                                                       \
  cout << endl
#define ranD srand(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
// int fx[]={0,0,1,-1};
// int fy[]={1,-1,0,0};
// int gx[]={0,0,1,1,1,-1,-1,-1};
// int gy[]={1,-1,0,1,-1,0,1,-1};
bool check(int n, int p) { return (1 << p) & n; }
const int N = 302;
pi pos[N];
int pre[N];
int main() {
  IOS;
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int a;
      cin >> a;
      pos[a] = {i, j};
    }
  }
  int mx = n * m;
  for (int i = 1; i <= k; i++) {
    int d = 0;
    for (int j = i + k; j <= mx; j += k) {
      d += abs(pos[j].ff - pos[j - k].ff) + abs(pos[j].ss - pos[j - k].ss);
      pre[j] = d;
      //                cout<<"distance of "<<j<<" = "<<d<<endl;
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << pre[r] - pre[l] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define inf 500000000
#define IOS ios_base::sync_with_stdio(0)
#define meM(y, a) memset(y, a, sizeof y)
#define sC(a) scanf("%d", &a)
#define alL(a) a.begin(), a.end()
#define prinT(a, sz)                                                           \
  cout << a[0];                                                                \
  for (int i = 1; i < sz; i++)                                                 \
    cout << " " << a[i];                                                       \
  cout << endl
#define ranD srand(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
// int fx[]={0,0,1,-1};
// int fy[]={1,-1,0,0};
// int gx[]={0,0,1,1,1,-1,-1,-1};
// int gy[]={1,-1,0,1,-1,0,1,-1};
bool check(int n, int p) { return (1 << p) & n; }
const int N = 100010;
pi pos[N];
int pre[N];
int main() {
  IOS;
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int a;
      cin >> a;
      pos[a] = {i, j};
    }
  }
  int mx = n * m;
  for (int i = 1; i <= k; i++) {
    int d = 0;
    for (int j = i + k; j <= mx; j += k) {
      d += abs(pos[j].ff - pos[j - k].ff) + abs(pos[j].ss - pos[j - k].ss);
      pre[j] = d;
      //                cout<<"distance of "<<j<<" = "<<d<<endl;
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << pre[r] - pre[l] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define rep(n, i) Rep(0, n - 1, i)
#define MAX 100000
typedef long long ll;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 31 - 1;
const int MOD = 1000000007;

// 1-indexed
vector<int> xfields(MAX);
vector<int> yfields(MAX);
ll dp[MAX];

void solve(void) {
  int H, W, D;
  scanf("%d %d %d\n", &H, &W, &D);
  rep(H, i) {
    rep(W, j) {
      int num;
      scanf("%d ", &num);
      xfields[num] = j;
      yfields[num] = i;
    }
    scanf("\n");
  }
  Rep(0, D - 1, i) {
    for (int d = 0; d < H * W; d += D) {
      dp[i + d + D] = dp[i + d] + abs(xfields[i + d + D] - xfields[i + d]) +
                      abs(yfields[i + d + D] - yfields[i + d]);
    }
  }
  int Q;
  scanf("%d\n", &Q);
  rep(Q, k) {
    int l, r;
    scanf("%d %d\n", &l, &r);
    if (r == l) {
      printf("0\n");
      continue;
    }
    printf("%lld\n", dp[r] - dp[l]);
  }
}

int main(void) {
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define rep(n, i) Rep(0, n - 1, i)
#define MAX 1000000
typedef long long ll;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 31 - 1;
const int MOD = 1000000007;

// 1-indexed
vector<int> xfields(MAX);
vector<int> yfields(MAX);
ll dp[MAX];

void solve(void) {
  int H, W, D;
  scanf("%d %d %d\n", &H, &W, &D);
  rep(H, i) {
    rep(W, j) {
      int num;
      scanf("%d ", &num);
      xfields[num] = j;
      yfields[num] = i;
    }
    scanf("\n");
  }
  Rep(0, D - 1, i) {
    for (int d = 0; d < H * W; d += D) {
      dp[i + d + D] = dp[i + d] + abs(xfields[i + d + D] - xfields[i + d]) +
                      abs(yfields[i + d + D] - yfields[i + d]);
    }
  }
  int Q;
  scanf("%d\n", &Q);
  rep(Q, k) {
    int l, r;
    scanf("%d %d\n", &l, &r);
    if (r == l) {
      printf("0\n");
      continue;
    }
    printf("%lld\n", dp[r] - dp[l]);
  }
}

int main(void) {
  solve();
  return 0;
}