Problem p03426 (C++)

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

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> plli;
typedef vector<lli> vlli;
typedef vector<plli> vplli;

#define rep(i, a) for (i = 0; i < a; i++)
#define repr(i, a) for (i = a; i >= 0; i--)
#define REP(i, a, b) for (i = a; i <= b; i++)
#define REPR(i, a, b) for (i = a; i >= b; i--)

#define scan(N) scanf("%lld", &N)
#define print(N) printf("%lld", N)
#define scan2(N1, N2) scanf("%lld %lld", &N1, &N2)
#define print2(N1, N2) printf("%lld %lld", N1, N2)
#define scan3(N1, N2, N3) scanf("%lld %lld %lld", &N1, &N2, &N3)
#define print3(N1, N2, N3) printf("%lld %lld %lld", N1, N2, N3)

#define mem(a, b) memset(a, b, sizeof(a))
#define opt                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define sqr(x) (x) * (x)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl

#define endl '\n'

const long long INF = 2000000000000;
const long long MOD = 1000000007;
const long long MAXN = 20005;

template <class T> bool is_prime(T n) {
  if ((n % 2 == 0 and n > 2) or n < 2)
    return false;
  for (T i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}
lli power(lli a, lli b) {
  lli value;
  if (!b)
    return 1;
  else if (b % 2 == 0) {
    value = power(a, b / 2) % MOD;
    return (value * value) % MOD;
  } else {
    value = power(a, b / 2) % MOD;
    return ((a * value) % MOD * (value)) % MOD;
  }
}

int main() {
  opt;
  lli i, j, h, w, d, x, q, l, r, maxi = -1;
  cin >> h >> w >> d;
  vplli v(MAXN);
  vlli cost(MAXN, 0);
  REP(i, 1, h) {
    REP(j, 1, w) {
      cin >> x;
      maxi = max(maxi, x);
      v[x].first = i;
      v[x].second = j;
    }
  }
  REP(i, 1, maxi) {
    cost[i + d] += abs(v[i + d].first - v[i].first) +
                   abs(v[i + d].second - v[i].second) + cost[i];
  }
  cin >> q;
  while (q--) {
    cin >> l >> r;
    cout << cost[r] - cost[l] << endl;
  }
  return 0;
}


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

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> plli;
typedef vector<lli> vlli;
typedef vector<plli> vplli;

#define rep(i, a) for (i = 0; i < a; i++)
#define repr(i, a) for (i = a; i >= 0; i--)
#define REP(i, a, b) for (i = a; i <= b; i++)
#define REPR(i, a, b) for (i = a; i >= b; i--)

#define scan(N) scanf("%lld", &N)
#define print(N) printf("%lld", N)
#define scan2(N1, N2) scanf("%lld %lld", &N1, &N2)
#define print2(N1, N2) printf("%lld %lld", N1, N2)
#define scan3(N1, N2, N3) scanf("%lld %lld %lld", &N1, &N2, &N3)
#define print3(N1, N2, N3) printf("%lld %lld %lld", N1, N2, N3)

#define mem(a, b) memset(a, b, sizeof(a))
#define opt                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define sqr(x) (x) * (x)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl

#define endl '\n'

const long long INF = 2000000000000;
const long long MOD = 1000000007;
const long long MAXN = 3000005;

template <class T> bool is_prime(T n) {
  if ((n % 2 == 0 and n > 2) or n < 2)
    return false;
  for (T i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}
lli power(lli a, lli b) {
  lli value;
  if (!b)
    return 1;
  else if (b % 2 == 0) {
    value = power(a, b / 2) % MOD;
    return (value * value) % MOD;
  } else {
    value = power(a, b / 2) % MOD;
    return ((a * value) % MOD * (value)) % MOD;
  }
}

int main() {
  opt;
  lli i, j, h, w, d, x, q, l, r, maxi = -1;
  cin >> h >> w >> d;
  vplli v(MAXN);
  vlli cost(MAXN, 0);
  REP(i, 1, h) {
    REP(j, 1, w) {
      cin >> x;
      maxi = max(maxi, x);
      v[x].first = i;
      v[x].second = j;
    }
  }
  REP(i, 1, maxi) {
    cost[i + d] += abs(v[i + d].first - v[i].first) +
                   abs(v[i + d].second - v[i].second) + cost[i];
  }
  cin >> q;
  while (q--) {
    cin >> l >> r;
    cout << cost[r] - cost[l] << endl;
  }
  return 0;
}