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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> Pi;
typedef pair<int, Pi> PP;

const int INF = 1000000000;
const long long int INF_ = 1000000000000000000;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

inline bool IsIn(int y, int x, int h, int w) {
  return 0 <= y && y < h && 0 <= x && x < w;
}

void BFS(vector<vector<int>> &f, vector<vector<int>> &l, int sy, int sx, int h,
         int w) {
  vector<vector<int>> done(h, vector<int>(w));
  vector<vector<int>> done_(h, vector<int>(w));
  l[sy][sx] = 1;
  done[sy][sx] = done_[sy][sx] = 1;
  queue<Pi> que;
  REP(i, 0, 4) {
    int ny = sy + dy[i], nx = sx + dx[i];
    if (!IsIn(ny, nx, h, w))
      continue;
    done_[ny][nx] = 1;
    que.push(Pi(ny, nx));
  }
  while (!que.empty()) {
    Pi p = que.front();
    que.pop();
    done[p.F_][p.S_] = 1;
    bool flag = true;
    int tmp = INF;
    REP(i, 0, 4) {
      int ny = p.F_ + dy[i], nx = p.S_ + dx[i];
      if (!IsIn(ny, nx, h, w))
        continue;
      if (done[ny][nx] == 1)
        tmp = min(tmp, l[ny][nx]);
      if (done[ny][nx] == 1 && l[ny][nx] >= f[p.F_][p.S_])
        flag = false;
      if (done_[ny][nx] != 1) {
        done_[ny][nx] = 1;
        que.push(Pi(ny, nx));
      }
    }
    if (flag)
      l[p.F_][p.S_] = f[p.F_][p.S_];
    else {
      if (tmp != INF)
        l[p.F_][p.S_] = max(tmp, f[p.F_][p.S_]);
      else
        l[p.F_][p.S_] = f[p.F_][p.S_];
    }
  }
}

/*
void Djikstra(vector<vector<int> > &f,vector<vector<int> > &l,int sy,int sx,int
h,int w){ vector<vector<bool> > used(h,vector<bool>(w,false)); REP(y,0,h)
fill(l[y].begin(),l[y].end(),INF); l[sy][sx]=1; while(true){ Pi v(-1,-1);
                REP(y,0,h){
                        REP(x,0,w){
                                if(!used[y][x]&&((v.F_==-1&&v.S_==-1)||l[y][x]<l[v.F_][v.S_])){
                                        v.F_=y;
                                        v.S_=x;
                                }
                        }
                }
                if(v.F_==-1&&v.S_==-1) break;
                used[v.F_][v.S_]=true;
                REP(i,0,4){
                        int ny=v.F_+dy[i],nx=v.S_+dx[i];
                        if(!IsIn(ny,nx,h,w)) continue;
                        l[ny][nx]=min(l[ny][nx],max(f[ny][nx],l[v.F_][v.S_]));
                }
        }
}
*/

void Djikstra(vector<vector<int>> &f, vector<vector<int>> &l, int sy, int sx,
              int h, int w) {
  priority_queue<PP, vector<PP>, greater<PP>> que;
  REP(y, 0, h) fill(l[y].begin(), l[y].end(), INF);
  l[sy][sx] = 1;
  que.push(PP(1, Pi(sy, sx)));
  while (!que.empty()) {
    PP p = que.top();
    que.pop();
    Pi v = p.S_;
    if (l[v.F_][v.S_] < p.F_)
      continue;
    REP(i, 0, 4) {
      int ny = v.F_ + dy[i], nx = v.S_ + dx[i];
      if (!IsIn(ny, nx, h, w))
        continue;
      if (l[ny][nx] > max(f[ny][nx], l[v.F_][v.S_])) {
        l[ny][nx] = max(f[ny][nx], l[v.F_][v.S_]);
        que.push(PP(l[ny][nx], Pi(ny, nx)));
      }
    }
  }
}

inline int f(int x) {
  if (x == 0)
    return 1;
  else if (x == 1)
    return 0;
}

void solve() {
  int R;
  while (cin >> R && R) {
    int W[2], H[2], X[2], Y[2];
    vector<vector<int>> field[2];
    vector<vector<int>> level[2];
    int max_[2] = {};
    REP(i, 0, 2) {
      cin >> W[i] >> H[i] >> X[i] >> Y[i];
      --X[i];
      --Y[i];
      field[i].resize(H[i]);
      level[i].resize(H[i]);
      REP(j, 0, H[i]) {
        field[i][j].resize(W[i]);
        level[i][j].resize(W[i]);
      }
      REP(y, 0, H[i]) REP(x, 0, W[i]) {
        cin >> field[i][y][x];
        max_[i] = max(max_[i], field[i][y][x]);
      }
      Djikstra(field[i], level[i], Y[i], X[i], H[i], W[i]);
      /*
      cout << endl << endl;
      REP(y,0,H[i]){
              REP(x,0,W[i]) cout << level[i][y][x] << ' ';
              cout << endl;
      }
      */
    }
    /*
    vector<unsigned int> s[2];
    REP(i,0,2){
            s[i].resize(max_[i]+1);
            REP(y,0,H[i]) REP(x,0,W[i]) ++s[i][level[i][y][x]];
            REP(j,1,max_[i]+1) s[i][j]+=s[i][j-1];
    }
    int result=INF;
    REP(i,0,2){
            for(int j=0; j<=max_[i]&&j<=result; ++j){
                    int
    idx=(int)(lower_bound(s[i].begin(),s[i].end(),R-s[f(i)][j])-s[i].begin());
                    if(!(0<=idx&&idx<=max_[i])) continue;
                    result=min(result,j+idx);
            }
    }
    cout << result << endl;
    */
    map<int, int> s[2];
    vector<int> idx[2], num[2];
    REP(i, 0, 2) {
      REP(y, 0, H[i]) REP(x, 0, W[i])++ s[i][level[i][y][x]];
      auto it = s[i].begin();
      idx[i].push_back(0);
      num[i].push_back(0);
      idx[i].push_back(it->F_);
      num[i].push_back(it->S_);
      auto prev = it;
      ++it;
      for (; it != s[i].end(); ++it, ++prev) {
        it->S_ += prev->S_;
        // cout << it->F_ << ' ' << it->S_ << endl;
        idx[i].push_back(it->F_);
        num[i].push_back(it->S_);
      }
    }
    int result = INF;
    /*
    cout << endl;
    REP(i,0,2) REP(j,0,idx[i].size()){
            cout << i << ' ' << idx[i][j] << ' ' << num[i][j] << endl;
    }
    cout << endl;
    */
    REP(i, 0, 2) REP(j, 0, idx[i].size()) {
      int tmp =
          (int)(lower_bound(num[f(i)].begin(), num[f(i)].end(), R - num[i][j]) -
                num[f(i)].begin());
      // cout << idx[i][tmp] << ' ' << num[i][tmp] << ' ' << idx[f(i)][j] << ' '
      // << num[f(i)][j] << endl;
      if (!(0 <= tmp && tmp <= idx[f(i)].size() - 1))
        continue;
      result = min(result, idx[i][j] + idx[f(i)][tmp]);
    }
    cout << result << endl;
  }
}

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> Pi;

const int INF = 1000000000;
const long long int INF_ = 1000000000000000000;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

inline bool IsIn(int y, int x, int h, int w) {
  return 0 <= y && y < h && 0 <= x && x < w;
}

void BFS(vector<vector<int>> &f, vector<vector<int>> &l, int sy, int sx, int h,
         int w) {
  vector<vector<int>> done(h, vector<int>(w));
  vector<vector<int>> done_(h, vector<int>(w));
  l[sy][sx] = 1;
  done[sy][sx] = done_[sy][sx] = 1;
  queue<Pi> que;
  REP(i, 0, 4) {
    int ny = sy + dy[i], nx = sx + dx[i];
    if (!IsIn(ny, nx, h, w))
      continue;
    done_[ny][nx] = 1;
    que.push(Pi(ny, nx));
  }
  while (!que.empty()) {
    Pi p = que.front();
    que.pop();
    done[p.F_][p.S_] = 1;
    bool flag = true;
    int tmp = INF;
    REP(i, 0, 4) {
      int ny = p.F_ + dy[i], nx = p.S_ + dx[i];
      if (!IsIn(ny, nx, h, w))
        continue;
      if (done[ny][nx] == 1)
        tmp = min(tmp, l[ny][nx]);
      if (done[ny][nx] == 1 && l[ny][nx] >= f[p.F_][p.S_])
        flag = false;
      if (done_[ny][nx] != 1) {
        done_[ny][nx] = 1;
        que.push(Pi(ny, nx));
      }
    }
    if (flag)
      l[p.F_][p.S_] = f[p.F_][p.S_];
    else {
      if (tmp != INF)
        l[p.F_][p.S_] = max(tmp, f[p.F_][p.S_]);
      else
        l[p.F_][p.S_] = f[p.F_][p.S_];
    }
  }
}

void Djikstra(vector<vector<int>> &f, vector<vector<int>> &l, int sy, int sx,
              int h, int w) {
  vector<vector<bool>> used(h, vector<bool>(w, false));
  REP(y, 0, h) fill(l[y].begin(), l[y].end(), INF);
  l[sy][sx] = 1;
  while (true) {
    Pi v(-1, -1);
    REP(y, 0, h) {
      REP(x, 0, w) {
        if (!used[y][x] &&
            ((v.F_ == -1 && v.S_ == -1) || l[y][x] < l[v.F_][v.S_])) {
          v.F_ = y;
          v.S_ = x;
        }
      }
    }
    if (v.F_ == -1 && v.S_ == -1)
      break;
    used[v.F_][v.S_] = true;
    REP(i, 0, 4) {
      int ny = v.F_ + dy[i], nx = v.S_ + dx[i];
      if (!IsIn(ny, nx, h, w))
        continue;
      l[ny][nx] = min(l[ny][nx], max(f[ny][nx], l[v.F_][v.S_]));
    }
  }
}

inline int f(int x) {
  if (x == 0)
    return 1;
  else if (x == 1)
    return 0;
}

void solve() {
  int R;
  while (cin >> R && R) {
    int W[2], H[2], X[2], Y[2];
    vector<vector<int>> field[2];
    vector<vector<int>> level[2];
    int max_[2] = {};
    REP(i, 0, 2) {
      cin >> W[i] >> H[i] >> X[i] >> Y[i];
      --X[i];
      --Y[i];
      field[i].resize(H[i]);
      level[i].resize(H[i]);
      REP(j, 0, H[i]) {
        field[i][j].resize(W[i]);
        level[i][j].resize(W[i]);
      }
      REP(y, 0, H[i]) REP(x, 0, W[i]) {
        cin >> field[i][y][x];
        max_[i] = max(max_[i], field[i][y][x]);
      }
      Djikstra(field[i], level[i], Y[i], X[i], H[i], W[i]);
      /*
      cout << endl << endl;
      REP(y,0,H[i]){
              REP(x,0,W[i]) cout << level[i][y][x] << ' ';
              cout << endl;
      }
      */
    }
    /*
    vector<unsigned int> s[2];
    REP(i,0,2){
            s[i].resize(max_[i]+1);
            REP(y,0,H[i]) REP(x,0,W[i]) ++s[i][level[i][y][x]];
            REP(j,1,max_[i]+1) s[i][j]+=s[i][j-1];
    }
    int result=INF;
    REP(i,0,2){
            for(int j=0; j<=max_[i]&&j<=result; ++j){
                    int
    idx=(int)(lower_bound(s[i].begin(),s[i].end(),R-s[f(i)][j])-s[i].begin());
                    if(!(0<=idx&&idx<=max_[i])) continue;
                    result=min(result,j+idx);
            }
    }
    cout << result << endl;
    */
    map<int, int> s[2];
    vector<int> idx[2], num[2];
    REP(i, 0, 2) {
      REP(y, 0, H[i]) REP(x, 0, W[i])++ s[i][level[i][y][x]];
      auto it = s[i].begin();
      idx[i].push_back(0);
      num[i].push_back(0);
      idx[i].push_back(it->F_);
      num[i].push_back(it->S_);
      auto prev = it;
      ++it;
      for (; it != s[i].end(); ++it, ++prev) {
        it->S_ += prev->S_;
        // cout << it->F_ << ' ' << it->S_ << endl;
        idx[i].push_back(it->F_);
        num[i].push_back(it->S_);
      }
    }
    int result = INF;
    /*
    cout << endl;
    REP(i,0,2) REP(j,0,idx[i].size()){
            cout << i << ' ' << idx[i][j] << ' ' << num[i][j] << endl;
    }
    cout << endl;
    */
    REP(i, 0, 2) REP(j, 0, idx[i].size()) {
      int tmp =
          (int)(lower_bound(num[f(i)].begin(), num[f(i)].end(), R - num[i][j]) -
                num[f(i)].begin());
      // cout << idx[i][tmp] << ' ' << num[i][tmp] << ' ' << idx[f(i)][j] << ' '
      // << num[f(i)][j] << endl;
      if (!(0 <= tmp && tmp <= idx[f(i)].size() - 1))
        continue;
      result = min(result, idx[i][j] + idx[f(i)][tmp]);
    }
    cout << result << endl;
  }
}

int main() {
  solve();
  return 0;
}