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

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define kill() return puts("INF"), 0
using namespace std;
using ll = long long;
using pr = pair<int, int>;
using pir = pair<int, pr>;
using pri = pair<pr, int>;

int N, M, R, C;
pri A[1005];
pir B[1005];
vector<int> cx, cy;
vector<vector<int>> v;
vector<vector<bool>> chk1, chk2;
queue<pr> q;
ll ans;

int xidx(int x) { return lower_bound(all(cx), x) - cx.begin() + 1; }

int yidx(int y) { return lower_bound(all(cy), y) - cy.begin() + 1; }

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 1, x, y, z; i <= N; i++) {
    scanf("%d %d %d", &x, &y, &z);
    A[i] = pri(pr(x, y), z);
    cx.push_back(x), cx.push_back(y);
    cy.push_back(z);
  }
  for (int i = 1, x, y, z; i <= M; i++) {
    scanf("%d %d %d", &x, &y, &z);
    B[i] = pir(x, pr(y, z));
    cx.push_back(x);
    cy.push_back(y), cy.push_back(z);
  }
  cx.push_back(0), cy.push_back(0);
  sort(all(cx)), sort(all(cy));
  cx.resize(unique(all(cx)) - cx.begin());
  cy.resize(unique(all(cy)) - cy.begin());
  R = sz(cx), C = sz(cy);
  v.resize(R + 2), chk1.resize(R + 2), chk2.resize(R + 2);
  for (int i = 0; i < R + 2; i++)
    v[i].resize(C + 2), chk1[i].resize(C + 2), chk2[i].resize(C + 2);
  for (int i = 1; i <= N; i++) {
    A[i].first.first = xidx(A[i].first.first);
    A[i].first.second = xidx(A[i].first.second);
    A[i].second = yidx(A[i].second);
    for (int j = A[i].first.first + 1; j <= A[i].first.second; j++)
      chk1[j][A[i].second] = 1;
  }
  for (int i = 1; i <= M; i++) {
    B[i].first = xidx(B[i].first);
    B[i].second.first = yidx(B[i].second.first);
    B[i].second.second = yidx(B[i].second.second);
    for (int j = B[i].second.first + 1; j <= B[i].second.second; j++)
      chk2[B[i].first][j] = 1;
  }
  int sx = xidx(0), sy = yidx(0);
  v[sx][sy] = 1, q.push(pr(sx, sy));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x == 1 || x == R + 1 || y == 1 || y == C + 1)
      kill();
    ans += 1LL * (cx[x - 1] - cx[x - 2]) * (cy[y - 1] - cy[y - 2]);
    if (!v[x - 1][y] && !(chk2[x - 1][y])) {
      v[x - 1][y] = 1;
      q.push(pr(x - 1, y));
    }
    if (!v[x + 1][y] && !(chk2[x][y])) {
      v[x + 1][y] = 1;
      q.push(pr(x + 1, y));
    }
    if (!v[x][y - 1] && !(chk1[x][y - 1])) {
      v[x][y - 1] = 1;
      q.push(pr(x, y - 1));
    }
    if (!v[x][y + 1] && !(chk1[x][y])) {
      v[x][y + 1] = 1;
      q.push(pr(x, y + 1));
    }
  }
  printf("%lld\\n", ans);
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define kill() return puts("INF"), 0
using namespace std;
using ll = long long;
using pr = pair<int, int>;
using pir = pair<int, pr>;
using pri = pair<pr, int>;

int N, M, R, C;
pri A[1005];
pir B[1005];
vector<int> cx, cy;
vector<vector<int>> v;
vector<vector<bool>> chk1, chk2;
queue<pr> q;
ll ans;

int xidx(int x) { return lower_bound(all(cx), x) - cx.begin() + 1; }

int yidx(int y) { return lower_bound(all(cy), y) - cy.begin() + 1; }

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 1, x, y, z; i <= N; i++) {
    scanf("%d %d %d", &x, &y, &z);
    A[i] = pri(pr(x, y), z);
    cx.push_back(x), cx.push_back(y);
    cy.push_back(z);
  }
  for (int i = 1, x, y, z; i <= M; i++) {
    scanf("%d %d %d", &x, &y, &z);
    B[i] = pir(x, pr(y, z));
    cx.push_back(x);
    cy.push_back(y), cy.push_back(z);
  }
  cx.push_back(0), cy.push_back(0);
  sort(all(cx)), sort(all(cy));
  cx.resize(unique(all(cx)) - cx.begin());
  cy.resize(unique(all(cy)) - cy.begin());
  R = sz(cx), C = sz(cy);
  v.resize(R + 2), chk1.resize(R + 2), chk2.resize(R + 1);
  for (int i = 0; i < R + 2; i++)
    v[i].resize(C + 2), chk1[i].resize(C + 2), chk2[i].resize(C + 2);
  for (int i = 1; i <= N; i++) {
    A[i].first.first = xidx(A[i].first.first);
    A[i].first.second = xidx(A[i].first.second);
    A[i].second = yidx(A[i].second);
    for (int j = A[i].first.first; j <= A[i].first.second; j++)
      chk1[j][A[i].second] = 1;
  }
  for (int i = 1; i <= M; i++) {
    B[i].first = xidx(B[i].first);
    B[i].second.first = yidx(B[i].second.first);
    B[i].second.second = yidx(B[i].second.second);
    for (int j = B[i].second.first; j <= B[i].second.second; j++)
      chk2[B[i].first][j] = 1;
  }
  int sx = xidx(0), sy = yidx(0);
  v[sx][sy] = 1, q.push(pr(sx, sy));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x == 1 || x == R + 1 || y == 1 || y == C + 1)
      kill();
    ans += 1LL * (cx[x - 1] - cx[x - 2]) * (cy[y - 1] - cy[y - 2]);
    if (!v[x - 1][y] && !(chk2[x - 1][y] && chk2[x - 1][y - 1])) {
      v[x - 1][y] = 1;
      q.push(pr(x - 1, y));
    }
    if (!v[x + 1][y] && !(chk2[x][y] && chk2[x][y - 1])) {
      v[x + 1][y] = 1;
      q.push(pr(x + 1, y));
    }
    if (!v[x][y - 1] && !(chk1[x][y - 1] && chk1[x - 1][y - 1])) {
      v[x][y - 1] = 1;
      q.push(pr(x, y - 1));
    }
    if (!v[x][y + 1] && !(chk1[x][y] && chk1[x - 1][y])) {
      v[x][y + 1] = 1;
      q.push(pr(x, y + 1));
    }
  }
  printf("%lld\\n", ans);
}