Problem p03426 (C++)

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

int a[303][303];
int R[303 * 303];
int C[303 * 303];
ll cost[18][303 * 303];

int main2() {
  CLR(cost, 0);

  int H = nextInt();
  int W = nextInt();
  int D = nextInt();
  REP(i, H) REP(j, W) {
    int x = nextInt();
    a[i][j] = x;
    R[x] = i;
    C[x] = j;
  }
  for (int i = 1; i <= H * W; i++) {
    if (i + D <= H * W) {
      cost[0][i] = abs(R[i] - R[i + D]) + abs(C[i] - C[i + D]);
    }
  }

  int d = D;
  for (int t = 1; t < 18; t++) {
    for (int i = 1; i <= H * W; i++) {
      if (i + d + d <= H * W)
        cost[t][i] = cost[t - 1][i] + cost[t - 1][i + d];
    }
    d = 2 * d;
  }

  // for (int t = 0; t < 3; t++) {
  // for (int i = 1; i <= H*W; i++) {
  // cout << cost[t][i] << " " ;
  // }
  // cout << endl;
  // }

  for (int Q = nextInt(); Q-- > 0;) {
    int L = nextInt();
    int R = nextInt();
    int s = (R - L) / D;
    ll ans = 0;
    for (int i = 0; i < 18; i++) {
      if (s >> i & 1) {
        ans += cost[i][L];
        L += (1 << i) * D;
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}


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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

int a[303][303];
int R[303 * 303];
int C[303 * 303];
ll cost[18][303 * 303];

int main2() {
  CLR(cost, 0);

  int H = nextInt();
  int W = nextInt();
  int D = nextInt();
  REP(i, H) REP(j, W) {
    int x = nextInt();
    a[i][j] = x;
    R[x] = i;
    C[x] = j;
  }
  for (int i = 1; i <= H * W; i++) {
    if (i + D <= H * W) {
      cost[0][i] = abs(R[i] - R[i + D]) + abs(C[i] - C[i + D]);
    }
  }

  int d = D;
  for (int t = 1; t < 18; t++) {
    if (1 + d + d > H * W)
      break;
    for (int i = 1; i <= H * W; i++) {
      if (i + d + d <= H * W)
        cost[t][i] = cost[t - 1][i] + cost[t - 1][i + d];
    }
    d = 2 * d;
  }

  // for (int t = 0; t < 3; t++) {
  // for (int i = 1; i <= H*W; i++) {
  // cout << cost[t][i] << " " ;
  // }
  // cout << endl;
  // }

  for (int Q = nextInt(); Q-- > 0;) {
    int L = nextInt();
    int R = nextInt();
    int s = (R - L) / D;
    ll ans = 0;
    for (int i = 0; i < 18; i++) {
      if (s >> i & 1) {
        ans += cost[i][L];
        L += (1 << i) * D;
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}