Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_int.hpp>
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

l_l b[100000];
ll kyori(l_l x, l_l y) {
  return (abs(x.first - y.first) + abs(x.second - y.second));
}

int main() {
  ll h, w, d;
  cin >> h >> w >> d;
  ll a[h][w];
  l_l b[h * w + 1];
  rep(i, 0, h - 1) {
    rep(j, 0, w - 1) {
      cin >> a[i][j];
      b[a[i][j]] = make_pair(i, j);
    }
  }
  ll dp[d][h * w + 1];
  ll c;
  rep(i, 0, d - 1) {
    dp[i][0] = 0;
    c = 1;
    while (c * d + i <= h * w) {
      dp[i][c] = dp[i][c - 1] + kyori(b[d * (c - 1) + i], b[d * c + i]);
      c++;
    }
  }
  ll q;
  cin >> q;
  ll l, r;
  ll ans[q];
  rep(i, 0, q - 1) {
    cin >> l >> r;
    if (l < d) {
      ans[i] = dp[l][(r - l) / d];
    } else {
      ans[i] = dp[l % d][(r - l % d) / d] - dp[l % d][(l - l % d) / d];
      // cout<<dp[l%d][(r-l%d)/d]<<' '<<dp[l%d][(l-l%d)/d]<<endl;
    }
  }
  rep(i, 0, q - 1) { cout << ans[i] << endl; }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_int.hpp>
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

l_l b[100000];
ll kyori(l_l x, l_l y) {
  return (abs(x.first - y.first) + abs(x.second - y.second));
}

int main() {
  ll h, w, d;
  cin >> h >> w >> d;
  ll a[h][w];
  l_l b[h * w + 1];
  rep(i, 0, h - 1) {
    rep(j, 0, w - 1) {
      cin >> a[i][j];
      b[a[i][j]] = make_pair(i, j);
    }
  }
  ll dp[d][(h * w + 1) / d + 10];
  ll c;
  rep(i, 0, d - 1) {
    dp[i][0] = 0;
    c = 1;
    while (c * d + i <= h * w) {
      dp[i][c] = dp[i][c - 1] + kyori(b[d * (c - 1) + i], b[d * c + i]);
      c++;
    }
  }
  ll q;
  cin >> q;
  ll l, r;
  ll ans[q];
  rep(i, 0, q - 1) {
    cin >> l >> r;
    if (l < d) {
      ans[i] = dp[l][(r - l) / d];
    } else {
      ans[i] = dp[l % d][(r - l % d) / d] - dp[l % d][(l - l % d) / d];
      // cout<<dp[l%d][(r-l%d)/d]<<' '<<dp[l%d][(l-l%d)/d]<<endl;
    }
  }
  rep(i, 0, q - 1) { cout << ans[i] << endl; }

  return 0;
}