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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(3));
  vector<vector<int>> B(M, vector<int>(3));
  for (auto &a : A)
    for (auto &x : a)
      cin >> x;
  for (auto &a : B)
    for (auto &x : a)
      cin >> x;
  map<int, int> xs, ys;
  xs[0] = 0;
  ys[0] = 0;
  xs[-(1 << 30)] = 0;
  ys[-(1 << 30)] = 0;
  xs[1 << 30] = 0;
  ys[1 << 30] = 0;
  for (auto a : A) {
    xs[a[0]] = 0;
    xs[a[1]] = 0;
    ys[a[2]] = 0;
  }
  for (auto a : B) {
    xs[a[0]] = 0;
    ys[a[1]] = 0;
    ys[a[2]] = 0;
  }
  vector<int> xp, yp;
  {
    int c = 0;
    for (auto &p : xs) {
      xp.push_back(p.first);
      p.second = c++;
    }
  }
  {
    int c = 0;
    for (auto &p : ys) {
      yp.push_back(p.first);
      p.second = c++;
    }
  }
  int nx = xs.size() - 1;
  int ny = ys.size() - 1;
  vector<vector<int>> b(nx, vector<int>(ny));
  for (auto a : A) {
    int x1 = xs[a[0]];
    int x2 = xs[a[1]];
    int y = ys[a[2]];
    for (int x = x1; x < x2; ++x) {
      b[x][y - 1] |= 1;
      b[x][y] |= 2;
    }
  }
  for (auto a : B) {
    int x = xs[a[0]];
    int y1 = ys[a[1]];
    int y2 = ys[a[2]];
    for (int y = y1; y < y2; ++y) {
      b[x - 1][y] |= 4;
      b[x][y] |= 8;
    }
  }
  vector<vector<bool>> painted(nx, vector<bool>(ny));
  queue<pair<int, int>> Q;
  {
    int x0 = xs[0];
    int y0 = ys[0];
    for (int i = 0; i <= 1; ++i)
      for (int j = 0; j <= 1; ++j) {
        int x = x0 - i;
        int y = y0 - j;
        painted[x][y] = true;
        Q.push({x, y});
      }
  }
  const int dx[] = {0, 0, +1, -1};
  const int dy[] = {+1, -1, 0, 0};
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    for (int k = 0; k < 4; ++k)
      if (!((b[x][y] >> k) & 1)) {
        int xx = x + dx[k];
        int yy = y + dy[k];
        if (xx == 0 || xx == nx - 1 || yy == 0 || yy == ny - 1) {
          cout << "INF" << endl;
          return 0;
        }
        if (!painted[xx][yy]) {
          painted[xx][yy] = true;
          Q.push({xx, yy});
        }
      }
  }
  ll res = 0;
  for (int x = 0; x < nx; ++x)
    for (int y = 0; y < ny; ++y)
      if (painted[x][y]) {
        ll xx = xp[x + 1] - xp[x];
        ll yy = yp[y + 1] - yp[y];
        res += xx * yy;
      }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(3));
  vector<vector<int>> B(M, vector<int>(3));
  for (auto &a : A)
    for (auto &x : a)
      cin >> x;
  for (auto &a : B)
    for (auto &x : a)
      cin >> x;
  map<int, int> xs, ys;
  xs[0] = 0;
  ys[0] = 0;
  xs[-(1 << 30)] = 0;
  ys[-(1 << 30)] = 0;
  xs[1 << 30] = 0;
  ys[1 << 30] = 0;
  for (auto a : A) {
    xs[a[0]] = 0;
    xs[a[1]] = 0;
    ys[a[2]] = 0;
  }
  for (auto a : B) {
    xs[a[0]] = 0;
    ys[a[1]] = 0;
    ys[a[2]] = 0;
  }
  vector<int> xp, yp;
  {
    int c = 0;
    for (auto &p : xs) {
      xp.push_back(p.first);
      p.second = c++;
    }
  }
  {
    int c = 0;
    for (auto &p : ys) {
      yp.push_back(p.first);
      p.second = c++;
    }
  }
  int nx = xs.size() - 1;
  int ny = ys.size() - 1;
  vector<vector<int>> b(nx, vector<int>(ny));
  for (auto a : A) {
    int x1 = xs[a[0]];
    int x2 = xs[a[1]];
    int y = ys[a[2]];
    for (int x = x1; x < x2; ++x) {
      b[x][y - 1] |= 1;
      b[x][y] |= 2;
    }
  }
  for (auto a : B) {
    int x = xs[a[0]];
    int y1 = ys[a[1]];
    int y2 = ys[a[2]];
    for (int y = y1; y < y2; ++y) {
      b[x - 1][y] |= 4;
      b[x][y] |= 8;
    }
  }
  vector<vector<bool>> painted(nx, vector<bool>(ny));
  queue<pair<int, int>> Q;
  {
    int x0 = xs[0];
    int y0 = ys[0];
    for (int i = 0; i <= 1; ++i)
      for (int j = 0; j <= 1; ++j) {
        int x = x0 - i;
        int y = y0 - j;
        painted[x][y] = true;
        Q.push({x, y});
      }
  }
  for (int x = 0; x < nx; ++x) {
    for (int y = 0; y < ny; ++y)
      cerr << b[x][y] << " ";
    cerr << endl;
  }
  const int dx[] = {0, 0, +1, -1};
  const int dy[] = {+1, -1, 0, 0};
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    cerr << x << " " << y << " " << b[x][y] << endl;
    Q.pop();
    for (int k = 0; k < 4; ++k)
      if (!((b[x][y] >> k) & 1)) {
        int xx = x + dx[k];
        int yy = y + dy[k];
        cerr << " " << xx << " " << yy << " " << k << endl;
        if (xx == 0 || xx == nx - 1 || yy == 0 || yy == ny - 1) {
          cout << "INF" << endl;
          return 0;
        }
        if (!painted[xx][yy]) {
          painted[xx][yy] = true;
          Q.push({xx, yy});
        }
      }
  }
  ll res = 0;
  for (int x = 0; x < nx; ++x)
    for (int y = 0; y < ny; ++y)
      if (painted[x][y]) {
        ll xx = xp[x + 1] - xp[x];
        ll yy = yp[y + 1] - yp[y];
        res += xx * yy;
      }
  cout << res << endl;
}