Problem p03426 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;

int find_index(const vector<tuple<int, int, int>> &group, int k) {
  int lower = 0;
  int upper = group.size();
  do {
    int cur = (upper + lower) / 2;
    if (get<0>(group[cur]) <= k) {
      lower = cur;
    } else {
      upper = cur;
    }
  } while (upper - lower > 1);
  return lower;
}

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<tuple<int, int, int>>> group(d);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      int a;
      cin >> a;
      group[a % d].push_back(make_tuple(a, i, j));
    }
  }
  for (int i = 0; i < d; ++i) {
    sort(group[i].begin(), group[i].end());
  }
  vector<vector<int>> power1(d), power2(d);
  for (int i = 0; i < d; ++i) {
    power1.resize(group[i].size());
    power2.resize(group[i].size());
    for (int j = 1; j < group[i].size(); ++j) {
      power1[i][j] = power1[i][j - 1];
      power1[i][j] += abs(get<1>(group[i][j]) - get<1>(group[i][j - 1])) +
                      abs(get<2>(group[i][j]) - get<2>(group[i][j - 1]));
    }
    power2[i].back() = abs(get<1>(group[i].front()) - get<1>(group[i].back())) +
                       abs(get<2>(group[i].front()) - get<2>(group[i].back()));
    for (int j = group[i].size() - 2; j >= 0; --j) {
      power2[i][j] = power2[i][j + 1];
      power2[i][j] += abs(get<1>(group[i][j]) - get<1>(group[i][j + 1])) +
                      abs(get<2>(group[i][j]) - get<2>(group[i][j + 1]));
    }
  }
  int q;
  cin >> q;
  vector<int> ans(q);
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    int g = a % d;
    int k = find_index(group[g], a);
    int l = find_index(group[g], b);

    if (k < l) {
      ans[i] = power1[g][l] - power1[g][k];
    } else if (k > l) {
      ans[i] = power1[g][l] + power2[g][k];
    }
  }
  for (int a : ans) {
    cout << a << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;

int find_index(const vector<tuple<int, int, int>> &group, int k) {
  int lower = 0;
  int upper = group.size();
  do {
    int cur = (upper + lower) / 2;
    if (get<0>(group[cur]) <= k) {
      lower = cur;
    } else {
      upper = cur;
    }
  } while (upper - lower > 1);
  return lower;
}

int main() {
  int h, w, d;
  cin >> h >> w >> d;
  vector<vector<tuple<int, int, int>>> group(d);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      int a;
      cin >> a;
      group[a % d].push_back(make_tuple(a, i, j));
    }
  }
  for (int i = 0; i < d; ++i) {
    sort(group[i].begin(), group[i].end());
  }
  vector<vector<int>> power1(d), power2(d);
  for (int i = 0; i < d; ++i) {
    power1[i].resize(group[i].size());
    power2[i].resize(group[i].size());
    for (int j = 1; j < group[i].size(); ++j) {
      power1[i][j] = power1[i][j - 1];
      power1[i][j] += abs(get<1>(group[i][j]) - get<1>(group[i][j - 1])) +
                      abs(get<2>(group[i][j]) - get<2>(group[i][j - 1]));
    }
    power2[i].back() = abs(get<1>(group[i].front()) - get<1>(group[i].back())) +
                       abs(get<2>(group[i].front()) - get<2>(group[i].back()));
    for (int j = group[i].size() - 2; j >= 0; --j) {
      power2[i][j] = power2[i][j + 1];
      power2[i][j] += abs(get<1>(group[i][j]) - get<1>(group[i][j + 1])) +
                      abs(get<2>(group[i][j]) - get<2>(group[i][j + 1]));
    }
  }
  int q;
  cin >> q;
  vector<int> ans(q);
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    int g = a % d;
    int k = find_index(group[g], a);
    int l = find_index(group[g], b);

    if (k < l) {
      ans[i] = power1[g][l] - power1[g][k];
    } else if (k > l) {
      ans[i] = power1[g][l] + power2[g][k];
    }
  }
  for (int a : ans) {
    cout << a << endl;
  }
  return 0;
}