Problem p00465 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(vec) sort(vec.begin(), vec.end())
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(min(a, b), c)
#define max_3(a, b, c) max(max(a, b), c)
#define mp1(a, b, c) P1(a, P(b, c))
#define fr first
#define sc second
#define pb push_back

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

int r;
int w[2], h[2], x[2], y[2];
int l[2][502][502];

int a[502][502];

int dfs(int X, int Y) {
  if (a[X][Y] != 0)
    return 0;
  a[X][Y] = 1;
  int ret = 1;
  rep(i, 4) { ret += dfs(X + dir_4[i][0], Y + dir_4[i][1]); }
  return ret;
}

/*int f(int t,int m){
        rep(i,502){
                rep(j,502){
                        a[i][j] = 1;
                }
        }
        rep1(i,h[t]){
                rep1(j,w[t]){
                        if(l[t][i][j] <= m)a[i][j] = 0;
                }
        }
        return dfs(y[t],x[t]);
}*/

int C;
bool comp(P1 a, P1 b) {
  if (a.fr != b.fr)
    return a <= b;
  if (a.sc.fr == y[C] && a.sc.sc == x[C])
    return true;
  if (b.sc.fr == y[C] && b.sc.sc == x[C])
    return false;
  return a <= b;
}

int main() {
  while (1) {
    scanf("%d", &r);
    if (r == 0)
      break;
    rep(i, 2) {
      scanf("%d%d%d%d", &w[i], &h[i], &x[i], &y[i]);
      rep1(j, h[i]) {
        rep1(k, w[i]) { scanf("%d", &l[i][j][k]); }
      }
    }

    static int cost[2][250010];
    vector<P1> vec[2];

    vec[0].clear();
    vec[1].clear();

    rep(k, 2) {
      vec[k].pb(mp1(0, -1, -1));
      rep1(i, h[k]) {
        rep1(j, w[k]) { vec[k].pb(mp1(l[k][i][j], i, j)); }
      }
      C = k;
      sort(vec[k].begin(), vec[k].end(), comp);

      rep(i, 502) {
        rep(j, 502) { a[i][j] = -1; }
      }

      cost[k][0] = 0;
      a[y[k]][x[k]] = 1;
      cost[k][1] = 1;

      for (int i = 2; i < vec[k].size(); i++) {
        P p = vec[k][i].sc;
        a[p.fr][p.sc] = 0;
        cost[k][i] = cost[k][i - 1];
        bool t = false;
        rep(j, 4) { t |= (a[p.fr + dir_4[j][0]][p.sc + dir_4[j][1]] == 1); }
        if (t)
          cost[k][i] += dfs(p.fr, p.sc);
      }

      /*rep(i,vec[k].size()){
              printf("%d %d\\n",vec[k][i].fr,cost[k][i]);
      }*/
    }

    int ret = INF;
    int k = vec[1].size() - 1;
    rep(i, vec[0].size()) {
      while (k > 0 && cost[0][i] + cost[1][k - 1] >= r)
        k--;
      if (cost[0][i] + cost[1][k] >= r) {
        ret = min(ret, vec[0][i].fr + vec[1][k].fr);
      }
    }

    printf("%d\\n", ret);

    /*int L = 0,R = 100000000;
    while(L < R){
            int M = (L+R)/2;
            if(f(0,M)+f(1,M) < r)L = M+1;
            else R = M;
    }

    printf("%d\\n",L);*/
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int w, h, x, y, a[505][505], b[505][505], c[505][505];

pair<int, int> make_P(int ma, int mb) {
  pair<int, int> ret;
  ret.first = ma;
  ret.second = mb;
  return ret;
}

pair<int, pair<int, int>> make_PP(int ma, pair<int, int> mb) {
  pair<int, pair<int, int>> ret;
  ret.first = ma;
  ret.second = mb;
  return ret;
}

bool f(pair<int, pair<int, int>> fx, pair<int, pair<int, int>> fy) {
  return fx.first <= fy.first;
}

void ww(int wx, int wy) {
  c[wx][wy] = 1;
  if (b[wx][wy + 1] == 1 && c[wx][wy + 1] == 0)
    ww(wx, wy + 1);
  if (b[wx][wy - 1] == 1 && c[wx][wy - 1] == 0)
    ww(wx, wy - 1);
  if (b[wx + 1][wy] == 1 && c[wx + 1][wy] == 0)
    ww(wx + 1, wy);
  if (b[wx - 1][wy] == 1 && c[wx - 1][wy] == 0)
    ww(wx - 1, wy);
}

vector<int> els(vector<int> vp) {
  vector<int> ret;
  int s = vp.size();
  ret.push_back(vp[0]);
  for (int i = 1; i < s; i++) {
    if (vp[i] != vp[i - 1])
      ret.push_back(vp[i]);
  }
  return ret;
}

vector<pair<int, int>> office() {
  for (int i = 0; i < 505; i++)
    for (int j = 0; j < 505; j++) {
      b[i][j] = 0;
      c[i][j] = 0;
    }
  vector<pair<int, pair<int, int>>> v;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= h; j++)
      v.push_back(make_PP(a[i][j], make_P(i, j)));
  sort(v.begin(), v.end(), f);
  // v=els(v);
  vector<pair<int, int>> ret;
  ret.push_back(make_P(0, 0));
  int s = v.size();
  b[x][y] = 1;
  c[x][y] = 1;
  for (int i = 0; i < s; i++) {
    /*for(int j=1;j<=h;j++)for(int k=1;k<=w;k++){
            if(a[j][k]==v[i]){
                    b[j][k]=1;
                    if(c[j][k+1]+c[j][k-1]+c[j+1][k]+c[j-1][k]!=0)ww(j,k);
            }
    }*/
    // b[v[i].second.first][v[i].second.second]=1;
    // if(c[v[i].second.first][v[i].second.second+1]+c[v[i].second.first][v[i].second.second-1]+c[v[i].second.first+1][v[i].second.second]+c[v[i].second.first-1][v[i].second.second]!=0)ww(v[i].second.first,v[i].second.second);
    int sum = 0;
    for (int j = 1; j <= h; j++)
      for (int k = 1; k <= w; k++)
        sum += c[j][k];
    ret.push_back(make_P(v[i].first, sum));
  }
  return ret;
}

int main() {
  while (1) {
    int r, s[2];
    scanf("%d", &r);
    if (r == 0)
      break;
    vector<pair<int, int>> vvp[2];
    for (int i = 0; i <= 1; i++) {
      for (int k = 0; k < 505; k++)
        for (int j = 0; j < 505; j++)
          a[k][j] = 0;
      scanf("%d%d%d%d", &w, &h, &x, &y);
      for (int j = 1; j <= h; j++)
        for (int k = 1; k <= w; k++)
          scanf("%d", &a[j][k]);
      vvp[i] = office();
      s[i] = vvp[i].size();
    }
    int n = s[1] - 1, ret = 200000000;
    for (int i = 0; i < s[0]; i++) {
      if (n != 0)
        while (vvp[0][i].second + vvp[1][n - 1].second >= r) {
          n--;
          if (n == 0)
            break;
        }
      if (vvp[0][i].second + vvp[1][n].second >= r)
        ret = min(ret, vvp[0][i].first + vvp[1][n].first);
    }
    printf("%d\\n", ret);
  }
  return 0;
}