Problem p03426 (C++)

Buggy submission (status: Runtime Error):
/*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*/
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
// const ll mod = 998244353;

inline void normal(ll &a) {
  a %= mod;
  (a < 0) && (a += mod);
}
inline ll modMul(ll a, ll b) {
  a %= mod, b %= mod;
  normal(a), normal(b);
  return (a * b) % mod;
}
inline ll modAdd(ll a, ll b) {
  a %= mod, b %= mod;
  normal(a), normal(b);
  return (a + b) % mod;
}
inline ll modSub(ll a, ll b) {
  a %= mod, b %= mod;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, mod - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

#define si(x) scanf("%d", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sl(x) scanf("%lld", &x)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define ss(ch) scanf("%s", ch)
#define pi(x) printf("%d", x)
#define pii(x, y) printf("%d %d", x, y)
#define piii(x, y, z) printf("%d %d %d", x, y, z)
#define pl(x) printf("%lld", x)
#define pll(x, y) printf("%lld %lld", x, y)
#define plll(x, y, z) printf("%lld %lld %lld", x, y, z)
#define ps(ch) printf("%s", ch)
#define F(i, a, b) for (int i = a; i <= b; i++)
#define R(i, b, a) for (int i = b; i >= a; i--)

const int N = 90001;
const int M = 1e5 + 5;

int px[N], py[N];
int ans[N];
int main() {
  // FASTIO
  /*
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/

  int n, m, D;
  siii(n, m, D);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x;
      si(x);
      px[x] = i;
      py[x] = j;
    }
  }

  for (int i = D + 1; i <= n * m; i++)
    ans[i] = ans[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D]);
  int q;
  si(q);
  while (q--) {
    int l, r;
    sii(l, r);
    printf("%d\n", ans[r] - ans[l]);
  }
  return 0;
}

Fixed submission:
/*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*/
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
// const ll mod = 998244353;

inline void normal(ll &a) {
  a %= mod;
  (a < 0) && (a += mod);
}
inline ll modMul(ll a, ll b) {
  a %= mod, b %= mod;
  normal(a), normal(b);
  return (a * b) % mod;
}
inline ll modAdd(ll a, ll b) {
  a %= mod, b %= mod;
  normal(a), normal(b);
  return (a + b) % mod;
}
inline ll modSub(ll a, ll b) {
  a %= mod, b %= mod;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, mod - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

#define si(x) scanf("%d", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sl(x) scanf("%lld", &x)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define ss(ch) scanf("%s", ch)
#define pi(x) printf("%d", x)
#define pii(x, y) printf("%d %d", x, y)
#define piii(x, y, z) printf("%d %d %d", x, y, z)
#define pl(x) printf("%lld", x)
#define pll(x, y) printf("%lld %lld", x, y)
#define plll(x, y, z) printf("%lld %lld %lld", x, y, z)
#define ps(ch) printf("%s", ch)
#define F(i, a, b) for (int i = a; i <= b; i++)
#define R(i, b, a) for (int i = b; i >= a; i--)

const int N = 90001;
const int M = 1e5 + 5;

int px[N], py[N];
int ans[N];
int main() {
  // FASTIO
  /*
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/

  int n, m, D;
  siii(n, m, D);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x;
      si(x);
      px[x] = i;
      py[x] = j;
    }
  }

  for (int i = D + 1; i <= n * m; i++)
    ans[i] = ans[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D]);
  int q;
  si(q);
  while (q--) {
    int l, r;
    sii(l, r);
    printf("%d\n", ans[r] - ans[l]);
  }
  return 0;
}