Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

#define rep(i, a) for (int i = 0; i < a; i++)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define loop3(i, j, k, a)                                                      \
  for (int i = 0; i < a; i++)                                                  \
    for (int j = 0; j < a; j++)                                                \
      if (i != j)                                                              \
        for (int k = 0; k < a; k++)                                            \
          if (i != k && j != k)
#define loop4(i, j, k, l, a)                                                   \
  for (int i = 0; i < a; i++)                                                  \
    for (int j = 0; j < a; j++)                                                \
      if (i != j)                                                              \
        for (int k = 0; k < a; k++)                                            \
          if (i != k && j != k)                                                \
            for (int l = 0; l < a; l++)                                        \
              if (i != l && j != l && k != l)
#define rep1(i, a) for (int i = 1; i <= a; i++)

#define scnd1(a) scanf("%d", &a)
#define scnd2(a, b) scanf("%d%d", &a, &b)
#define scnd3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scnd4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)

#define cin1(a) cin >> a;
#define cin2(a, b) cin >> a >> b;
#define cin3(a, b, c) cin >> a >> b >> c;
#define cin4(a, b, c, d) cin >> a >> b >> c >> d;
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define prtd1(a) printf("%d\n", a)
#define prtd2(a, b) printf("%d %d\n", a, b)
#define prtd3(a, b, c) printf("%d %d %d\n", a, b, c)
#define prtd4(a, b, c, d) printf("%d %d %d %d\n", a, b, c, d)

#define mem(a, n) memset(a, n, sizeof(a))
typedef long long ll;

pair<int, int> nums[1000];
int cost[100000];

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

  int n;
  rep1(i, H) {
    rep1(j, W) {
      cin >> n;
      nums[n] = {i, j};
    }
  }

  mem(cost, 0);

  for (int i = 1; i < 1 + D; i++) {
    for (int u = i; u + D <= H * W; u += D) {
      cost[u + D] = abs(nums[u + D].first - nums[u].first) +
                    abs(nums[u + D].second - nums[u].second);
      cost[u + D] += cost[u];
      // cout4( u, cost[u],u+D,cost[u+D]);
    }
  }

  int Q, s, t;
  cin >> Q;
  rep(i, Q) {
    cin >> s >> t;
    cout1(cost[t] - cost[s]);
    // cout1( 100000000000000000-1);
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

#define rep(i, a) for (int i = 0; i < a; i++)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define loop3(i, j, k, a)                                                      \
  for (int i = 0; i < a; i++)                                                  \
    for (int j = 0; j < a; j++)                                                \
      if (i != j)                                                              \
        for (int k = 0; k < a; k++)                                            \
          if (i != k && j != k)
#define loop4(i, j, k, l, a)                                                   \
  for (int i = 0; i < a; i++)                                                  \
    for (int j = 0; j < a; j++)                                                \
      if (i != j)                                                              \
        for (int k = 0; k < a; k++)                                            \
          if (i != k && j != k)                                                \
            for (int l = 0; l < a; l++)                                        \
              if (i != l && j != l && k != l)
#define rep1(i, a) for (int i = 1; i <= a; i++)

#define scnd1(a) scanf("%d", &a)
#define scnd2(a, b) scanf("%d%d", &a, &b)
#define scnd3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scnd4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)

#define cin1(a) cin >> a;
#define cin2(a, b) cin >> a >> b;
#define cin3(a, b, c) cin >> a >> b >> c;
#define cin4(a, b, c, d) cin >> a >> b >> c >> d;
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define prtd1(a) printf("%d\n", a)
#define prtd2(a, b) printf("%d %d\n", a, b)
#define prtd3(a, b, c) printf("%d %d %d\n", a, b, c)
#define prtd4(a, b, c, d) printf("%d %d %d %d\n", a, b, c, d)

#define mem(a, n) memset(a, n, sizeof(a))
typedef long long ll;

pair<int, int> nums[100000];
int cost[100000];

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

  int n;
  rep1(i, H) {
    rep1(j, W) {
      cin >> n;
      nums[n] = {i, j};
    }
  }

  mem(cost, 0);

  for (int i = 1; i < 1 + D; i++) {
    for (int u = i; u + D <= H * W; u += D) {
      cost[u + D] = abs(nums[u + D].first - nums[u].first) +
                    abs(nums[u + D].second - nums[u].second);
      cost[u + D] += cost[u];
      // cout4( u, cost[u],u+D,cost[u+D]);
    }
  }

  int Q, s, t;
  cin >> Q;
  rep(i, Q) {
    cin >> s >> t;
    cout1(cost[t] - cost[s]);
    // cout1( 100000000000000000-1);
  }
}