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

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

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  pair<int, int> list[H * W + 5];
  int d[H * W + 5];
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      int tmp;
      cin >> tmp;
      list[tmp].first = i;
      list[tmp].second = j;
    }
  }

  d[1] = 0;
  for (int i = 1; i < H * W; ++i) {
    for (int j = i; j + D <= H * W; j += D) {
      int xprev = list[j].first, yprev = list[j].second,
          xcrnt = list[j + D].first, ycrnt = list[j + D].second, dprev = d[j];
      d[j + D] = dprev + abs(xcrnt - xprev) + abs(ycrnt - yprev);
    }
  }

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

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

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  pair<int, int> list[H * W + 5];
  int d[H * W + 5];
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      int tmp;
      cin >> tmp;
      list[tmp].first = i;
      list[tmp].second = j;
    }
  }

  d[1] = 0;
  for (int i = 1; i <= D; ++i) {
    for (int j = i; j + D <= H * W; j += D) {
      int xprev = list[j].first, yprev = list[j].second,
          xcrnt = list[j + D].first, ycrnt = list[j + D].second, dprev = d[j];
      d[j + D] = dprev + abs(xcrnt - xprev) + abs(ycrnt - yprev);
    }
  }

  int Q;
  cin >> Q;
  int L[Q], R[Q];
  for (int i = 0; i < Q; ++i)
    cin >> L[i] >> R[i];
  for (int i = 0; i < Q; ++i)
    cout << d[R[i]] - d[L[i]] << endl;
  return 0;
}
Problem p03426 (C++)

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m, d;
  cin >> n >> m >> d;
  vector<pair<int, int>> a(100010);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x;
      cin >> x;
      a[x] = {i, j};
    }
  }
  vector<vector<int>> g(d + 1);
  for (int i = 1; i <= d; i++) {
    g[i].push_back(0);
    g[i].push_back(0);
    for (int j = i + d; j <= 100; j += d) {
      g[i].push_back(abs(a[j].first - a[j - d].first) +
                     abs(a[j].second - a[j - d].second) + g[i].back());
    }
  }

  int q;
  cin >> q;
  while (q-- > 0) {
    int l, r;
    cin >> l >> r;
    int i = l % d;
    if (i == 0) {
      i = d;
      cout << g[i][r / d] - g[i][l / d] << '\n';
    } else {
      cout << g[i][r / d + 1] - g[i][l / d + 1] << '\n';
    }
  }
}

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m, d;
  cin >> n >> m >> d;
  vector<pair<int, int>> a(100010);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x;
      cin >> x;
      a[x] = {i, j};
    }
  }
  vector<vector<int>> g(d + 1);
  for (int i = 1; i <= d; i++) {
    g[i].push_back(0);
    g[i].push_back(0);
    for (int j = i + d; j <= 100000; j += d) {
      g[i].push_back(abs(a[j].first - a[j - d].first) +
                     abs(a[j].second - a[j - d].second) + g[i].back());
    }
  }

  int q;
  cin >> q;
  while (q-- > 0) {
    int l, r;
    cin >> l >> r;
    int i = l % d;
    if (i == 0) {
      i = d;
      cout << g[i][r / d] - g[i][l / d] << '\n';
    } else {
      cout << g[i][r / d + 1] - g[i][l / d + 1] << '\n';
    }
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc089/tasks/abc089_d
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, d, t, q;
  cin >> h >> w >> d;
  vector<pii> v(h * w + 10);
  vector<vector<int>> disp(h, vector<int>(w));
  REP(i, h) REP(j, w) {
    cin >> t;
    v[t] = {i, j};
    disp[i][j] = t;
  }
  vector<ll> memo(h * w + 1, 0);
  REP(i, d) {
    int start = i;
    int goal = h * w;
    ll sum = 0;
    while (start < goal) {
      pii posNow = v[start];
      pii posNext = v[start + d];
      int diff = abs(posNow.first - posNext.first) +
                 abs(posNow.second - posNext.second);
      sum += diff;
      start += d;
      if (start > h * w)
        break;
      memo[start] = sum;
    }
  }
  cin >> q;
  vector<pii> s(q);
  REP(i, q) cin >> s[i].first >> s[i].second;
  REP(i, q) cout << memo[s[i].second] - memo[s[i].first] << "\n";

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/abc089/tasks/abc089_d
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, d, t, q;
  cin >> h >> w >> d;
  vector<pii> v(h * w * 2);
  vector<vector<int>> disp(h, vector<int>(w));
  REP(i, h) REP(j, w) {
    cin >> t;
    v[t] = {i, j};
    disp[i][j] = t;
  }
  vector<ll> memo(h * w + 1, 0);
  REP(i, d) {
    int start = i;
    int goal = h * w;
    ll sum = 0;
    while (start < goal) {
      pii posNow = v[start];
      pii posNext = v[start + d];
      int diff = abs(posNow.first - posNext.first) +
                 abs(posNow.second - posNext.second);
      sum += diff;
      start += d;
      if (start > h * w)
        break;
      memo[start] = sum;
    }
  }
  cin >> q;
  vector<pii> s(q);
  REP(i, q) cin >> s[i].first >> s[i].second;
  REP(i, q) cout << memo[s[i].second] - memo[s[i].first] << "\n";

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a, b) memset((a), (b), sizeof(a))
const LL INF = 1e9 + 7;
const int N = 1e5 + 10;
pair<int, int> p[N];
LL ans[N];

int dis(int i, int j) {
  return abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second);
}
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n, m;
  cin >> n >> m;
  int k;
  cin >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x;
      cin >> x;
      p[x] = {i, j};
    }
  }
  for (int i = n * m; i > 0; i--) {
    ans[i] += ans[i + k] + dis(i, i + k);
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << ans[l] - ans[r] << '\n';
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a, b) memset((a), (b), sizeof(a))
const LL INF = 1e9 + 7;
const int N = 1e5 + 10;
pair<int, int> p[N];
LL ans[N];

int dis(int i, int j) {
  return abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second);
}
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n, m;
  cin >> n >> m;
  int k;
  cin >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x;
      cin >> x;
      p[x] = {i, j};
    }
  }
  for (int i = n * m - k; i > 0; i--) {
    ans[i] += ans[i + k] + dis(i, i + k);
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << ans[l] - ans[r] << '\n';
  }
  return 0;
}
Problem p03426 (C++)

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

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 310, M = 1010;

int n, m, k, d;
int g[N][N];
PII p[N * N];
vector<vector<LL>> s;

int main() {
  scanf("%d%d%d", &n, &m, &d);

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> g[i][j];
      p[g[i][j]] = {i, j};
    }

  s.resize(d + 10);
  for (int i = 0; i < d; i++) {
    s[i].resize((n * m + d - 1) / d + 10);
    s[i][0] = 0;
    for (int j = 0; (j + 1) * d <= n * m; j++) {
      PII st = p[j * d + i], ed = p[(j + 1) * d + i];
      LL sum = abs(st.F - ed.F) + abs(st.S - ed.S);
      s[i][j + 1] = s[i][j] + sum;
    }
  }

  scanf("%d", &k);
  while (k--) {
    int l, r;
    scanf("%d%d", &l, &r);

    int t = l % d;
    printf("%lld\n", s[t][r / d] - s[t][l / d]);
  }
  return 0;
}

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

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 310, M = 1010;

int n, m, k, d;
int g[N][N];
PII p[N * N];
vector<vector<LL>> s;

int main() {
  scanf("%d%d%d", &n, &m, &d);

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> g[i][j];
      p[g[i][j]] = {i, j};
    }

  s.resize(d + 10);
  for (int i = 0; i < d; i++) {
    s[i].resize((n * m + d - 1) / d + 10);
    s[i][0] = 0;
    for (int j = 0; (j + 1) * d + i <= n * m; j++) {
      PII st = p[j * d + i], ed = p[(j + 1) * d + i];
      LL sum = abs(st.F - ed.F) + abs(st.S - ed.S);
      s[i][j + 1] = s[i][j] + sum;
    }
  }

  scanf("%d", &k);
  while (k--) {
    int l, r;
    scanf("%d%d", &l, &r);

    int t = l % d;
    printf("%lld\n", s[t][r / d] - s[t][l / d]);
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ALL(x) (x).begin(), (x).end()
#define dump(x) cout << (x) << endl
#define LMAX 9223372036854775807LL
#define LMIN -9223372036854775807LL
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

const ll INF = 1e18;
const ll mod = 1e9 + 7;

int main() {
  ll h, w, d;
  cin >> h >> w >> d;
  vector<vector<P>> p(d, vector<P>(300));
  vector<vector<ll>> dif(d, vector<ll>(300));
  REP(i, h) {
    REP(j, w) {
      int a;
      cin >> a;
      int t = a % d, s = a / d;
      p[t][s] = make_pair(i, j);
    }
  }
  REP(i, d) {
    dif[i][0] = 0;
    for (int j = 1; j * d + i <= h * w; j++) {
      dif[i][j] = fabs(p[i][j - 1].first - p[i][j].first) +
                  fabs(p[i][j - 1].second - p[i][j].second) + dif[i][j - 1];
    }
  }
  ll q;
  cin >> q;
  REP(i, q) {
    ll l, r;
    cin >> l >> r;
    dump(dif[l % d][r / d] - dif[l % d][l / d]);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ALL(x) (x).begin(), (x).end()
#define dump(x) cout << (x) << endl
#define LMAX 9223372036854775807LL
#define LMIN -9223372036854775807LL
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

const ll INF = 1e18;
const ll mod = 1e9 + 7;

int main() {
  ll h, w, d;
  cin >> h >> w >> d;
  vector<vector<P>> p(d, vector<P>(h * w / d + 1));
  vector<vector<ll>> dif(d, vector<ll>(h * w / d + 1));
  REP(i, h) {
    REP(j, w) {
      int a;
      cin >> a;
      int t = a % d, s = a / d;
      p[t][s] = make_pair(i, j);
    }
  }
  REP(i, d) {
    dif[i][0] = 0;
    for (int j = 1; j * d + i <= h * w; j++) {
      dif[i][j] = fabs(p[i][j - 1].first - p[i][j].first) +
                  fabs(p[i][j - 1].second - p[i][j].second) + dif[i][j - 1];
    }
  }
  ll q;
  cin >> q;
  REP(i, q) {
    ll l, r;
    cin >> l >> r;
    dump(dif[l % d][r / d] - dif[l % d][l / d]);
  }
}
Problem p03426 (C++)

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

using namespace std;

int H, W, D, Q;
int A[301][301];
int dp[300 * 300 + 1];
int L[301], R[301];

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

  unordered_map<int, pair<int, int>> map;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> A[i][j];
      map[A[i][j]] = make_pair(i, j);
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++)
    cin >> L[i] >> R[i];

  for (int i = 1; i <= H * W; i++) {
    if (i <= D) {
      dp[i] = 0;
    } else {
      auto prev = map[i - D];
      auto present = map[i];
      dp[i] = dp[i - D] + abs(present.first - prev.first) +
              abs(present.second - prev.second);
    }
  }

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int H, W, D, Q;
int A[301][301];
int dp[300 * 300 + 1];
int L[100000], R[100000];

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

  unordered_map<int, pair<int, int>> map;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> A[i][j];
      map[A[i][j]] = make_pair(i, j);
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++)
    cin >> L[i] >> R[i];

  for (int i = 1; i <= H * W; i++) {
    if (i <= D) {
      dp[i] = 0;
    } else {
      auto prev = map[i - D];
      auto present = map[i];
      dp[i] = dp[i - D] + abs(present.first - prev.first) +
              abs(present.second - prev.second);
    }
  }

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

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P2;

int main() {
  int H, W, D;
  scanf("%d %d %d", &H, &W, &D);

  vector<P2> vec[D];

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int A;
      scanf("%d", &A);
      vec[A % D].push_back(make_pair(A, make_pair(i, j)));
    }
  }

  int len[D][90001];
  for (int i = 0; i < D; i++) {
    sort(vec[i].begin(), vec[i].end());

    len[i][0] = 0;
    for (int j = 1; j < vec[i].size(); j++) {
      len[i][j] = abs(vec[i][j].second.first - vec[i][j - 1].second.first) +
                  abs(vec[i][j].second.second - vec[i][j - 1].second.second);
    }
    for (int j = 1; j < vec[i].size(); j++) {
      len[i][j] += len[i][j - 1];
    }
  }

  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int L, R;
    scanf("%d %d", &L, &R);
    int d = 0;
    if (L % D == 0)
      d = 1;
    printf("%d\n", len[R % D][R / D - d] - len[L % D][L / D - d]);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P2;

int main() {
  int H, W, D;
  scanf("%d %d %d", &H, &W, &D);

  vector<P2> vec[D];

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int A;
      scanf("%d", &A);
      vec[A % D].push_back(make_pair(A, make_pair(i, j)));
    }
  }

  int len[D][500000 / D];
  for (int i = 0; i < D; i++) {
    sort(vec[i].begin(), vec[i].end());

    len[i][0] = 0;
    for (int j = 1; j < vec[i].size(); j++) {
      len[i][j] = abs(vec[i][j].second.first - vec[i][j - 1].second.first) +
                  abs(vec[i][j].second.second - vec[i][j - 1].second.second);
    }
    for (int j = 1; j < vec[i].size(); j++) {
      len[i][j] += len[i][j - 1];
    }
  }

  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int L, R;
    scanf("%d %d", &L, &R);
    int d = 0;
    if (L % D == 0)
      d = 1;
    printf("%d\n", len[R % D][R / D - d] - len[L % D][L / D - d]);
  }

  return 0;
}
Problem p03426 (C++)

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

int main(void) {
  int h, w, d;
  scanf("%d %d %d", &h, &w, &d);
  vector<pair<int, int>> data(1000000);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int a;
      scanf("%d", &a);
      data[a] = {i, j};
    }
  }
  vector<int> c(90001);

  for (int i = 1; i <= d; i++) {
    int t = i;
    while (t < 90001) {
      c[t + d] = c[t] + abs(data[t].F - data[t + d].F) +
                 abs(data[t].S - data[t + d].S);
      t += d;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    cout << c[b] - c[a] << endl;
  }
}


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

int main(void) {
  int h, w, d;
  scanf("%d %d %d", &h, &w, &d);
  vector<pair<int, int>> data(1000000);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int a;
      scanf("%d", &a);
      data[a] = {i, j};
    }
  }
  vector<int> c(900001);

  for (int i = 1; i <= d; i++) {
    int t = i;
    while (t < 90001) {
      c[t + d] = c[t] + abs(data[t].F - data[t + d].F) +
                 abs(data[t].S - data[t + d].S);
      t += d;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    cout << c[b] - c[a] << endl;
  }
}
Problem p03426 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)

pair<int, int> A[300 * 300 + 100];
int main() {
  int H, W, D;
  cin >> H >> W >> D;
  REP(i, H) {
    REP(j, W) {
      int a;
      cin >> a;
      A[a] = make_pair(i, j);
    }
  }

  vector<vector<int>> X(D + 1);
  for (int d = 1; d <= D; d++) {
    int lastx;
    int lasty;
    int lastc;
    for (int t = d; t < H * W; t += D) {
      int x = A[t].first;
      int y = A[t].second;
      int cost = 0;
      if (t != d) {
        cost = abs(x - lastx) + abs(y - lasty);
        cost += lastc;
      }
      lastx = x;
      lasty = y;
      lastc = cost;
      X[d].push_back(cost);
    }
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int l, r;
    cin >> l >> r;
    int idxh = (r - (r % D)) / D;
    int idxl = (l - (l % D)) / D;
    if (l % D == 0)
      idxl--, idxh--;
    int s = (l % D == 0) ? D : l % D;
    ll ans = X[s][idxh] - X[s][idxl];
    cout << ans << endl;
  }
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)

pair<int, int> A[300 * 300 + 100];
int main() {
  int H, W, D;
  cin >> H >> W >> D;
  REP(i, H) {
    REP(j, W) {
      int a;
      cin >> a;
      A[a] = make_pair(i, j);
    }
  }

  vector<vector<int>> X(D + 1);
  for (int d = 1; d <= D; d++) {
    int lastx;
    int lasty;
    int lastc;
    for (int t = d; t <= H * W; t += D) {
      int x = A[t].first;
      int y = A[t].second;
      int cost = 0;
      if (t != d) {
        cost = abs(x - lastx) + abs(y - lasty);
        cost += lastc;
      }
      lastx = x;
      lasty = y;
      lastc = cost;
      X[d].push_back(cost);
    }
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int l, r;
    cin >> l >> r;
    int idxh = (r - (r % D)) / D;
    int idxl = (l - (l % D)) / D;
    if (l % D == 0)
      idxl--, idxh--;
    int s = (l % D == 0) ? D : l % D;
    ll ans = X[s][idxh] - X[s][idxl];
    cout << ans << endl;
  }
  return 0;
}
Problem p03426 (C++)

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

#define abs(X) ((X) < 0 ? -(X) : (X))

using namespace std;

int sum[90005];
pair<int, int> point[90005];

int main() {
  int h, w, d;
  int q;
  int l, r;
  int a;

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

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

    sum[x] = 0;

    while (x <= h * w) {
      sum[x + d] = sum[x] + abs(point[x + d].first - point[x].first) +
                   abs(point[x + d].second - point[x].second);

      x += d;
    }
  }

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

    cout << sum[r] - sum[l] << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <utility>

#define abs(X) ((X) < 0 ? -(X) : (X))

using namespace std;

int sum[90005];
pair<int, int> point[90005];

int main() {
  int h, w, d;
  int q;
  int l, r;
  int a;

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

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

    sum[x] = 0;

    while (x + d <= h * w) {
      sum[x + d] = sum[x] + abs(point[x + d].first - point[x].first) +
                   abs(point[x + d].second - point[x].second);

      x += d;
    }
  }

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

    cout << sum[r] - sum[l] << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

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

  vector<int> s(h * w + 10, 0);
  for (int i = 1; i <= d; ++i) {
    int t = i;
    while (t + d <= h * w + 10) {
      s[t + d] = s[t] + abs(p[t + d].first - p[t].first) +
                 abs(p[t + d].second - p[t].second);
      t += d;
    }
  }

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

    if (l == r) {
      cout << 0 << endl;
    } else {
      cout << s[r] - s[l] << endl;
    }
  }
}

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

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

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

  vector<int> s(h * w + 10, 0);
  for (int i = 1; i <= d; ++i) {
    int t = i;
    while (t + d < h * w + 10) {
      s[t + d] = s[t] + abs(p[t + d].first - p[t].first) +
                 abs(p[t + d].second - p[t].second);
      t += d;
    }
  }

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

    if (l == r) {
      cout << 0 << endl;
    } else {
      cout << s[r] - s[l] << endl;
    }
  }
}
Problem p03426 (C++)

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

using namespace std;
typedef long long ll;

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

  vector<vector<ll>> A(H + 10, vector<ll>(W + 10));
  vector<ll> idx(H * W + 10), idy(H * W + 10), d(H * W + 10);
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      cin >> A[i][j];
      idx[A[i][j]] = i;
      idy[A[i][j]] = j;
    }
  }
  for (int i = D + 1; i <= H * W; i++) {
    d[i] = d[i - D] + llabs(idx[i] - idx[i - D]) + llabs(idy[i] - idy[i - D]);
  }
  ll Q;
  cin >> Q;
  vector<ll> L(Q + 10), R(Q + 10);
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
  }
  for (int i = 0; i < Q; i++) {
    cout << d[R[i]] - d[L[i]] << endl;
  }
}

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

using namespace std;
typedef long long ll;

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

  vector<vector<ll>> A(H + 10, vector<ll>(W + 10));
  vector<ll> idx(H * W + 10), idy(H * W + 10), d(H * W + 10);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      idx[A[i][j]] = i;
      idy[A[i][j]] = j;
    }
  }
  for (int i = D + 1; i <= H * W; i++) {
    d[i] = d[i - D] + llabs(idx[i] - idx[i - D]) + llabs(idy[i] - idy[i - D]);
  }
  ll Q;
  cin >> Q;
  vector<ll> L(Q + 10), R(Q + 10);
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
  }
  for (int i = 0; i < Q; i++) {
    cout << d[R[i]] - d[L[i]] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
/*  -*- coding: utf-8 -*-
 *
 * d.cc: D: Practical Skill Test
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_H = 300;
const int MAX_W = 300;
const int MAX_HW = MAX_H * MAX_W;

/* typedef */

/* global variables */

int ais[MAX_H], ajs[MAX_W];
int dsums[MAX_HW];

/* subroutines */

/* main */

int main() {
  int h, w, d;
  scanf("%d%d%d", &h, &w, &d);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int aij;
      scanf("%d", &aij);
      aij--;
      ais[aij] = i, ajs[aij] = j;
    }

  int hw = h * w;
  for (int i = 0, j = d; j < hw; i++, j++)
    dsums[j] = dsums[i] + abs(ais[j] - ais[i]) + abs(ajs[j] - ajs[i]);

  int q;
  scanf("%d", &q);

  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--, r--;
    printf("%d\n", dsums[r] - dsums[l]);
  }
  return 0;
}


Fixed submission:
/*  -*- coding: utf-8 -*-
 *
 * d.cc: D: Practical Skill Test
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_H = 300;
const int MAX_W = 300;
const int MAX_HW = MAX_H * MAX_W;

/* typedef */

/* global variables */

int ais[MAX_HW], ajs[MAX_HW];
int dsums[MAX_HW];

/* subroutines */

/* main */

int main() {
  int h, w, d;
  scanf("%d%d%d", &h, &w, &d);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int aij;
      scanf("%d", &aij);
      aij--;
      ais[aij] = i, ajs[aij] = j;
    }

  int hw = h * w;
  for (int i = 0, j = d; j < hw; i++, j++)
    dsums[j] = dsums[i] + abs(ais[j] - ais[i]) + abs(ajs[j] - ajs[i]);

  int q;
  scanf("%d", &q);

  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--, r--;
    printf("%d\n", dsums[r] - dsums[l]);
  }
  return 0;
}
Problem p03426 (C++)

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

using namespace std;

class nv {
public:
  int a;
  int x, y;
  bool operator<(const nv &obj) const { return a < obj.a; }
};

int h, w, d, q;
unsigned long long int s[300 * 300];
vector<nv> p;

void input() {
  nv temp;
  cin >> h >> w >> d;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      cin >> temp.a;
      temp.x = x;
      temp.y = y;
      p.push_back(temp);
    }
  }
  cin >> q;
}

void prepare_query(void) {
  sort(p.begin(), p.end());
  for (int i = 1; i <= d; i++) {
    for (int ii = i + d; ii <= h * w; ii = ii + d) {
      nv cmp;
      cmp.a = ii - d;
      auto i_b = lower_bound(p.begin(), p.end(), cmp);
      cmp.a = ii;
      auto i_a = lower_bound(p.begin(), p.end(), cmp);
      s[ii] = s[ii - d] + abs(i_a->x - i_b->x) + abs(i_a->y - i_b->y);
    }
  }
}

unsigned long long int proc_query(int l, int r) { return s[r] - s[l]; }

int main(void) {
  input();
  prepare_query();
  int l, r;
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    cout << proc_query(l, r) << endl;
  }
}


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

using namespace std;

class nv {
public:
  int a;
  int x, y;
  bool operator<(const nv &obj) const { return a < obj.a; }
};

int h, w, d, q;
unsigned long long int s[300 * 300 + 1];
vector<nv> p;

void input() {
  nv temp;
  cin >> h >> w >> d;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      cin >> temp.a;
      temp.x = x;
      temp.y = y;
      p.push_back(temp);
    }
  }
  cin >> q;
}

void prepare_query(void) {
  sort(p.begin(), p.end());
  for (int i = 1; i <= d; i++) {
    for (int ii = i + d; ii <= h * w; ii = ii + d) {
      nv cmp;
      cmp.a = ii - d;
      auto i_b = lower_bound(p.begin(), p.end(), cmp);
      cmp.a = ii;
      auto i_a = lower_bound(p.begin(), p.end(), cmp);
      s[ii] = s[ii - d] + abs(i_a->x - i_b->x) + abs(i_a->y - i_b->y);
    }
  }
}

unsigned long long int proc_query(int l, int r) { return s[r] - s[l]; }

int main(void) {
  input();
  prepare_query();
  int l, r;
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    cout << proc_query(l, r) << endl;
  }
}
Problem p03426 (C++)

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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  i64 H, W, D;
  cin >> H >> W >> D;

  vector<vector<i64>> A(H, vector<i64>(W));

  vector<pair<i64, i64>> pos(100000, make_pair(-1, -1));
  rep(i, 0, H) rep(j, 0, W) {
    cin >> A[i][j];
    pos[A[i][j]] = make_pair(i, j);
  }

  vector<i64> acc(10000);
  rep(i, D, 100000) {
    int y1 = pos[i - D].first, x1 = pos[i - D].second;
    int y2 = pos[i].first, x2 = pos[i].second;
    if (y1 == -1 and x1 == -1)
      continue;
    if (y2 == -1 and x2 == -1)
      continue;
    acc[i] = acc[i - D] + abs(y1 - y2) + abs(x1 - x2);
  }

  int Q;
  cin >> Q;

  rep(i, 0, Q) {
    i64 L, R, ans = 0;
    cin >> L >> R;
    cout << acc[R] - acc[L] << endl;
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}


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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  i64 H, W, D;
  cin >> H >> W >> D;

  vector<vector<i64>> A(H, vector<i64>(W));

  vector<pair<i64, i64>> pos(100000, make_pair(-1, -1));
  rep(i, 0, H) rep(j, 0, W) {
    cin >> A[i][j];
    pos[A[i][j]] = make_pair(i, j);
  }

  vector<i64> acc(100000);
  rep(i, D, 100000) {
    int y1 = pos[i - D].first, x1 = pos[i - D].second;
    int y2 = pos[i].first, x2 = pos[i].second;
    if (y1 == -1 and x1 == -1)
      continue;
    if (y2 == -1 and x2 == -1)
      continue;
    acc[i] = acc[i - D] + abs(y1 - y2) + abs(x1 - x2);
  }

  int Q;
  cin >> Q;

  rep(i, 0, Q) {
    i64 L, R, ans = 0;
    cin >> L >> R;
    cout << acc[R] - acc[L] << endl;
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define abs(x) ((x > 0) ? x : (-(x)))
int H, W, D, A;
int Q, L, R;
int px[9000 + 10], py[9000 + 10];
int d[9000 + 10];
int main() {
  scanf("%d %d %d", &H, &W, &D);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &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]);
  }
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &L, &R);
    printf("%d\n", d[R] - d[L]);
  }
}


Fixed submission:
#include <cstdio>
#define abs(x) ((x > 0) ? x : (-(x)))
int H, W, D, A;
int Q, L, R;
int px[90001], py[90001];
int d[90001];
int main() {
  scanf("%d %d %d", &H, &W, &D);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &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]);
  }
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &L, &R);
    printf("%d\n", d[R] - d[L]);
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
const int H = 301;
const int W = 301;
ll a[H][W];
ll_pair A[H * W];
ll h, w, d, q, l, r, ans = 0;
ll kyori[H * W] = {0};

int main() {
  cin >> h >> w >> d;
  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      cin >> a[i][j];
      A[a[i][j]] = make_pair(i, j);
    }
  }
  FOR(i, 0, d) {
    ll x = i;
    while (x < H * W) {
      ans += abs(A[x].first - A[x + d].first);
      ans += abs(A[x].second - A[x + d].second);
      x += d;
      kyori[x] = ans;
    }
    ans = 0;
  }
  cin >> q;
  FOR(i, 0, q) {
    cin >> l >> r;
    cout << kyori[r] - kyori[l] << endl;
    ans = 0;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
const int H = 301;
const int W = 301;
ll a[H][W];
ll_pair A[H * W];
ll h, w, d, q, l, r, ans = 0;
ll kyori[H * W] = {0};

int main() {
  cin >> h >> w >> d;
  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      cin >> a[i][j];
      A[a[i][j]] = make_pair(i, j);
    }
  }
  FOR(i, 0, d) {
    ll x = i;
    while (x < H * W - d) {
      ans += abs(A[x].first - A[x + d].first);
      ans += abs(A[x].second - A[x + d].second);
      x += d;
      kyori[x] = ans;
    }
    ans = 0;
  }
  cin >> q;
  FOR(i, 0, q) {
    cin >> l >> r;
    cout << kyori[r] - kyori[l] << endl;
    ans = 0;
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):

// D - Practical Skill Test

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

using POS = pair<int, int>;

int A[300][300];
unordered_map<int, POS> n_to_pos;
int cum_pow[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];
      n_to_pos[A[i][j]] = make_pair(i, j);
    }
  }

  // 事前計算
  for (int i = 0; i < D; i++) {
    for (int l = i; l <= H * W; l += D) {
      int r = l + D;

      POS l_pos = n_to_pos[l];
      POS r_pos = n_to_pos[r];

      int pow =
          abs(l_pos.first - r_pos.first) + abs(l_pos.second - r_pos.second);
      cum_pow[r] = cum_pow[l] + pow;
    }
  }

  int Q;
  cin >> Q;

  vector<int> ans;

  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    ans.push_back(cum_pow[R] - cum_pow[L]);
  }

  for (int a : ans) {
    cout << a << endl;
  }

  return 0;
}

Fixed submission:

// D - Practical Skill Test

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

using POS = pair<int, int>;

int A[300][300];
unordered_map<int, POS> n_to_pos;
int cum_pow[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];
      n_to_pos[A[i][j]] = make_pair(i, j);
    }
  }

  // 事前計算
  for (int i = 0; i < D; i++) {
    for (int l = i; l + D <= H * W; l += D) {
      int r = l + D;

      POS l_pos = n_to_pos[l];
      POS r_pos = n_to_pos[r];

      int pow =
          abs(l_pos.first - r_pos.first) + abs(l_pos.second - r_pos.second);
      cum_pow[r] = cum_pow[l] + pow;
    }
  }

  int Q;
  cin >> Q;

  vector<int> ans;

  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    ans.push_back(cum_pow[R] - cum_pow[L]);
  }

  for (int a : ans) {
    cout << a << endl;
  }

  return 0;
}
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):
    for x in range(W):
        d[A[y][x]] = (y, x)

t = [[0] * (H * W // D + 1) for _ in range(D)]
for i in range(D):
    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]
        t[i][j + 1] = abs(y1 - y2) + abs(x1 - x2)
        j += 1
    for j in range(len(H * W // D + 1) - 1):
        t[i][j + 1] += t[i][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):
    for x in range(W):
        d[A[y][x]] = (y, x)

t = [[0] * (H * W // D + 1) for _ in range(D)]
for i in range(D):
    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]
        t[i][j + 1] = abs(y1 - y2) + abs(x1 - x2)
        j += 1
    for j in range(H * W // D):
        t[i][j + 1] += t[i][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 <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int INF = 1e9;

int dis[1000];

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  P mp[h * w + 1];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      int a;
      cin >> a;
      mp[a] = make_pair(i, j);
    }
  }
  for (int i = 1 + d; i <= h * w; i++) {
    dis[i] = abs(mp[i].first - mp[i - d].first) +
             abs(mp[i].second - mp[i - d].second);
    dis[i] += dis[i - d];
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << dis[r] - dis[l] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int INF = 1e9;

int dis[90001];

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  P mp[h * w + 1];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      int a;
      cin >> a;
      mp[a] = make_pair(i, j);
    }
  }
  for (int i = 1 + d; i <= h * w; i++) {
    dis[i] = abs(mp[i].first - mp[i - d].first) +
             abs(mp[i].second - mp[i - d].second);
    dis[i] += dis[i - d];
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << dis[r] - dis[l] << endl;
  }
  return 0;
}
Problem p03426 (C++)

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

using std::cin;
using std::cout;
using std::endl;

using std::pair;
using std::string;

using std::sort;
using std::vector;

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

  vector<int> m(h * w + 1);
  for (int i = 0; i < d; i++) {
    m[i] = 0;
  }
  for (int i = d; i <= h * w; i++) {
    int x1 = a[i].first;
    int y1 = a[i].second;
    int x2 = a[i - d].first;
    int y2 = a[i - d].second;
    int p = std::abs(x1 - x2) + std::abs(y1 - y2);
    m[i] = m[i - d] + p;
  }

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


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

using std::cin;
using std::cout;
using std::endl;

using std::pair;
using std::string;

using std::sort;
using std::vector;

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

  vector<int> m(h * w + 1);
  for (int i = 0; i < d; i++) {
    m[i] = 0;
  }
  for (int i = d; i <= h * w; i++) {
    int x1 = a[i].first;
    int y1 = a[i].second;
    int x2 = a[i - d].first;
    int y2 = a[i - d].second;
    int p = std::abs(x1 - x2) + std::abs(y1 - y2);
    m[i] = m[i - d] + p;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << m[r] - m[l] << endl;
  }
  return 0;
}
Problem p03427 (C++)

Buggy submission (status: Runtime Error):
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pii pair<int, int>
using namespace std;

int h[20], ans = 0;
int dp[20][160][2];

int getsum(int idx, int n, int sum, int flag) {
  ans = max(ans, sum);
  if (idx > n)
    return sum;
  if (dp[idx][sum][flag] != -1)
    return dp[idx][sum][flag];
  int limit = 9, res = 0;
  if (!flag)
    limit = h[idx];
  for (int i = 0; i <= limit; i++) {
    if (i < limit or flag) {
      res = max(res, getsum(idx + 1, n, sum + i, 1));
    } else {
      res = max(res, getsum(idx + 1, n, sum + i, 0));
    }
  }
  dp[idx][sum][flag] = res;
}

inline void solve() {
  ans = 0;
  string s;
  cin >> s;
  for (int i = 0; i < sz(s); i++)
    h[i + 1] = s[i] - '0';
  memset(dp, -1, sizeof(dp));
  getsum(1, sz(s), 0, 0);
  cout << ans << endl;
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}


Fixed submission:
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pii pair<int, int>
using namespace std;

int h[20], ans = 0;
int dp[20][160][2];

int getsum(int idx, int n, int sum, int flag) {
  ans = max(ans, sum);
  if (idx > n)
    return sum;
  if (dp[idx][sum][flag] != -1)
    return dp[idx][sum][flag];
  int limit = 9, res = 0;
  if (!flag)
    limit = h[idx];
  for (int i = 0; i <= limit; i++) {
    if (i < limit or flag) {
      res = max(res, getsum(idx + 1, n, sum + i, 1));
    } else {
      res = max(res, getsum(idx + 1, n, sum + i, 0));
    }
  }
  dp[idx][sum][flag] = res;
  return res;
}

inline void solve() {
  ans = 0;
  string s;
  cin >> s;
  for (int i = 0; i < sz(s); i++)
    h[i + 1] = s[i] - '0';
  memset(dp, -1, sizeof(dp));
  getsum(1, sz(s), 0, 0);
  cout << ans << endl;
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}
Problem p03427 (Python)

Buggy submission (status: Runtime Error):
s = input()

if s < 10:
    print(s)
else:
    print(int(s[0]) - 1 + 9 * (len(s) - 1))


Fixed submission:
s = input()

if int(s) < 10:
    print(int(s))
elif all([x == "9" for x in s[1:]]):
    print(int(s[0]) + 9 * (len(s) - 1))
else:
    print(int(s[0]) - 1 + 9 * (len(s) - 1))
Problem p03427 (Python)

Buggy submission (status: Runtime Error):
n = input()
k = len(n)
c = int(n[0])
if n == c + "9" * (k - 1):
    print(int(n[0]) + 9 * (k - 1))
else:
    print(int(n[0]) + 9 * (k - 1) - 1)


Fixed submission:
n = input()
k = len(n)
c = n[0]
if n == c + "9" * (k - 1):
    print(int(n[0]) + 9 * (k - 1))
else:
    print(int(n[0]) + 9 * (k - 1) - 1)
Problem p03427 (Python)

Buggy submission (status: Runtime Error):
N = str(input())

for item in N[1:]:
    if item != "9":
        print(int(N[0]) - 1 + 9 * (len(N) - 1))
        break
else:
    print(int(N[0]) + 9 * len(N[1]))


Fixed submission:
N = str(input())

for item in N[1:]:
    if item != "9":
        print(int(N[0]) - 1 + 9 * (len(N) - 1))
        break
else:
    print(int(N[0]) + 9 * len(N[1:]))
Problem p03427 (Python)

Buggy submission (status: Runtime Error):
n = input()

lst = list(map(int, list(n)))
print(sum(lst[0], lst[0] - 1 + 9 * (len(lst) - 1)))


Fixed submission:
n = input()

lst = list(map(int, list(n)))
print(max(sum(lst), lst[0] - 1 + 9 * (len(lst) - 1)))
Problem p03427 (Python)

Buggy submission (status: Runtime Error):
N = list(input())
if len(N) == 1:
    print(int(N))
else:
    print((len(N) - 1) * 9 + (int(N[0]) - 1))


Fixed submission:
def main():
    from itertools import dropwhile

    S = input()
    it = dropwhile(lambda char: char == "9", reversed(S))
    # 末尾から見て9以外の数字が現れたら、以降をiterate

    len_ = len(list(it))
    if len_ <= 1:
        print(sum(map(int, S)))
    else:
        head = S[0]
        body = str(int(head) - 1)
        body += "9" * (len(S) - 1)

        print(sum(map(int, body)))


if __name__ == "__main__":
    main()
Problem p03427 (Python)

Buggy submission (status: Runtime Error):
N = int(input())

n = N - int((len(str(N)) - 1) * "9")
tmp = (len(str(N)) - 1) * "9"
if len(str(n)) > (len(str(N)) - 1):
    tmp = str(n)[0] + tmp

res = 0
for t in tmp:
    res += int(t)

print(res)


Fixed submission:
N = int(input())

if N < 10:
    print(N)
    exit()

n = N - int((len(str(N)) - 1) * "9")
tmp = (len(str(N)) - 1) * "9"
if len(str(n)) > (len(str(N)) - 1):
    tmp = str(n)[0] + tmp

res = 0
for t in tmp:
    res += int(t)

print(res)