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

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<LP, ll> edge;
typedef pair<P, P> PP;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int n, m;
set<ll> xs = {}, ys = {};
vector<ll> vx, vy;
edge hol[1010], ver[1010];
map<ll, int> ma_x, ma_y;
bool blocked[10010][10010][4];
bool visited[10010][10010];
bool isINF;
ll ans = 0;
int MAX_X, MAX_Y;
queue<P> que;

void bfs(int x, int y) {
  que.push(P(x, y));
  // cout << x << " " << y << endl;
  if (isINF)
    return;
  visited[x][y] = true;
  while (!que.empty()) {
    int x = que.front().first, y = que.front().second;
    visited[x][y] = true;
    rep(k, 4) {
      int nx = x + dx[k], ny = y + dy[k];
      if (blocked[x][y][k])
        continue;
      if (nx >= 0 && ny >= 0)
        if (visited[nx][ny])
          continue;
      if (nx < 0 || nx >= MAX_X || ny < 0 || ny >= MAX_Y) {
        isINF = true;
        return;
      }
      visited[nx][ny] = true;
      que.push(P(nx, ny));
    }
    que.pop();
  }
}

void solve() {
  cin >> n >> m;
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b >> c;
    hol[i] = edge(LP(a, b), c);
    xs.insert(c);
    ys.insert(a);
    ys.insert(b);
  }
  rep(i, m) {
    ll a, b, c;
    cin >> c >> a >> b;
    ver[i] = edge(LP(a, b), c);
    ys.insert(c);
    xs.insert(a);
    xs.insert(b);
  }
  MAX_X = xs.size() - 1;
  MAX_Y = ys.size() - 1;
  int id = 0;
  for (ll x : xs) {
    vx.push_back(x);
    ma_x[x] = id;
    id++;
  }
  id = 0;
  for (ll y : ys) {
    vy.push_back(y);
    ma_y[y] = id;
    id++;
  }
  rep(i, n) {
    int a = ma_y[hol[i].first.first], b = ma_y[hol[i].first.second],
        c = ma_x[hol[i].second];
    // cout << a << " " << b << " " << c << endl;
    Rep(y, a, b) {
      if (c - 1 >= 0)
        blocked[c - 1][y][0] = true;
      blocked[c][y][1] = true;
    }
  }
  rep(i, m) {
    int a = ma_x[ver[i].first.first], b = ma_x[ver[i].first.second],
        c = ma_y[ver[i].second];
    Rep(x, a, b) {
      if (c - 1 >= 0)
        blocked[x][c - 1][2] = true;
      blocked[x][c][3] = true;
    }
  }
  // Rep(i,-1,MAX_X){
  //     Rep(j,-1,MAX_Y){
  //         cout << "(" << i << "," << j << ") (" << i+1 << "," << j << ")" <<
  //         " " << blocked[PP(P(i,j),P(i+1,j))] << endl; cout << "(" << i <<
  //         "," << j << ") (" << i << "," << j+1 << ")" << " " <<
  //         blocked[PP(P(i,j),P(i,j+1))] << endl;
  //     }
  // }
  // for(auto s:blocked){
  //     cout << "(" << s.first.first.first << "," << s.first.first.second << ")
  //     (" << s.first.second.first << "," << s.first.second.second << ")" <<
  //     endl;
  // }
  // cout << MAX_X << " " << MAX_Y << endl;
  if (*xs.begin() >= 0 || *ys.begin() >= 0) {
    cout << "INF" << endl;
    return;
  }
  auto it_end_x = xs.end(), it_end_y = ys.end();
  it_end_x--;
  it_end_y--;
  if (*it_end_x <= 0 || *it_end_y <= 0) {
    cout << "INF" << endl;
    return;
  }
  int sx = lower_bound(vx.begin(), vx.end(), 0) - vx.begin() - 1;
  int sy = lower_bound(vy.begin(), vy.end(), 0) - vy.begin() - 1;
  bfs(sx, sy);
  if (isINF) {
    cout << "INF" << endl;
    return;
  }
  rep(i, MAX_X) {
    rep(j, MAX_Y) {
      if (visited[i][j]) {
        // cout << i << " " << j << endl;
        ans += (vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(50);
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<LP, ll> edge;
typedef pair<P, P> PP;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int n, m;
set<ll> xs = {}, ys = {};
vector<ll> vx, vy;
edge hol[1010], ver[1010];
map<ll, int> ma_x, ma_y;
bool blocked[10010][10010][4];
bool visited[10010][10010];
bool isINF;
ll ans = 0;
int MAX_X, MAX_Y;
queue<P> que;

void bfs(int x, int y) {
  que.push(P(x, y));
  // cout << x << " " << y << endl;
  if (isINF)
    return;
  visited[x][y] = true;
  while (!que.empty()) {
    int x = que.front().first, y = que.front().second;
    visited[x][y] = true;
    rep(k, 4) {
      int nx = x + dx[k], ny = y + dy[k];
      if (blocked[x][y][k])
        continue;
      if (nx >= 0 && ny >= 0)
        if (visited[nx][ny])
          continue;
      if (nx < 0 || nx >= MAX_X || ny < 0 || ny >= MAX_Y) {
        isINF = true;
        return;
      }
      que.push(P(nx, ny));
    }
    que.pop();
  }
}

void solve() {
  cin >> n >> m;
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b >> c;
    hol[i] = edge(LP(a, b), c);
    xs.insert(c);
    ys.insert(a);
    ys.insert(b);
  }
  rep(i, m) {
    ll a, b, c;
    cin >> c >> a >> b;
    ver[i] = edge(LP(a, b), c);
    ys.insert(c);
    xs.insert(a);
    xs.insert(b);
  }
  MAX_X = xs.size() - 1;
  MAX_Y = ys.size() - 1;
  int id = 0;
  for (ll x : xs) {
    vx.push_back(x);
    ma_x[x] = id;
    id++;
  }
  id = 0;
  for (ll y : ys) {
    vy.push_back(y);
    ma_y[y] = id;
    id++;
  }
  rep(i, n) {
    int a = ma_y[hol[i].first.first], b = ma_y[hol[i].first.second],
        c = ma_x[hol[i].second];
    // cout << a << " " << b << " " << c << endl;
    Rep(y, a, b) {
      if (c - 1 >= 0)
        blocked[c - 1][y][0] = true;
      blocked[c][y][1] = true;
    }
  }
  rep(i, m) {
    int a = ma_x[ver[i].first.first], b = ma_x[ver[i].first.second],
        c = ma_y[ver[i].second];
    Rep(x, a, b) {
      if (c - 1 >= 0)
        blocked[x][c - 1][2] = true;
      blocked[x][c][3] = true;
    }
  }
  // Rep(i,-1,MAX_X){
  //     Rep(j,-1,MAX_Y){
  //         cout << "(" << i << "," << j << ") (" << i+1 << "," << j << ")" <<
  //         " " << blocked[PP(P(i,j),P(i+1,j))] << endl; cout << "(" << i <<
  //         "," << j << ") (" << i << "," << j+1 << ")" << " " <<
  //         blocked[PP(P(i,j),P(i,j+1))] << endl;
  //     }
  // }
  // for(auto s:blocked){
  //     cout << "(" << s.first.first.first << "," << s.first.first.second << ")
  //     (" << s.first.second.first << "," << s.first.second.second << ")" <<
  //     endl;
  // }
  // cout << MAX_X << " " << MAX_Y << endl;
  if (*xs.begin() >= 0 || *ys.begin() >= 0) {
    cout << "INF" << endl;
    return;
  }
  auto it_end_x = xs.end(), it_end_y = ys.end();
  it_end_x--;
  it_end_y--;
  if (*it_end_x <= 0 || *it_end_y <= 0) {
    cout << "INF" << endl;
    return;
  }
  int sx = lower_bound(vx.begin(), vx.end(), 0) - vx.begin() - 1;
  int sy = lower_bound(vy.begin(), vy.end(), 0) - vy.begin() - 1;
  bfs(sx, sy);
  if (isINF) {
    cout << "INF" << endl;
    return;
  }
  rep(i, MAX_X) {
    rep(j, MAX_Y) {
      if (visited[i][j]) {
        // cout << i << " " << j << endl;
        ans += (vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(50);
  solve();
}