Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define ALL(a) a.begin(), a.end()
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF 2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define M_PI 3.14159265358979323846
typedef long long ll;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
int main() {
  int H, W, D;
  cin >> H >> W >> D;
  map<int, int> mp;
  REP(i, H) {
    REP(j, W) {
      int a;
      cin >> a;
      mp[a] = i * 1000 + j;
    }
  }

  vector<vector<int>> sum(D, vector<int>(H * W));
  REP(i, D) {
    int cnt = 0;
    int prev = mp[i];
    sum[i][0] = 0;
    for (int mas = i + D; mas <= H * W; mas += D) {
      int now = mp[mas];
      int one = 0;
      one += abs(now % 1000 - prev % 1000);
      one += abs(now / 1000 - prev / 1000);
      sum[i][cnt + 1] = sum[i][cnt] + one;
      cnt++;
      prev = now;
    }
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int L, R;
    cin >> L >> R;
    int ans = sum[R % D][R / D] - sum[L % D][L / D];
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define ALL(a) a.begin(), a.end()
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF 2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define M_PI 3.14159265358979323846
typedef long long ll;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
int main() {
  int H, W, D;
  cin >> H >> W >> D;
  map<int, int> mp;
  REP(i, H) {
    REP(j, W) {
      int a;
      cin >> a;
      mp[a] = i * 1000 + j;
    }
  }

  vector<vector<int>> sum(D, vector<int>(H * W / D + 1));
  REP(i, D) {
    int cnt = 0;
    int prev = mp[i];
    sum[i][0] = 0;
    for (int mas = i + D; mas <= H * W; mas += D) {
      int now = mp[mas];
      int one = 0;
      one += abs(now % 1000 - prev % 1000);
      one += abs(now / 1000 - prev / 1000);
      sum[i][cnt + 1] = sum[i][cnt] + one;
      cnt++;
      prev = now;
    }
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int L, R;
    cin >> L >> R;
    int ans = sum[R % D][R / D] - sum[L % D][L / D];
    cout << ans << endl;
  }

  return 0;
}