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

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

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<P>> vec(d, vector<P>((h * w) / d + 1));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      int a;
      cin >> a;
      vec[a % d][a / d] = {i + 1, j + 1};
    }
  }
  vector<vector<int>> sum(d, vector<int>((h * w) / d + 1));

  for (int i = 0; i < d; ++i) {
    for (int j = 1; j < (h * w) / d + 1; ++j) {
      int tmp1 = abs(vec[i][j].first - vec[i][j - 1].first);
      int tmp2 = abs(vec[i][j].second - vec[i][j - 1].second);
      sum[i][j + 1] = sum[i][j] + tmp1 + tmp2;
    }
  }

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

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

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<P>> vec(d, vector<P>((h * w) / d + 1));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      int a;
      cin >> a;
      vec[a % d][a / d] = {i + 1, j + 1};
    }
  }
  vector<vector<int>> sum(d, vector<int>((h * w) / d + 2));

  for (int i = 0; i < d; ++i) {
    for (int j = 1; j < (h * w) / d + 1; ++j) {
      int tmp1 = abs(vec[i][j].first - vec[i][j - 1].first);
      int tmp2 = abs(vec[i][j].second - vec[i][j - 1].second);
      sum[i][j + 1] = sum[i][j] + tmp1 + tmp2;
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << sum[l % d][r / d + 1] - sum[l % d][l / d + 1] << endl;
  }
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> v(300, vector<int>(300));
vector<int> a(901);
int main() {
  int n, m, d;
  cin >> n >> m >> d;
  map<int, pair<int, int>> mp;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      // int x; cin>>x; v[i].push_back(x);
      cin >> v[i][j];
      mp[v[i][j]] = {i + 1, j + 1};
    }
  }
  for (int i = 1; i <= n * m; i++) {
    if (i <= d) {
      a[i] = 0;
      continue;
    }
    int xa = mp[i].first, xb = mp[i - d].first, ya = mp[i].second,
        yb = mp[i - d].second;
    a[i] = a[i - d] + abs(xa - xb) + abs(ya - yb);
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << a[r] - a[l] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> v(300, vector<int>(300));
vector<int> a(90001);
int main() {
  int n, m, d;
  cin >> n >> m >> d;
  map<int, pair<int, int>> mp;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      // int x; cin>>x; v[i].push_back(x);
      cin >> v[i][j];
      mp[v[i][j]] = {i + 1, j + 1};
    }
  }
  for (int i = 1; i <= n * m; i++) {
    if (i <= d) {
      a[i] = 0;
      continue;
    }
    int xa = mp[i].first, xb = mp[i - d].first, ya = mp[i].second,
        yb = mp[i - d].second;
    a[i] = a[i - d] + abs(xa - xb) + abs(ya - yb);
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << a[r] - a[l] << endl;
  }
}
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;
typedef pair<int, int> P;
const int INF = 1001001001;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<P> a(100000);
  rep(i, h) rep(j, w) {
    ll a1;
    cin >> a1;
    a[a1] = make_pair(i, j);
  }
  int q;

  vector<ll> v[d + 5]; // あまりごとに

  for (int i = 0; i < d; i++) {
    ll now = i;
    if (i == 0)
      now = d;
    P pos = a[now];
    v[i].push_back(0);
    ll cnt = 0;
    while (now <= h * w) {
      now += d;
      P posnew = a[now];
      ll ad = abs(posnew.first - pos.first) + abs(posnew.second - pos.second);
      //  cout<<pos.first<<'f'<<pos.second<<"s"<<endl;
      //  cout<<posnew.first<<'f'<<posnew.second<<"s"<<endl;

      //  cout<<ad<<'i'<<i<<"now"<<now<<endl;
      cnt += ad;
      v[i].push_back(cnt);
      pos = a[now];
    }
  }

  cin >> q;
  rep(i, q) {
    int l1, r1;
    cin >> l1 >> r1;

    ll D = l1 % d;
    ll s1 = (l1 - D) / d;
    ll f1 = (r1 - D) / d;
    if (D == 0) {
      s1--;
      f1--;
    }
    // cout<<s1<<"s1"<<f1<<"f1"<<endl;
    ll cnt = v[D][f1] - v[D][s1];
    cout << cnt << 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;
typedef pair<int, int> P;
const int INF = 1001001001;

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<P> a(1000000);
  rep(i, h) rep(j, w) {
    ll a1;
    cin >> a1;
    a[a1] = make_pair(i, j);
  }
  int q;

  vector<ll> v[d + 5]; // あまりごとに

  for (int i = 0; i < d; i++) {
    ll now = i;
    if (i == 0)
      now = d;
    P pos = a[now];
    v[i].push_back(0);
    ll cnt = 0;
    while (now <= h * w) {
      now += d;
      P posnew = a[now];
      ll ad = abs(posnew.first - pos.first) + abs(posnew.second - pos.second);
      //  cout<<pos.first<<'f'<<pos.second<<"s"<<endl;
      //  cout<<posnew.first<<'f'<<posnew.second<<"s"<<endl;

      //  cout<<ad<<'i'<<i<<"now"<<now<<endl;
      cnt += ad;
      v[i].push_back(cnt);
      pos = a[now];
    }
  }

  cin >> q;
  rep(i, q) {
    int l1, r1;
    cin >> l1 >> r1;

    ll D = l1 % d;
    ll s1 = (l1 - D) / d;
    ll f1 = (r1 - D) / d;
    if (D == 0) {
      s1--;
      f1--;
    }
    // cout<<s1<<"s1"<<f1<<"f1"<<endl;
    ll cnt = v[D][f1] - v[D][s1];
    cout << cnt << endl;
  }
  return 0;
}
Problem p03426 (C++)

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

#define LL long long
int h, w, d, a, x[301], y[301], l, r, q, dis[100005];

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

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

  for (int i = d + 1; i <= h * w; i++)
    dis[i] = dis[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d]);

  cin >> q;
  while (q--) {
    cin >> l >> r;
    cout << dis[r] - dis[l] << '\n';
  }
}

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

#define LL long long
int h, w, d, a, x[100005], y[100005], l, r, q, dis[100005];

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

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

  for (int i = d + 1; i <= h * w; i++)
    dis[i] = dis[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d]);

  cin >> q;
  while (q--) {
    cin >> l >> r;
    cout << dis[r] - dis[l] << '\n';
  }
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
// #define ll long long
int h, w, d;
int t[500][500];
int yy[500000];
int xx[500000];
int ans[110000];

int lastl = -1, tans = 0, td = 0;
int che(int l, int r) {
  int y, x, ret, tmp;
  if (l != lastl) {
    y = yy[l], x = xx[l];
    ret = 0;
    tmp = l;
  } else {
    y = yy[td], x = xx[td];
    ret = tans;
    tmp = td;
  }
  // cout<<y<<" "<<x<<endl;
  while (t[y][x] != r) {
    tmp += d;
    // cout<<y<<" "<<x<<" "<<tmp<<endl;

    int nx = xx[tmp], ny = yy[tmp];
    ret += (abs(ny - y) + abs(nx - x));
    x = nx, y = ny;
  }

  td = r;
  tans = ret;

  return ret;
}

pair<P, int> X[200000];

int main() {
  cin >> h >> w >> d;
  rep(i, h) {
    rep(j, w) {
      cin >> t[i][j];
      yy[t[i][j]] = i;
      xx[t[i][j]] = j;
    }
  }
  int q, l, r, num;

  cin >> q;
  rep(i, q) {
    cin >> l >> r;
    // cout<<che(l,r)<<endl;
    X[i] = mp(mp(l, r), i);
  }
  sort(X, X + q);
  rep(i, q) {
    l = X[i].first.first, r = X[i].first.second, num = X[i].second;
    int ret = 0;
    ret = che(l, r);
    ans[num] = ret;
  }

  rep(i, q) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
// #define ll long long
int h, w, d;
int t[500][500];
int yy[500000];
int xx[500000];
int ans[110000];

int lastl = -1, tans = 0, td = 0;
int che(int l, int r) {
  int y, x, ret, tmp;
  if (l != lastl) {
    y = yy[l], x = xx[l];
    ret = 0;
    tmp = l;
  } else {
    y = yy[td], x = xx[td];
    ret = tans;
    tmp = td;
  }
  // cout<<y<<" "<<x<<endl;
  while (t[y][x] != r) {
    tmp += d;
    // cout<<y<<" "<<x<<" "<<tmp<<endl;

    int nx = xx[tmp], ny = yy[tmp];
    ret += (abs(ny - y) + abs(nx - x));
    x = nx, y = ny;
  }

  td = r;
  tans = ret;
  lastl = l;
  return ret;
}

pair<P, int> X[200000];

int main() {
  cin >> h >> w >> d;
  rep(i, h) {
    rep(j, w) {
      cin >> t[i][j];
      yy[t[i][j]] = i;
      xx[t[i][j]] = j;
    }
  }
  int q, l, r, num;

  cin >> q;
  rep(i, q) {
    cin >> l >> r;
    // cout<<che(l,r)<<endl;
    X[i] = mp(mp(l, r), i);
  }
  sort(X, X + q);
  rep(i, q) {
    l = X[i].first.first, r = X[i].first.second, num = X[i].second;
    int ret = 0;
    ret = che(l, r);
    ans[num] = ret;
  }

  rep(i, q) cout << ans[i] << endl;
  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define ssort(n) sort((n).begin(), (n).end())
#define rsort(n) sort((n).begin(), (n).end(), greater<int>())
#define mp make_pair
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
typedef pair<P, int> COST;
#define repl(i, n) for (ll i = 0; i < (n); ++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl

using Graf_char = vector<vector<char>>;
using Graf_int = vector<vector<int>>;
#define MAX 10000000

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

  int q;
  cin >> q;
  vector<P> task(q);
  rep(i, q) { cin >> task[i].first >> task[i].second; }
  vector<ll> power(q);
  int val;
  rep(k, q) {
    val = task[k].first;
    while (val != task[k].second) {
      int i = a[val].first;
      int j = a[val].second;
      int x = a[val + d].first;
      int y = a[val + d].second;
      power[k] += (abs(x - i) + abs(y - j));
      val = val + d;
    }
  }
  rep(i, q) { cout << power[i] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define ssort(n) sort((n).begin(), (n).end())
#define rsort(n) sort((n).begin(), (n).end(), greater<int>())
#define mp make_pair
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
typedef pair<P, int> COST;
#define repl(i, n) for (ll i = 0; i < (n); ++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl

using Graf_char = vector<vector<char>>;
using Graf_int = vector<vector<int>>;
#define MAX 10000000

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

  int q;
  cin >> q;
  vector<P> task(q);
  rep(i, q) { cin >> task[i].first >> task[i].second; }
  vector<ll> power(q);
  int val, val_r;
  vector<ll> dist(h * w + 1, 0);
  rep(i, d + 1) { dist[i] = 0; }
  FOR(i, h * w + 1, d + 1) {
    int k = a[i - d].first;
    int l = a[i - d].second;
    int x = a[i].first;
    int y = a[i].second;
    dist[i] = dist[i - d] + (abs(x - k) + abs(y - l));
  }
  rep(i, q) {
    val = task[i].first;
    val_r = task[i].second;
    power[i] = dist[val_r] - dist[val];
  }
  rep(i, q) { cout << power[i] << endl; }
}
Problem p03426 (C++)

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int H, W, D;
int A[310][310];
int Q;
int L[100010], R[100010];
pii pos[910];
int cost[910];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> H >> W >> D;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      pos[A[i][j]] = pii(i, j);
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
  }
  for (int i = 1; i + D <= H * W; i++) {
    cost[i + D] = abs(pos[i + D].first - pos[i].first) +
                  abs(pos[i + D].second - pos[i].second);
    cost[i + D] += cost[i];
  }
  for (int i = 0; i < Q; i++) {
    cout << cost[R[i]] - cost[L[i]] << endl;
  }

  return 0;
}

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int H, W, D;
int A[310][310];
int Q;
int L[100010], R[100010];
pii pos[90010];
int cost[90010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> H >> W >> D;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      pos[A[i][j]] = pii(i, j);
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
  }
  for (int i = 1; i + D <= H * W; i++) {
    cost[i + D] = abs(pos[i + D].first - pos[i].first) +
                  abs(pos[i + D].second - pos[i].second);
    cost[i + D] += cost[i];
  }
  for (int i = 0; i < Q; i++) {
    cout << cost[R[i]] - cost[L[i]] << endl;
  }

  return 0;
}
Problem p03426 (C++)

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

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

int main() {
  // マスに入れる数字を0~h*w-1と思う
  int h, w, d;
  cin >> h >> w >> d;
  vector<pair<int, int>> b(h * w);
  int ta;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> ta;
      b[ta - 1] = make_pair(i, j);
    }
  }
  int q;
  cin >> q;
  vector<pair<int, int>> query(q);
  int tl, tr;
  for (int i = 0; i < q; i++) {
    cin >> tl >> tr;
    query[i] = make_pair(tl - 1, tr - 1);
  }
  // mp[i][j]:iからj回移動するのに掛かる魔力
  vector<vector<int>> mp(d, vector<int>(h * w / d, 0));
  for (int i = 0; i < d; i++) {
    for (int j = 1; i + j * d < h * w; j++) {
      mp[i][j] = mp[i][j - 1] + md(b[i + d * (j - 1)], b[i + d * j]);
    }
  }
  int start;
  int step1, step2;
  for (int i = 0; i < q; i++) {
    start = query[i].first % d;
    step1 = query[i].first / d;
    step2 = query[i].second / d;
    cout << mp[start][step2] - mp[start][step1] << endl;
  }
}

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

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

int main() {
  // マスに入れる数字を0~h*w-1と思う
  int h, w, d;
  cin >> h >> w >> d;
  vector<pair<int, int>> b(h * w);
  int ta;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> ta;
      b[ta - 1] = make_pair(i, j);
    }
  }
  int q;
  cin >> q;
  vector<pair<int, int>> query(q);
  int tl, tr;
  for (int i = 0; i < q; i++) {
    cin >> tl >> tr;
    query[i] = make_pair(tl - 1, tr - 1);
  }
  // mp[i][j]:iからj回移動するのに掛かる魔力
  vector<vector<int>> mp(d, vector<int>(h * w / d + 1, 0));
  for (int i = 0; i < d; i++) {
    for (int j = 1; i + j * d < h * w; j++) {
      mp[i][j] = mp[i][j - 1] + md(b[i + d * (j - 1)], b[i + d * j]);
    }
  }
  int start;
  int step1, step2;
  for (int i = 0; i < q; i++) {
    start = query[i].first % d;
    step1 = query[i].first / d;
    step2 = query[i].second / d;
    cout << mp[start][step2] - mp[start][step1] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;
const int MOD = 1000000007;
const int INF = LLONG_MAX / 3;
const int MAX = 510000;
signed main() {
  int h, w, d;
  cin >> h >> w >> d;
  P p[100000];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int k;
      cin >> k;
      p[k - 1] = P(i, j);
    }
  }
  vector<int> a[10000];
  for (int i = 0; i < d; i++) {
    int c = i;
    int j = 0;
    while (1) {
      if (c > h * w)
        break;
      if ((j == 0 && i >= 1) || (i == 0 && j <= 1))
        a[i].push_back(0);
      else {
        a[i].push_back(a[i][j - 1] + abs(p[c - 1].first - p[c - d - 1].first) +
                       abs(p[c - 1].second - p[c - d - 1].second));
      }
      c += d;
      j++;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    int t = l % d;
    cout << a[t][r / d] - a[t][l / d] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;
const int MOD = 1000000007;
const int INF = LLONG_MAX / 3;
const int MAX = 510000;
signed main() {
  int h, w, d;
  cin >> h >> w >> d;
  P p[100000];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int k;
      cin >> k;
      p[k - 1] = P(i, j);
    }
  }
  vector<int> a[100000];
  for (int i = 0; i < d; i++) {
    int c = i;
    int j = 0;
    while (1) {
      if (c > h * w)
        break;
      if ((j == 0 && i >= 1) || (i == 0 && j <= 1))
        a[i].push_back(0);
      else {
        a[i].push_back(a[i][j - 1] + abs(p[c - 1].first - p[c - d - 1].first) +
                       abs(p[c - 1].second - p[c - d - 1].second));
      }
      c += d;
      j++;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    int t = l % d;
    cout << a[t][r / d] - a[t][l / d] << endl;
  }
  return 0;
}
Problem p03426 (C++)

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

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;
  vector<int> ax(h * w + 1), ay(h * w + 1);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      int tmp;
      cin >> tmp;
      ax[tmp] = i;
      ay[tmp] = j;
    }
  }
  vector<int> bx(h * w + 1 + d, 0), by(h * w + 1 + d, 0);
  for (int i = 0; i < d; ++i) {
    for (int j = i; j < h * w; j += d) {
      bx[j + d] = bx[j] + abs(ax[j] - ax[j + d]);
      by[j + d] = by[j] + abs(ay[j] - ay[j + d]);
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << (by[r] - by[l]) + (bx[r] - bx[l]) << endl;
  }
  return 0;
}


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

int main(void) {
  int h, w, d;
  cin >> h >> w >> d;
  vector<int> ax(h * w + 1 + d), ay(h * w + 1 + d);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      int tmp;
      cin >> tmp;
      ax[tmp] = i;
      ay[tmp] = j;
    }
  }
  vector<int> bx(h * w + 1 + d, 0), by(h * w + 1 + d, 0);
  for (int i = 0; i < d; ++i) {
    for (int j = i; j < h * w; j += d) {
      bx[j + d] = bx[j] + abs(ax[j] - ax[j + d]);
      by[j + d] = by[j] + abs(ay[j] - ay[j + d]);
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << (by[r] - by[l]) + (bx[r] - bx[l]) << endl;
  }
  return 0;
}
Problem p03426 (C++)

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

int main() {
  typedef pair<int, int> P;
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<P>> tree(h * w + 1);
  vector<P> start(d + 1);
  int x;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> x;
      if (x > d) {
        tree[x - d].push_back({i, j});
      } else {
        start[x] = {i, j};
      }
    }
  }
  vector<int> distance(h * w + 1);
  for (int i = 1; i <= d; i++) {
    distance[i] = 0;
    distance[i + d] = abs(start[i].first - tree[i][0].first) +
                      abs(start[i].second - tree[i][0].second);
    for (int j = i + d; j + d <= h * w; j += d) {
      distance[j + d] = distance[j] +
                        abs(tree[j - d][0].first - tree[j][0].first) +
                        abs(tree[j - d][0].second - tree[j][0].second);
      ;
    }
  }

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


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

int main() {
  typedef pair<int, int> P;
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<P>> tree(h * w + 1);
  vector<P> start(d + 1);
  int x;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> x;
      if (x > d) {
        tree[x - d].push_back({i, j});
      } else {
        start[x] = {i, j};
      }
    }
  }
  vector<int> distance(h * w + 1);
  for (int i = 1; i <= d; i++) {
    distance[i] = 0;
    if (i + d <= h * w)
      distance[i + d] = abs(start[i].first - tree[i][0].first) +
                        abs(start[i].second - tree[i][0].second);
    for (int j = i + d; j + d <= h * w; j += d) {
      distance[j + d] = distance[j] +
                        abs(tree[j - d][0].first - tree[j][0].first) +
                        abs(tree[j - d][0].second - tree[j][0].second);
      ;
    }
  }

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

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

using namespace std;
int h, w;
int sum[520], mp[250005];
int abs(int x) {
  if (x > 0)
    return x;
  else
    return -x;
}
void solve(int x, int d) {
  int y, a, b, pa, pb;
  y = mp[x];
  pa = y / 500;
  pb = y % 500;
  for (int i = x; i <= h * w; i += d) {
    y = mp[i];
    a = y / 500;
    b = y % 500;
    sum[i] = sum[i - d] + abs(pa - a) + abs(pb - b);
    pa = a;
    pb = b;
  }
}
int main() {
  int d, x;
  scanf("%d%d%d", &h, &w, &d);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf("%d", &x);
      mp[x] = i * 500 + j;
    }
  }
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= d; i++) {
    solve(i, d);
  }
  int q, l, r;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &l, &r);
    // cout<<sum[r]<<" "<<sum[l]<<endl;
    printf("%d\n", sum[r] - sum[l]);
  }
  return 0;
}

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

using namespace std;
int h, w;
int sum[250005], mp[250005];
int abs(int x) {
  if (x > 0)
    return x;
  else
    return -x;
}
void solve(int x, int d) {
  int y, a, b, pa, pb;
  y = mp[x];
  pa = y / 500;
  pb = y % 500;
  for (int i = x; i <= h * w; i += d) {
    y = mp[i];
    a = y / 500;
    b = y % 500;
    sum[i] = sum[i - d] + abs(pa - a) + abs(pb - b);
    pa = a;
    pb = b;
  }
}
int main() {
  int d, x;
  scanf("%d%d%d", &h, &w, &d);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf("%d", &x);
      mp[x] = i * 500 + j;
    }
  }
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= d; i++) {
    solve(i, d);
  }
  int q, l, r;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &l, &r);
    // cout<<sum[r]<<" "<<sum[l]<<endl;
    printf("%d\n", sum[r] - sum[l]);
  }
  return 0;
}
Problem p03426 (C++)

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

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

  vector<pair<int, int>> a(H * W + 1);
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
      a[A[i][j]] = make_pair(i, j);

  for (int i = 1; i <= Q; ++i) {
    int ans = 0;
    for (int x = L[i]; x < R[i]; x += D)
      ans +=
          abs(a[x + D].first - a[x].first) + abs(a[x + D].second - a[x].second);
    cout << ans << endl;
  }
}

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

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

  vector<pair<int, int>> a(H * W + 1);
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
      a[A[i][j]] = make_pair(i, j);

  vector<int> sum(H * W + 1);
  for (int i = 0; i < D; ++i)
    for (int j = i + D; j <= H * W; j += D)
      sum[j] += sum[j - D] + abs(a[j].first - a[j - D].first) +
                abs(a[j].second - a[j - D].second);

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
  int t[1000];
  fill(t, t + 1000, -1);
  int h, w, d;
  int q;
  map<int, pair<int, int>> mp;
  cin >> h >> w >> d;
  imfor(i, h) {
    imfor(j, w) {
      int a;
      cin >> a;
      mp[a] = make_pair(i, j);
    }
  }
  imfor(i, h * w) {
    if (i == 0) {
      continue;
    }
    if (t[i] != -1) {
      continue;
    }
    int j = i;
    while (j <= h * w) {
      if (j - d < 1) {
        t[j] = 0;
      } else {
        t[j] = t[j - d];
        t[j] += abs(mp[j].first - mp[j - d].first) +
                abs(mp[j].second - mp[j - d].second);
      }
      j += d;
    }
  }
  // imfor(i, h * w) {
  //     cout << t[i] << " ";
  // }
  // cout << endl;
  // return 1;

  cin >> q;
  imfor(i, q) {
    int ans = 0;
    int l, r;
    cin >> l >> r;
    cout << t[r] - t[l] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
  int t[100000];
  fill(t, t + 100000, -1);
  int h, w, d;
  int q;
  map<int, pair<int, int>> mp;
  cin >> h >> w >> d;
  imfor(i, h) {
    imfor(j, w) {
      int a;
      cin >> a;
      mp[a] = make_pair(i, j);
    }
  }
  imfor(i, h * w) {
    if (i == 0) {
      continue;
    }
    if (t[i] != -1) {
      continue;
    }
    int j = i;
    while (j <= h * w) {
      if (j - d < 1) {
        t[j] = 0;
      } else {
        t[j] = t[j - d];
        t[j] += abs(mp[j].first - mp[j - d].first) +
                abs(mp[j].second - mp[j - d].second);
      }
      j += d;
    }
  }
  // imfor(i, h * w) {
  //     cout << t[i] << " ";
  // }
  // cout << endl;
  // return 1;

  cin >> q;
  imfor(i, q) {
    int ans = 0;
    int l, r;
    cin >> l >> r;
    cout << t[r] - t[l] << endl;
  }
}
Problem p03426 (C++)

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

using namespace std;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<pair<int, int>> A(H * W + 1);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int a;
      cin >> a;
      A[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<long long> C(H * W + D + 1);
  for (int i = 0; i <= H * W; i++) {
    C[i + D] = abs(A[i].first - A[i + D].first) +
               abs(A[i].second - A[i + D].second) + C[i];
  }
  for (int i = 0; i < Q; i++) {
    cout << C[R[i]] - C[L[i]] << endl;
  }

  return 0;
}
/* vim:set fdm=marker: */


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

using namespace std;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<pair<int, int>> A(H * W + D + 1);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int a;
      cin >> a;
      A[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<long long> C(H * W + D + 1);
  for (int i = 0; i <= H * W; i++) {
    C[i + D] = abs(A[i].first - A[i + D].first) +
               abs(A[i].second - A[i + D].second) + C[i];
  }
  for (int i = 0; i < Q; i++) {
    cout << C[R[i]] - C[L[i]] << endl;
  }

  return 0;
}
/* vim:set fdm=marker: */
Problem p03426 (C++)

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

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define Decimal fixed << setpresicion(20)

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

int dist[110000];
pair<int, int> pos[110000];

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  REP(i, H) {
    REP(j, W) {
      int A;
      cin >> A;
      pos[A].FI = i;
      pos[A].SE = j;
    }
  }

  REP(i, H * W) {
    dist[i + D] = dist[i] + abs(pos[i + D].FI - pos[i].FI) +
                  abs(pos[i + D].SE - pos[i].SE);
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int L, R;
    cin >> L >> R;
    cout << dist[R] - dist[L] << endl;
  }

  return 0;
}


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

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define Decimal fixed << setpresicion(20)

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

int dist[1100000];
pair<int, int> pos[1100000];

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  REP(i, H) {
    REP(j, W) {
      int A;
      cin >> A;
      pos[A].FI = i;
      pos[A].SE = j;
    }
  }

  REP(i, H * W) {
    dist[i + D] = dist[i] + abs(pos[i + D].FI - pos[i].FI) +
                  abs(pos[i + D].SE - pos[i].SE);
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int L, R;
    cin >> L >> R;
    cout << dist[R] - dist[L] << endl;
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

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

  vector<P> A(H * W + 1);
  rep(i, H) {
    rep(j, W) {
      int a;
      cin >> a;
      A[a] = make_pair(i, j);
    }
  }

  vector<vector<int>> S(D, vector<int>(H * W));
  rep(i, D) {
    int k = 0;
    int offset = 0;
    if (i == 0)
      offset = D;
    for (int j = i + offset; j + D <= H * W; j += D) {
      P p1 = A[j];
      P p2 = A[j + D];
      S[i][k + 1] =
          S[i][k] + abs(p1.first - p2.first) + abs(p1.second - p2.second);
      k++;
    }
  }

  //    rep(i, D) {
  //        rep(j, H + W) {
  //            cout << "(" << i << "," << j << ") " << S[i][j] << " : ";
  //        }
  //        cout << endl;
  //    }

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

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

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

  vector<P> A(H * W + 1);
  rep(i, H) {
    rep(j, W) {
      int a;
      cin >> a;
      A[a] = make_pair(i, j);
    }
  }

  vector<vector<int>> S(D, vector<int>(((H + 1) * (W + 1)) / D));
  rep(i, D) {
    int k = 0;
    int offset = 0;
    if (i == 0)
      offset = D;
    for (int j = i + offset; j + D <= H * W; j += D) {
      P p1 = A[j];
      P p2 = A[j + D];
      S[i][k + 1] =
          S[i][k] + abs(p1.first - p2.first) + abs(p1.second - p2.second);
      k++;
    }
  }

  //    rep(i, D) {
  //        rep(j, H + W) {
  //            cout << "(" << i << "," << j << ") " << S[i][j] << " : ";
  //        }
  //        cout << endl;
  //    }

  int Q;
  cin >> Q;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    int d = l % D;
    if (d == 0) {
      r -= D;
      l -= D;
    }
    cout << S[d][r / D] - S[d][l / D] << endl;
  }
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define int long long
#define mod(int) (1000000007)
using namespace std;
int test[213456][2], maryoku[213456];
signed main() {
  int n, w, d, a, q, l, r;
  cin >> n >> w >> d;
  for (int h = 0; h < n; h++) {
    for (int i = 0; i < w; i++) {
      cin >> a;
      test[a][0] = h;
      test[a][1] = i;
    }
  }
  for (int h = 1; h <= n * w - d; h++) {
    int sx = 0, sy = 0, gx = 0, gy = 0;
    sx = test[h][0];
    sy = test[h][1];
    gx = test[h + d][0];
    gy = test[h + d][1];
    if (h <= d) {
      maryoku[h] = abs(gx - sx) + abs(gy - sy);
    } else {
      maryoku[h] = maryoku[h - d] + abs(gx - sx) + abs(gy - sy);
    }
  }
  cin >> q;
  for (int h = 0; h < q; h++) {
    cin >> l >> r;
    cout << maryoku[r - d] - maryoku[l - d] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define int long long
#define mod(int) (1000000007)
using namespace std;
int test[213456][2], maryoku[213456];
signed main() {
  int n, w, d, a, q, l, r;
  cin >> n >> w >> d;
  for (int h = 0; h < n; h++) {
    for (int i = 0; i < w; i++) {
      cin >> a;
      test[a][0] = h;
      test[a][1] = i;
    }
  }
  for (int h = 1; h <= n * w - d; h++) {
    int sx = 0, sy = 0, gx = 0, gy = 0;
    sx = test[h][0];
    sy = test[h][1];
    gx = test[h + d][0];
    gy = test[h + d][1];
    if (h <= d) {
      maryoku[h] = abs(gx - sx) + abs(gy - sy);
    } else {
      maryoku[h] = maryoku[h - d] + abs(gx - sx) + abs(gy - sy);
    }
  }
  cin >> q;
  for (int h = 0; h < q; h++) {
    cin >> l >> r;
    if (l <= d) {
      l = d;
    }
    if (r <= d) {
      r = d;
    }
    cout << maryoku[r - d] - maryoku[l - d] << endl;
  }
}
Problem p03426 (Python)

Buggy submission (status: Runtime Error):
# https://atcoder.jp/contests/abc089/tasks/abc089_d

# queryがたくさんある系
# query一つにつきO(1)ぐらいで応答しないと行けない→前処理の必要あり

# あるマスについてはD倍したマスだけ把握しておけばいいんじゃない？
"""
D=2
1 4 3
2 5 7
8 9 6
"""
# だったらstart=1,2において、移動のスコアの累積和を入れていく
# Qに回答時は、L%D番目のstartの累積和において、L//Dにおいて累積和の差を求めれば良い
# ピッタリDだった場合に面倒なので-0.5して置くのは良いかもしれない

from collections import defaultdict
from itertools import accumulate
import sys

read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_matrix(H):
    """
    H is number of rows
    """
    return [list(map(int, read().split())) for _ in range(H)]


def get_cost(i, j, x, y):
    return abs(x - i) + abs(y - j)


H, W, D = read_ints()
A = read_matrix(H)
ruiseki = defaultdict(lambda: [])
num_to_co = {}

for i, a in enumerate(A):
    for j, aa in enumerate(a):
        num_to_co[aa] = (i, j)

for num in range(1, D + 1):
    pre_i, pre_j = num_to_co[num]
    pre_num = num
    ruiseki[num].append(0)
    while W * H >= pre_num + D:
        now_num = pre_num + D
        i, j = num_to_co[now_num]
        cost = get_cost(pre_i, pre_j, i, j)
        ruiseki[num].append(cost)
        # 必要なもの、前のi,j今のi,j 今の番号
        # 更新するもの 前のi,j←今のi,j 今の番号を次の番号に
        pre_i, pre_j = i, j
        pre_num = now_num

    ruiseki[num] = list(accumulate(ruiseki[num]))
# print(ruiseki)

# Qに答える番だ
Q = int(input())
for _ in range(Q):
    L, R = read_ints()
    tmp = ruiseki[L % D if L % D != 0 else (L // D)]
    # print(tmp)
    # print((R - 1) // D, (L - 1) // D)
    print(tmp[(R - 1) // D] - tmp[(L - 1) // D])


Fixed submission:
# https://atcoder.jp/contests/abc089/tasks/abc089_d

# queryがたくさんある系
# query一つにつきO(1)ぐらいで応答しないと行けない→前処理の必要あり

# あるマスについてはD倍したマスだけ把握しておけばいいんじゃない？
"""
D=2
1 4 3
2 5 7
8 9 6
"""
# だったらstart=1,2において、移動のスコアの累積和を入れていく
# Qに回答時は、L%D番目のstartの累積和において、L//Dにおいて累積和の差を求めれば良い
# ピッタリDだった場合に面倒なので-0.5して置くのは良いかもしれない

from collections import defaultdict
from itertools import accumulate
import sys

read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_matrix(H):
    """
    H is number of rows
    """
    return [list(map(int, read().split())) for _ in range(H)]


def get_cost(i, j, x, y):
    return abs(x - i) + abs(y - j)


H, W, D = read_ints()
A = read_matrix(H)
ruiseki = defaultdict(lambda: [])
num_to_co = {}

for i, a in enumerate(A):
    for j, aa in enumerate(a):
        num_to_co[aa] = (i, j)

for num in range(1, D + 1):
    pre_i, pre_j = num_to_co[num]
    pre_num = num
    ruiseki[num].append(0)
    while W * H >= pre_num + D:
        now_num = pre_num + D
        i, j = num_to_co[now_num]
        cost = get_cost(pre_i, pre_j, i, j)
        ruiseki[num].append(cost)
        # 必要なもの、前のi,j今のi,j 今の番号
        # 更新するもの 前のi,j←今のi,j 今の番号を次の番号に
        pre_i, pre_j = i, j
        pre_num = now_num

    ruiseki[num] = list(accumulate(ruiseki[num]))
# print(ruiseki)

# Qに答える番だ
Q = int(input())
for _ in range(Q):
    L, R = read_ints()
    tmp = ruiseki[L % D if L % D != 0 else D]
    # print(tmp)
    # print((R - 1) // D, (L - 1) // D)
    print(tmp[(R - 1) // D] - tmp[(L - 1) // D])
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
using namespace std;
int H, W, D, Q, Ax[301], Ay[301], sum[90001];
int main() {
  scanf("%d %d %d", &H, &W, &D);
  for (int i = 1; i <= H; ++i) {
    for (int j = 1, t; j <= W; ++j) {
      scanf("%d", &t);
      Ax[t] = i, Ay[t] = j;
    }
  }
  for (int i = 1; i <= H * W; ++i) {
    if (i <= D)
      continue;
    sum[i] = sum[i - D] + abs(Ax[i] - Ax[i - D]) + abs(Ay[i] - Ay[i - D]);
  }
  scanf("%d", &Q);
  for (int i = 1, L, R; i <= Q; ++i) {
    scanf("%d %d", &L, &R);
    printf("%d\n", sum[R] - sum[L]);
  }
}

Fixed submission:
#include <cmath>
#include <cstdio>
using namespace std;
const int M = 90001;
int H, W, D, Q, Ax[M], Ay[M], sum[M];
int main() {
  scanf("%d %d %d", &H, &W, &D);
  for (int i = 1; i <= H; ++i) {
    for (int j = 1, t; j <= W; ++j) {
      scanf("%d", &t);
      Ax[t] = i, Ay[t] = j;
    }
  }
  for (int i = 1; i <= H * W; ++i) {
    if (i <= D)
      continue;
    sum[i] = sum[i - D] + abs(Ax[i] - Ax[i - D]) + abs(Ay[i] - Ay[i - D]);
  }
  scanf("%d", &Q);
  for (int i = 1, L, R; i <= Q; ++i) {
    scanf("%d %d", &L, &R);
    printf("%d\n", sum[R] - sum[L]);
  }
}
Problem p03426 (C++)

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

using namespace std;
typedef long long ll;

const ll INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<pair<int, int>> list(H * W, pair<int, int>());
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int d;
      cin >> d;
      list[d] = make_pair(i, j);
    }
  }

  vector<ll> rw(H * W + 1, 0);
  for (int i = 1; i <= H * W - D; i++) {
    rw[i + D] = rw[i] + abs(list[i + D].first - list[i].first) +
                abs(list[i + D].second - list[i].second);
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int l, r;
    cin >> l >> r;
    cout << rw[r] - rw[l] << endl;
  }

  return 0;
}


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

using namespace std;
typedef long long ll;

const ll INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  vector<pair<int, int>> list(H * W + 1, pair<int, int>());
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int d;
      cin >> d;
      list[d] = make_pair(i, j);
    }
  }

  vector<ll> rw(H * W + 1, 0);
  for (int i = 1; i <= H * W - D; i++) {
    rw[i + D] = rw[i] + abs(list[i + D].first - list[i].first) +
                abs(list[i + D].second - list[i].second);
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int l, r;
    cin >> l >> r;
    cout << rw[r] - rw[l] << endl;
  }

  return 0;
}
Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define MOD 1000000007

const int MA = 90001;
int px[MA], py[MA];
int d[MA];

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 = H * W; i > 0; i--) {
    if (i + D > H * W)
      d[i] = 0;
    d[i] = d[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;
    cout << d[l] - d[r] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define MOD 1000000007

const int MA = 90001;
int px[MA], py[MA];
int d[MA];

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 = H * W - D; i > 0; i--) {
    d[i] = d[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;
    cout << d[l] - d[r] << endl;
  }
  return 0;
}
Problem p03426 (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
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  map<int, pair<int, int>> a;
  vector<vector<int>> cost(H * W + 1, vector<int>(H * W * 1, 0));

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int A;
      cin >> A;
      a[A] = {i, j};
    }
  }

  vector<int> d(H * W + 5, 0);
  for (int i = D + 1; i <= H * W; i++) {
    d[i] = d[i - D] + abs(a[i].first - a[i - D].first) +
           abs(a[i].second - a[i - D].second);
  }

  int q;
  cin >> q;
  vector<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;
  }
}

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
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

int main() {
  int H, W, D;
  cin >> H >> W >> D;
  map<int, pair<int, int>> a;

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int A;
      cin >> A;
      a[A] = {i, j};
    }
  }

  vector<int> d(H * W + 5, 0);
  for (int i = D + 1; i <= H * W; i++) {
    d[i] = d[i - D] + abs(a[i].first - a[i - D].first) +
           abs(a[i].second - a[i - D].second);
  }

  int q;
  cin >> q;
  vector<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;
  }
}
Problem p03426 (C++)

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

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


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

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