Problem p00465 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dxy[5] = {0, -1, 0, 1, 0};
int main() {
  int r;
  while (scanf("%d", &r), r) {
    vector<pair<int, int>> ans[2];
    for (int k = 0; k < 2; k++) {
      int w, h, sx, sy;
      int fld[500][500];
      vector<int> vec(1, 0);
      scanf("%d%d%d%d", &w, &h, &sx, &sy);
      sx--;
      sy--;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          int v;
          scanf("%d", &v);
          fld[i][j] = v;
          vec.push_back(v);
        }
      }
      sort(vec.begin(), vec.end());
      vec.erase(unique(vec.begin(), vec.end()), vec.end());
      priority_queue<piii, vector<piii>, greater<piii>> Q;
      int pos = 0, cnt = 0;
      Q.push(piii(1, pii(sy, sx)));
      bool used[500][500] = {{0}};
      while (Q.size()) {
        int y = Q.top().second.first, x = Q.top().second.second;
        int v = Q.top().first;
        Q.pop();
        if (used[y][x])
          continue;
        if (v > vec[pos]) {
          ans[k].push_back(pii(cnt, vec[pos]));
          pos++;
          Q.push(piii(v, pii(y, x)));
          continue;
        }
        used[y][x] = true;
        cnt++;
        for (int i = 0; i < 4; i++) {
          int ty = y + dxy[i + 1], tx = x + dxy[i];
          if (ty < 0 || ty >= h || tx < 0 || tx >= w)
            continue;
          Q.push(piii(fld[ty][tx], pii(ty, tx)));
        }
      }
      ans[k].push_back(pii(cnt, vec[pos]));
    }
    // for(int k=0;k<2;k++)for(int
    // i=0;i<ans[k].size();i++)printf("%d...%d:%d\\n",k,ans[k][i].first,ans[k][i].second);

    int Min = 1000000000;
    for (int i = 0; i < ans[0].size(); i++) {
      int to = r - ans[0][i].first;
      int lb = -1, ub = ans[1].size();
      while (ub - lb > 1) {
        int mid = (ub + lb) / 2;
        if (ans[1][mid].first >= to)
          ub = mid;
        else
          lb = mid;
      }
      if (ub == ans[1].size())
        continue;
      int value = ans[0][i].first + ans[1][ub].first;
      Min = min(Min, ans[0][i].second + ans[1][ub].second);

      // printf("%d:%d->%d\\n",ans[0][i].second,ans[1][ub].second,value);
    }
    printf("%d\\n", Min);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int dxy[5] = {0, -1, 0, 1, 0};
int used[500][500];
int main() {
  int r;
  while (scanf("%d", &r), r) {
    vector<pair<int, int>> ans[2];
    for (int k = 0; k < 2; k++) {
      int w, h, sx, sy;
      int fld[500][500];
      vector<int> vec(1, 0);
      scanf("%d%d%d%d", &w, &h, &sx, &sy);
      sx--;
      sy--;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          int v;
          scanf("%d", &v);
          fld[i][j] = v;
          vec.push_back(v);
        }
      }
      sort(vec.begin(), vec.end());
      vec.erase(unique(vec.begin(), vec.end()), vec.end());
      bool visited[500][500];
      fill_n(*visited, 500 * 500, false);
      fill_n(*used, 500 * 500, -1);
      queue<pair<int, int>> que[2];
      que[0].push(pair<int, int>(sy, sx));
      for (int i = 0; i < vec.size(); i++) {
        int cnt = 0;
        while (que[i & 1].size()) {
          int y = que[i & 1].front().first, x = que[i & 1].front().second;
          que[i & 1].pop();
          if (y < 0 || y >= h || x < 0 || x >= w)
            continue;
          if (visited[y][x])
            continue;
          if (used[y][x] == vec[i])
            continue;
          if (fld[y][x] > vec[i]) {
            que[(i + 1) & 1].push(pair<int, int>(y, x));
            used[y][x] = vec[i];
            continue;
          }
          cnt++;
          visited[y][x] = true;
          for (int l = 0; l < 4; l++) {
            int ty = y + dxy[l + 1], tx = x + dxy[l];
            que[i & 1].push(pair<int, int>(ty, tx));
          }
        }
        ans[k].push_back(pair<int, int>(cnt, vec[i]));
        ans[k][i].first += (i ? ans[k][i - 1].first : 0);
      }
    }

    int Min = 1e9;
    for (int i = 0; i < ans[0].size(); i++) {
      int to = r - ans[0][i].first;
      int lb = 0, ub = ans[1].size();
      while (ub - lb > 1) {
        int mid = (ub + lb) / 2;
        if (ans[1][mid].first > to)
          ub = mid;
        else
          lb = mid;
      }
      int value = ans[0][i].first + ans[1][ub - 1].first;
      if (value >= r) {
        Min = min(Min, ans[0][i].second + ans[1][ub - 1].second);
        // printf("%d:%d->%d\\n",ans[0][i].second,ans[1][ub-1].second,value);
      }
    }
    printf("%d\\n", Min);
  }
  return 0;
}