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

#include <bits/stdc++.h>
using namespace std;

#define lli long long
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define rep0(i, j) for (int i = 0; i < j; i++)
#define rep1(i, j) for (int i = 1; i <= j; i++)
#define rep0d(i, j) for (int i = j - 1; i >= 0; i--)
#define MOD 1000000007
#define MAX (lli)1e12
#define d(a, b) distance(a.begin(), a.find(b))
typedef pair<lli, lli> p;
class Graph {
  int V;
  vector<list<p>> adj;
  const int INF = (int)1e6;

public:
  Graph(lli V) : adj(V) { this->V = V; }

  void addEdge(int u, int v, int w) { adj[u].push_back(make_pair(v, w)); }

  lli edgeNum() {
    lli sum = 0;
    rep0(i, V) sum += adj[i].size();
    return sum;
  }

  bool route(lli from, lli to) { // vec[i]==j: j->iっていうもとに戻れる道がある
    vector<lli> vec(V, -1);
    dfs(1, from, vec);
    return vec[to] >= 0;
  }

  void dfs(lli ed, lli num, vector<lli> &vec) {
    for (auto e : adj[num]) {
      if (vec[e.first] != -1)
        continue;
      vec[e.first] = num;
      dfs(ed + 1, e.first, vec);
    }
  }

  vector<int> bfs(int n) {
    queue<int> q;
    vector<int> vec(V, -1);

    q.push(n);
    vec[n] = 0;
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (auto e : adj[p]) {
        if (vec[e.first] == -1) {
          q.push(e.first);
          vec[e.first] = e.second + vec[p];
        }
      }
    }
    return vec;
  }

  vector<int> shortestPath(int src) {
    priority_queue<p, vector<p>, greater<p>> pq;

    vector<int> dist(V, INF);
    pq.push(make_pair(0, src));
    dist[src] = 0;

    while (!pq.empty()) {
      int u = pq.top().second;
      pq.pop();

      list<p>::iterator i;
      for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
        int v = i->first;
        int weight = i->second;

        if (dist[v] > dist[u] + weight) {
          dist[v] = dist[u] + weight;
          pq.push(make_pair(dist[v], v));
        }
      }
    }
    for (int i = 0; i < V; i++)
      if (dist[i] == INF)
        dist[i] = -1;
    return dist;
  }
};

struct UF {
  vector<int> par, size;

  UF(int N) : par(N), size(N, 1) { rep0(i, N) par[i] = i; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
    size[rx] += size[ry];
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int getSize(int x) { return size[x]; }
};
struct BIT {
  vector<lli> tab; // bit table
  BIT(int n) : tab(n + 1) { rep0(i, n + 1) tab[i] = 0; }

  void add(int p, lli num) {
    for (++p; p < tab.size(); p += p & -p)
      tab[p] += num;
  }

  lli sum(int p) { // from 0 to p
    lli ans = 0;
    for (++p; p > 0; p -= p & -p)
      ans += tab[p];
    return ans;
  }

  void debug() {
    rep0(i, tab.size()) cout << tab[i] << ' ';
    cout << endl;
  }
};

// pairに関するもの
lli sum(p c) { return (c.first + c.second); }
p operator-(const p &l, const p &r) {
  return {l.first - r.first, l.second - r.second};
}
bool cmpf(const pair<lli, lli> left, const pair<lli, lli> right) {
  return left.first < right.first;
}
bool cmps(const pair<lli, lli> left, const pair<lli, lli> right) {
  return left.second < right.second;
}

lli gcd(lli a, lli b) {
  if (a < b)
    return gcd(b, a);
  lli temp;
  while ((temp = a % b)) {
    a = b;
    b = temp;
  }
  return b;
}

struct nCr {
  vector<lli> f, rf;
  lli count = 0;
  nCr(lli num) : f(num), rf(num) {
    count = num;
    f[0] = 1;
    rep1(i, num) f[i] = (f[i - 1] * i) % MOD;
    rep0(i, num + 1) rf[i] = inv(f[i]);
  }

  lli inv(lli x) {
    lli res = 1;
    lli k = MOD - 2;
    lli y = x;
    while (k) {
      if (k & 1)
        res = (res * y) % MOD;
      y = (y * y) % MOD;
      k /= 2;
    }
    return res;
  }
  lli C(lli n, lli k) {
    if (k > n || k < 0)
      return 0;
    lli a = f[n];
    lli b = rf[n - k];
    lli c = rf[k];

    lli bc = (b * c) % MOD;
    return (a * bc) % MOD;
  }
};

int main() {
  lli n, m;
  cin >> n >> m;
  vector<vector<lli>> vn(n, vector<lli>(3)), vm(m, vector<lli>(3));
  map<lli, lli> mpv, mph;
  rep0(i, n) {
    cin >> vn[i][0];
    mpv[vn[i][0]]++;
    cin >> vn[i][1];
    mpv[vn[i][1]]++;
    cin >> vn[i][2];
    mph[vn[i][2]]++;
  }
  rep0(i, m) {
    cin >> vm[i][2];
    mpv[vm[i][2]]++;
    cin >> vm[i][0];
    mph[vm[i][0]]++;
    cin >> vm[i][1];
    mph[vm[i][1]]++;
  }

  mpv[0]++;
  mph[0]++;
  vector<lli> vecv(mpv.size()), vech(mph.size());

  auto itrv = mpv.begin();
  rep0(i, mpv.size()) {
    itrv->second = i;
    vecv[i] = itrv->first;
    itrv++;
  }
  auto itrh = mph.begin();
  rep0(i, mph.size()) {
    itrh->second = i;
    vech[i] = itrh->first;
    itrh++;
  }

  lli vsize = mpv.size() * 2 + 1, hsize = mph.size() * 2 + 1;
  vector<vector<lli>> table(vsize + 1, vector<lli>(hsize, 0)),
      check(vsize, vector<lli>(hsize, 0));
  vsize--;
  hsize--;

  rep0(i, n) {
    auto itrh = mph.find(vn[i][2]);
    for (auto itr = mpv.find(vn[i][0]); itr != mpv.find(vn[i][1]); itr++) {
      table[itr->second * 2 + 3][itrh->second * 2 + 1] = 1;
      table[itr->second * 2 + 2][itrh->second * 2 + 1] = 1;
      table[itr->second * 2 + 1][itrh->second * 2 + 1] = 1;
    }
  }
  rep0(i, m) {
    auto itrv = mpv.find(vm[i][2]);
    for (auto itr = mph.find(vm[i][0]); itr != mph.find(vm[i][1]); itr++) {
      table[2 * itrv->second + 1][itr->second * 2 + 1] = 1;
      table[2 * itrv->second + 1][itr->second * 2 + 2] = 1;
      table[2 * itrv->second + 1][itr->second * 2 + 3] = 1;
    }
  }

  queue<p> q;
  int st[][2] = {{1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
  for (auto e : st)
    q.push({d(mpv, 0) * 2 + 1 + e[0], d(mph, 0) * 2 + 1 + e[1]});

  lli ans = 0;
  int mov[][2] = {{0, 2}, {0, -2}, {2, 0}, {-2, 0}};
  while (!q.empty()) {
    p tp = q.front();
    q.pop();
    if (tp.first <= 0 || tp.first >= vsize || tp.second <= 0 ||
        tp.second >= hsize) {
      cout << "INF\\n";
      return 0;
      // cout << tp.first << ' ' << tp.second << ' ' << endl;
      // break;
    } else {
      // cout << tp.first << ' ' << tp.second << ' ' << (next(mph.begin(),
      // tp.first/2+1)->first - next(mph.begin(), tp.first/2)->first)
      //       << ' ' << (next(mpv.begin(), tp.second/2+1)->first -
      //       next(mpv.begin(), tp.second/2)->first )  << endl;
      if (check[tp.first][tp.second] == 1)
        continue;
      // ans+= (next(mph.begin(), tp.first/2+1)->first - next(mph.begin(),
      // tp.first/2)->first)
      //       * (next(mpv.begin(), tp.second/2+1)->first - next(mpv.begin(),
      //       tp.second/2)->first);
      ans += (vecv[tp.first / 2] - vecv[tp.first / 2 - 1]) *
             (vech[tp.second / 2] - vech[tp.second / 2 - 1]);
      // cout << tp.first << ' ' << tp.second << ' ' << (vecv[tp.first/2] -
      // vecv[tp.first/2-1])
      //         << ' ' << (vech[tp.second/2] - vech[tp.second/2-1]) << endl;
      check[tp.first][tp.second] = 1;
      for (auto e : mov) {
        if (table[e[0] / 2 + tp.first][e[1] / 2 + tp.second] == 0) {
          if (check[e[0] + tp.first][e[1] + tp.second] != 1) {
            q.push({e[0] + tp.first, e[1] + tp.second});
          }
        }
      }
    }
  }
  cout << ans << endl;

  //  rep0(i,vsize+1){
  //    rep0(j,hsize+1){
  //      cout << table[i][j] << ' ';
  //    }
  //    cout << endl;
  //  }
  //
  // cout << endl;
  //  rep0(i,vsize+1){
  //    rep0(j,hsize+1){
  //      cout << check[i][j] << ' ';
  //    }
  //    cout << endl;
  //  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define lli long long
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define rep0(i, j) for (int i = 0; i < j; i++)
#define rep1(i, j) for (int i = 1; i <= j; i++)
#define rep0d(i, j) for (int i = j - 1; i >= 0; i--)
#define MOD 1000000007
#define MAX (lli)1e12
#define d(a, b) distance(a.begin(), a.find(b))
typedef pair<lli, lli> p;
class Graph {
  int V;
  vector<list<p>> adj;
  const int INF = (int)1e6;

public:
  Graph(lli V) : adj(V) { this->V = V; }

  void addEdge(int u, int v, int w) { adj[u].push_back(make_pair(v, w)); }

  lli edgeNum() {
    lli sum = 0;
    rep0(i, V) sum += adj[i].size();
    return sum;
  }

  bool route(lli from, lli to) { // vec[i]==j: j->iっていうもとに戻れる道がある
    vector<lli> vec(V, -1);
    dfs(1, from, vec);
    return vec[to] >= 0;
  }

  void dfs(lli ed, lli num, vector<lli> &vec) {
    for (auto e : adj[num]) {
      if (vec[e.first] != -1)
        continue;
      vec[e.first] = num;
      dfs(ed + 1, e.first, vec);
    }
  }

  vector<int> bfs(int n) {
    queue<int> q;
    vector<int> vec(V, -1);

    q.push(n);
    vec[n] = 0;
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (auto e : adj[p]) {
        if (vec[e.first] == -1) {
          q.push(e.first);
          vec[e.first] = e.second + vec[p];
        }
      }
    }
    return vec;
  }

  vector<int> shortestPath(int src) {
    priority_queue<p, vector<p>, greater<p>> pq;

    vector<int> dist(V, INF);
    pq.push(make_pair(0, src));
    dist[src] = 0;

    while (!pq.empty()) {
      int u = pq.top().second;
      pq.pop();

      list<p>::iterator i;
      for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
        int v = i->first;
        int weight = i->second;

        if (dist[v] > dist[u] + weight) {
          dist[v] = dist[u] + weight;
          pq.push(make_pair(dist[v], v));
        }
      }
    }
    for (int i = 0; i < V; i++)
      if (dist[i] == INF)
        dist[i] = -1;
    return dist;
  }
};

struct UF {
  vector<int> par, size;

  UF(int N) : par(N), size(N, 1) { rep0(i, N) par[i] = i; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
    size[rx] += size[ry];
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int getSize(int x) { return size[x]; }
};
struct BIT {
  vector<lli> tab; // bit table
  BIT(int n) : tab(n + 1) { rep0(i, n + 1) tab[i] = 0; }

  void add(int p, lli num) {
    for (++p; p < tab.size(); p += p & -p)
      tab[p] += num;
  }

  lli sum(int p) { // from 0 to p
    lli ans = 0;
    for (++p; p > 0; p -= p & -p)
      ans += tab[p];
    return ans;
  }

  void debug() {
    rep0(i, tab.size()) cout << tab[i] << ' ';
    cout << endl;
  }
};

// pairに関するもの
lli sum(p c) { return (c.first + c.second); }
p operator-(const p &l, const p &r) {
  return {l.first - r.first, l.second - r.second};
}
bool cmpf(const pair<lli, lli> left, const pair<lli, lli> right) {
  return left.first < right.first;
}
bool cmps(const pair<lli, lli> left, const pair<lli, lli> right) {
  return left.second < right.second;
}

lli gcd(lli a, lli b) {
  if (a < b)
    return gcd(b, a);
  lli temp;
  while ((temp = a % b)) {
    a = b;
    b = temp;
  }
  return b;
}

struct nCr {
  vector<lli> f, rf;
  lli count = 0;
  nCr(lli num) : f(num), rf(num) {
    count = num;
    f[0] = 1;
    rep1(i, num) f[i] = (f[i - 1] * i) % MOD;
    rep0(i, num + 1) rf[i] = inv(f[i]);
  }

  lli inv(lli x) {
    lli res = 1;
    lli k = MOD - 2;
    lli y = x;
    while (k) {
      if (k & 1)
        res = (res * y) % MOD;
      y = (y * y) % MOD;
      k /= 2;
    }
    return res;
  }
  lli C(lli n, lli k) {
    if (k > n || k < 0)
      return 0;
    lli a = f[n];
    lli b = rf[n - k];
    lli c = rf[k];

    lli bc = (b * c) % MOD;
    return (a * bc) % MOD;
  }
};

int main() {
  lli n, m;
  cin >> n >> m;
  vector<vector<lli>> vn(n, vector<lli>(3)), vm(m, vector<lli>(3));
  // map<lli,lli> mpv, mph;
  set<lli> mpv, mph;
  rep0(i, n) {
    //    cin >> vn[i][0]; mpv[vn[i][0]]++;
    //    cin >> vn[i][1]; mpv[vn[i][1]]++;
    //    cin >> vn[i][2]; mph[vn[i][2]]++;
    cin >> vn[i][0];
    mpv.insert(vn[i][0]);
    cin >> vn[i][1];
    mpv.insert(vn[i][1]);
    cin >> vn[i][2];
    mph.insert(vn[i][2]);
  }
  rep0(i, m) {
    //    cin >> vm[i][2]; mpv[vm[i][2]]++;
    //    cin >> vm[i][0]; mph[vm[i][0]]++;
    //    cin >> vm[i][1]; mph[vm[i][1]]++;
    cin >> vm[i][2];
    mpv.insert(vm[i][2]);
    cin >> vm[i][0];
    mph.insert(vm[i][0]);
    cin >> vm[i][1];
    mph.insert(vm[i][1]);
  }

  //  mpv[0]++; mph[0]++;
  mpv.insert(0);
  mph.insert(0);
  vector<lli> vecv(mpv.size()), vech(mph.size());

  auto itrv = mpv.begin();
  rep0(i, mpv.size()) {
    //    vecv[i]=itrv->first;
    vecv[i] = *itrv;
    itrv++;
  }
  auto itrh = mph.begin();
  rep0(i, mph.size()) {
    //    vech[i]=itrh->first;
    vech[i] = *itrh;
    itrh++;
  }

  lli vsize = mpv.size() * 2 + 1, hsize = mph.size() * 2 + 1;
  vector<vector<lli>> table(vsize + 1, vector<lli>(hsize, 0)),
      check(vsize, vector<lli>(hsize, 0));
  vsize--;
  hsize--;

  rep0(i, n) {
    for (auto itr = mpv.find(vn[i][0]); itr != mpv.find(vn[i][1]); itr++) {
      table[distance(mpv.begin(), itr) * 2 + 3][d(mph, vn[i][2]) * 2 + 1] = 1;
      table[distance(mpv.begin(), itr) * 2 + 2][d(mph, vn[i][2]) * 2 + 1] = 1;
      table[distance(mpv.begin(), itr) * 2 + 1][d(mph, vn[i][2]) * 2 + 1] = 1;
    }
  }
  rep0(i, m) {
    for (auto itr = mph.find(vm[i][0]); itr != mph.find(vm[i][1]); itr++) {
      table[2 * d(mpv, vm[i][2]) + 1][distance(mph.begin(), itr) * 2 + 1] = 1;
      table[2 * d(mpv, vm[i][2]) + 1][distance(mph.begin(), itr) * 2 + 2] = 1;
      table[2 * d(mpv, vm[i][2]) + 1][distance(mph.begin(), itr) * 2 + 3] = 1;
    }
  }

  queue<p> q;
  int st[][2] = {{1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
  for (auto e : st)
    q.push({d(mpv, 0) * 2 + 1 + e[0], d(mph, 0) * 2 + 1 + e[1]});

  lli ans = 0;
  int mov[][2] = {{0, 2}, {0, -2}, {2, 0}, {-2, 0}};
  while (!q.empty()) {
    p tp = q.front();
    q.pop();
    if (tp.first <= 0 || tp.first >= vsize || tp.second <= 0 ||
        tp.second >= hsize) {
      cout << "INF\\n";
      return 0;
      // cout << tp.first << ' ' << tp.second << ' ' << endl;
      // break;
    } else {
      // cout << tp.first << ' ' << tp.second << ' ' << (next(mph.begin(),
      // tp.first/2+1)->first - next(mph.begin(), tp.first/2)->first)
      //       << ' ' << (next(mpv.begin(), tp.second/2+1)->first -
      //       next(mpv.begin(), tp.second/2)->first )  << endl;
      if (check[tp.first][tp.second] == 1)
        continue;
      // ans+= (next(mph.begin(), tp.first/2+1)->first - next(mph.begin(),
      // tp.first/2)->first)
      //       * (next(mpv.begin(), tp.second/2+1)->first - next(mpv.begin(),
      //       tp.second/2)->first);
      ans += (vecv[tp.first / 2] - vecv[tp.first / 2 - 1]) *
             (vech[tp.second / 2] - vech[tp.second / 2 - 1]);
      // cout << tp.first << ' ' << tp.second << ' ' << (vecv[tp.first/2] -
      // vecv[tp.first/2-1])
      //         << ' ' << (vech[tp.second/2] - vech[tp.second/2-1]) << endl;
      check[tp.first][tp.second] = 1;
      for (auto e : mov) {
        if (table[e[0] / 2 + tp.first][e[1] / 2 + tp.second] == 0) {
          if (check[e[0] + tp.first][e[1] + tp.second] != 1) {
            q.push({e[0] + tp.first, e[1] + tp.second});
          }
        }
      }
    }
  }
  cout << ans << endl;

  //  rep0(i,vsize+1){
  //    rep0(j,hsize+1){
  //      cout << table[i][j] << ' ';
  //    }
  //    cout << endl;
  //  }
  //
  // cout << endl;
  //  rep0(i,vsize+1){
  //    rep0(j,hsize+1){
  //      cout << check[i][j] << ' ';
  //    }
  //    cout << endl;
  //  }
}