Problem p03426 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long ll;
// typedef pair<int,int> Pint;
typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef tuple<int,int,int> T;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define revrep(i, n) for (ll i = n - 1; i >= 0; i--)
ll max(ll a, ll b) { return (a > b) ? a : b; }
ll min(ll a, ll b) { return (a < b) ? a : b; }

ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 1000000000;          // 10^9
ll MOD = 1000000007;
// vector<int> dy = {0,0,1,-1};
// vector<int> dx = {1,-1,0,0};

ll H, W, D;
ll A[310][310];
vector<pair<ll, ll>> pos(100010);
ll dp[100010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> D;
  rep(i, H) rep(j, W) cin >> A[i][j];
  rep(i, H) rep(j, W) { pos[A[i][j]] = {i + 1, j + 1}; }
  for (int i = D + 1; i <= H * W; i++) {
    for (int j = 0; j < D; j++) {
      dp[i] = dp[i - D] + abs(pos[i].first - pos[i - D].first) +
              abs(pos[i].second - pos[i - D].second);
    }
  }
  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll L, R;
    cin >> L >> R;
    cout << dp[R] - dp[L] << endl;
  }
}


Fixed submission:
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long ll;
// typedef pair<int,int> Pint;
typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef tuple<int,int,int> T;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define revrep(i, n) for (ll i = n - 1; i >= 0; i--)
ll max(ll a, ll b) { return (a > b) ? a : b; }
ll min(ll a, ll b) { return (a < b) ? a : b; }

ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 1000000000;          // 10^9
ll MOD = 1000000007;
// vector<int> dy = {0,0,1,-1};
// vector<int> dx = {1,-1,0,0};

ll H, W, D;
ll A[310][310];
vector<pair<ll, ll>> pos(100010);
ll dp[100010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> D;
  rep(i, H) rep(j, W) cin >> A[i][j];
  rep(i, H) rep(j, W) { pos[A[i][j]] = {i + 1, j + 1}; }
  for (int i = D + 1; i <= H * W; i++) {
    dp[i] = dp[i - D] + abs(pos[i].first - pos[i - D].first) +
            abs(pos[i].second - pos[i - D].second);
  }
  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll L, R;
    cin >> L >> R;
    cout << dp[R] - dp[L] << endl;
  }
}