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

#include <bits/stdc++.h>

using namespace std;
#define int long long
map<int, vector<pair<int, int>>> u1;
map<int, vector<pair<int, int>>> u2;
vector<int> keys1;
vector<int> keys2;
bool check1(int x, int y, int z) {
  vector<pair<int, int>> v = u2[keys2[y]];
  for (int i = 0; i < v.size(); ++i) {
    if (v[i].first <= keys1[x - 1] && v[i].second >= keys1[x]) {
      return false;
    }
  }
  return true;
}
bool check2(int x, int y, int z) {
  vector<pair<int, int>> v = u1[keys1[y]];
  for (int i = 0; i < v.size(); ++i) {
    if (v[i].first <= keys2[x - 1] && v[i].second >= keys2[x]) {
      return false;
    }
  }
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  pair<int, pair<int, int>> a[n];
  pair<int, pair<int, int>> b[m];
  set<int> o1;
  set<int> o2;
  for (int i = 0; i < n; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    a[i] = {z, {x, y}};
    if (!o1.count(z)) {
      vector<pair<int, int>> v;
      v.push_back({x, y});
      u1[z] = v;
      o1.insert(z);
      keys1.push_back(z);
    } else {
      u1[z].push_back({x, y});
    }
  }
  for (int i = 0; i < m; ++i) {
    int x, y, z;
    cin >> z >> x >> y;
    b[i] = {z, {x, y}};
    if (!o2.count(z)) {
      vector<pair<int, int>> v;
      v.push_back({x, y});
      u2[z] = v;
      o2.insert(z);
      keys2.push_back(z);
    } else {
      u2[z].push_back({x, y});
    }
  }
  sort(keys1.begin(), keys1.end());
  sort(keys2.begin(), keys2.end());
  int n1 = keys1.size();
  int m1 = keys2.size();
  int s[n1 + 1][m1 + 1];
  for (int i = 0; i <= n1; ++i) {
    for (int j = 0; j <= m1; ++j) {
      if (i == 0 || i == n1) {
        s[i][j] = (-1);
        continue;
      }
      if (j == 0 || j == m1) {
        s[i][j] = (-1);
        continue;
      }
      s[i][j] = (keys1[i] - keys1[i - 1]) * (keys2[j] - keys2[j - 1]);
    }
  }
  // cout<<keys2[1]<<" keys2[1] "<<endl;
  int x1 = upper_bound(keys1.begin(), keys1.end(), 0) - keys1.begin();
  int y1 = upper_bound(keys2.begin(), keys2.end(), 0) - keys2.begin();
  deque<pair<int, int>> q;
  vector<pair<int, int>> v;
  bool used[n + 1][m + 1];
  q.push_back({x1, y1});
  v.push_back({x1, y1});
  if (keys1[x1 - 1] == 0) {
    q.push_back({x1 - 1, y1});
    v.push_back({x1 - 1, y1});
  }
  if (keys2[y1 - 1] == 0) {
    q.push_back({x1, y1 - 1});
    v.push_back({x1, y1 - 1});
  }
  if (keys1[x1 - 1] == 0 && keys2[y1 - 1] == 0) {
    q.push_back({x1 - 1, y1 - 1});
    v.push_back({x1 - 1, y1 - 1});
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      used[i][j] = false;
    }
  }
  for (int i = 0; i < v.size(); ++i) {
    used[v[i].first][v[i].second] = true;
  }
  int ans = 0;
  while (!q.empty()) {
    deque<pair<int, int>> q1;
    while (!q.empty()) {
      pair<int, int> o = q.front();
      q.pop_front();
      int x = o.first;
      int y = o.second;
      // cout<<x<<" "<<y<<" xy "<<endl;
      // cout<<s[x][y]<<" s[x][y] "<<endl;
      ans += s[x][y];
      if (s[x][y] == (-1)) {
        cout << "INF";
        return 0;
      }
      if (!used[x][y - 1] && check1(x, y - 1, y)) {
        q1.push_back({x, y - 1});
        used[x][y - 1] = true;
      }
      if (!used[x][y + 1] && check1(x, y, y + 1)) {
        q1.push_back({x, y + 1});
        used[x][y + 1] = true;
      }
      if (!used[x - 1][y] && check2(y, x - 1, x)) {
        q1.push_back({x - 1, y});
        used[x - 1][y] = true;
      }
      if (!used[x + 1][y] && check2(y, x, x + 1)) {
        q1.push_back({x + 1, y});
        used[x + 1][y] = true;
      }
    }
    q = q1;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define int long long
map<int, vector<pair<int, int>>> u1;
map<int, vector<pair<int, int>>> u2;
vector<int> keys1;
vector<int> keys2;
bool check1(int x, int y, int z) {
  // cout<<x<<" "<<y<<" "<<z<<" xyz "<<endl;
  vector<pair<int, int>> v = u1[keys2[y]];
  // cout<<v.size()<<" sz "<<endl;
  for (int i = 0; i < v.size(); ++i) {
    // cout<<v[i].first<<" "<<v[i].second<<endl;
    if (v[i].first <= keys1[x - 1] && v[i].second >= keys1[x]) {
      return false;
    }
  }
  // cout<<" can "<<endl;
  return true;
}
bool check2(int x, int y, int z) {
  vector<pair<int, int>> v = u2[keys1[y]];
  for (int i = 0; i < v.size(); ++i) {
    if (v[i].first <= keys2[x - 1] && v[i].second >= keys2[x]) {
      return false;
    }
  }
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  pair<int, pair<int, int>> a[n];
  pair<int, pair<int, int>> b[m];
  set<int> o1;
  set<int> o2;
  for (int i = 0; i < n; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    a[i] = {z, {x, y}};
    if (!o1.count(z)) {
      vector<pair<int, int>> v;
      v.push_back({x, y});
      u1[z] = v;
      o1.insert(z);
      keys1.push_back(z);
    } else {
      u1[z].push_back({x, y});
    }
  }
  for (int i = 0; i < m; ++i) {
    int x, y, z;
    cin >> z >> x >> y;
    b[i] = {z, {x, y}};
    if (!o2.count(z)) {
      vector<pair<int, int>> v;
      v.push_back({x, y});
      u2[z] = v;
      o2.insert(z);
      keys2.push_back(z);
    } else {
      u2[z].push_back({x, y});
    }
  }
  sort(keys1.begin(), keys1.end());
  sort(keys2.begin(), keys2.end());
  int n1 = keys1.size();
  int m1 = keys2.size();
  int s[n1 + 1][m1 + 1];
  for (int i = 0; i <= n1; ++i) {
    for (int j = 0; j <= m1; ++j) {
      if (i == 0 || i == n1) {
        s[i][j] = (-1);
        continue;
      }
      if (j == 0 || j == m1) {
        s[i][j] = (-1);
        continue;
      }
      s[i][j] = (keys1[i] - keys1[i - 1]) * (keys2[j] - keys2[j - 1]);
    }
  }
  // cout<<keys2[1]<<" keys2[1] "<<endl;
  int x1 = upper_bound(keys1.begin(), keys1.end(), 0) - keys1.begin();
  int y1 = upper_bound(keys2.begin(), keys2.end(), 0) - keys2.begin();
  deque<pair<int, int>> q;
  vector<pair<int, int>> v;
  bool used[n + 1][m + 1];
  q.push_back({x1, y1});
  v.push_back({x1, y1});
  if (keys1[x1 - 1] == 0) {
    q.push_back({x1 - 1, y1});
    v.push_back({x1 - 1, y1});
  }
  if (keys2[y1 - 1] == 0) {
    q.push_back({x1, y1 - 1});
    v.push_back({x1, y1 - 1});
  }
  if (keys1[x1 - 1] == 0 && keys2[y1 - 1] == 0) {
    q.push_back({x1 - 1, y1 - 1});
    v.push_back({x1 - 1, y1 - 1});
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      used[i][j] = false;
    }
  }
  for (int i = 0; i < v.size(); ++i) {
    used[v[i].first][v[i].second] = true;
  }
  int ans = 0;
  while (!q.empty()) {
    deque<pair<int, int>> q1;
    while (!q.empty()) {
      pair<int, int> o = q.front();
      q.pop_front();
      int x = o.first;
      int y = o.second;
      // cout<<x<<" "<<y<<" xy "<<endl;
      // cout<<s[x][y]<<" s[x][y] "<<endl;
      ans += s[x][y];
      if (s[x][y] == (-1)) {
        cout << "INF";
        return 0;
      }
      if (!used[x][y - 1] && check1(x, y - 1, y)) {
        q1.push_back({x, y - 1});
        used[x][y - 1] = true;
      }
      if (!used[x][y + 1] && check1(x, y, y + 1)) {
        q1.push_back({x, y + 1});
        used[x][y + 1] = true;
      }
      if (!used[x - 1][y] && check2(y, x - 1, x)) {
        q1.push_back({x - 1, y});
        used[x - 1][y] = true;
      }
      if (!used[x + 1][y] && check2(y, x, x + 1)) {
        q1.push_back({y, x + 1});
        used[x + 1][y] = true;
      }
    }
    q = q1;
  }
  cout << ans;
  return 0;
}