Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rmsame(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define rmvector(a, b)                                                         \
  rep(i, a.size()) rep(j, b.size()) if (a[i] == b[j]) {                        \
    a.erase(a.begin() + i);                                                    \
    i--;                                                                       \
    break;                                                                     \
  }
template <typename X> bool exist(vector<X> vec, X item) {
  return find(all(vec), item) != vec.end();
}

pair<ll, ll> A[90010];
ll ans[90010];
ll dis(pair<ll, ll> a, pair<ll, ll> b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}

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

  ll h, w, d;
  cin >> h >> w >> d;

  rep(i, h) rep(j, w) {
    ll inp;
    cin >> inp;
    A[inp] = mp(i, j);
  }

  ll q;
  cin >> q;
  vector<pair<ll, ll>> lr;
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    lr.pb(mp(l, r));
  }

  rep(i, d + 1) {
    ans[i] = 0;
    if (i == 0)
      continue;
    ll s = i;
    while (s <= h * w) {
      s += d;
      ans[s] = ans[s - d] + dis(A[s - d], A[s]);
    }
  }

  rep(i, q) { cout << ans[lr[i].second] - ans[lr[i].first] << endl; }

  // cout << fixed << setprecision(16) << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rmsame(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define rmvector(a, b)                                                         \
  rep(i, a.size()) rep(j, b.size()) if (a[i] == b[j]) {                        \
    a.erase(a.begin() + i);                                                    \
    i--;                                                                       \
    break;                                                                     \
  }
template <typename X> bool exist(vector<X> vec, X item) {
  return find(all(vec), item) != vec.end();
}

pair<ll, ll> A[90010];
ll ans[90010];
ll dis(pair<ll, ll> a, pair<ll, ll> b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}

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

  ll h, w, d;
  cin >> h >> w >> d;

  rep(i, h) rep(j, w) {
    ll inp;
    cin >> inp;
    A[inp] = mp(i, j);
  }

  ll q;
  cin >> q;
  vector<pair<ll, ll>> lr;
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    lr.pb(mp(l, r));
  }

  rep(i, d + 1) {
    ans[i] = 0;
    if (i == 0)
      continue;
    ll s = i;
    while (s <= (h * w - d)) {
      s += d;
      ans[s] = ans[s - d] + dis(A[s - d], A[s]);
    }
  }

  rep(i, q) { cout << ans[lr[i].second] - ans[lr[i].first] << endl; }

  // cout << fixed << setprecision(16) << ans << endl;
  return 0;
}