Problem:
Score: 600 points

Problem Statement

There is a grass field that stretches infinitely.

In this field, there is a negligibly small cow. Let (x, y) denote the point that is x\ \mathrm{cm} south and y\ \mathrm{cm} east of the point where the cow stands now. The cow itself is standing at (0, 0).

There are also N north-south lines and M east-west lines drawn on the field. The i-th north-south line is the segment connecting the points (A_i, C_i) and (B_i, C_i), and the j-th east-west line is the segment connecting the points (D_j, E_j) and (D_j, F_j).

What is the area of the region the cow can reach when it can move around as long as it does not cross the segments (including the endpoints)? If this area is infinite, print INF instead.

Constraints

All values in input are integers between -10^9 and 10^9 (inclusive).

1 \leq N, M \leq 1000

A_i < B_i\ (1 \leq i \leq N)

E_j < F_j\ (1 \leq j \leq M)

The point (0, 0) does not lie on any of the given segments.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
:
A_N B_N C_N
D_1 E_1 F_1
:
D_M E_M F_M

Output

If the area of the region the cow can reach is infinite, print INF; otherwise, print an integer representing the area in \mathrm{cm^2}.

(Under the constraints, it can be proved that the area of the region is always an integer if it is not infinite.)

Sample Input 1

5 6
1 2 0
0 1 1
0 2 2
-3 4 -1
-2 6 3
1 0 1
0 1 2
2 0 2
-1 -4 5
3 -2 4
1 2 4

Sample Output 1

13

The area of the region the cow can reach is 13\ \mathrm{cm^2}.

Sample Input 2

6 1
-3 -1 -2
-3 -1 1
-2 -1 2
1 4 -2
1 4 -1
1 4 1
3 1 4

Sample Output 2

INF

The area of the region the cow can reach is infinite.

Solution in C++ (GCC 9.2.1) (judge verdict: Runtime Error)

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

using Size = int;
template<typename T> class Compression {
 public:
  Compression(const std::vector<T>& list) : value_(list) {
    std::sort(begin(value_), end(value_));
    value_.erase(std::unique(begin(value_), end(value_)), end(value_));
    for(Size i = 0; i < value_.size(); ++i) index_[value_[i]] = i;
  }
  Size size() const {return value_.size();}
  Size index(const T& v) const {return index_.at(v);}
  T operator[](const Size i) const {return value_.at(i);}
 private:
  std::unordered_map<T, Size> index_;
  std::vector<T> value_;
};

int main() {
  using LL = long long;
  struct Segment {LL x, m, y;};
  const LL INF = 5e18;

  int N, M;
  cin >> N >> M;
  vector<Segment> V(N), H(M);
  for(auto p: {&V, &H}) for(auto& s: *p) cin >> s.x >> s.m >> s.y;

  vector<LL> P = {-INF, 0, INF};
  for(auto p: {&V, &H}) for(auto& s: *p) {
    P.emplace_back(s.x);
    P.emplace_back(s.m);
    P.emplace_back(s.y);
  }
  Compression C(P);

  // (0,-1) -> (-1,0) -> (0,1) -> (1,0)
  enum {L, U, R, D};
  const int NEXT[5] = {0, -1, 0, 1, 0};
  vector<vector<vector<bool>>> ng(4, vector<vector<bool>>(C.size()-1, vector<bool>(C.size()-1)));
  for(auto i=0; i<C.size()-1; ++i) ng[U][C.index(-INF)][i]  = ng[L][i][C.index(-INF)]  = true;
  for(auto i=0; i<C.size()-1; ++i) ng[D][C.index(INF)-1][i] = ng[R][i][C.index(INF)-1] = true;
  for(const auto& s: V) for(auto x=C.index(s.x), y=C.index(s.y); x<C.index(s.m); ++x) ng[R][x][y-1] = ng[L][x][y] = true;
  for(const auto& s: H) for(auto x=C.index(s.x), y=C.index(s.m); y<C.index(s.y); ++y) ng[D][x-1][y] = ng[U][x][y] = true;

  vector<vector<bool>> used(C.size()-1, vector<bool>(C.size()-1));
  function<LL(int,int)> dfs = [&](auto i, auto j) {
    assert(0<=i&&i<C.size()-1);
    assert(0<=j&&j<C.size()-1);
    if(used[i][j]) return 0ll;
    used[i][j] = true;
    auto lx = C[i], hx = C[i+1];
    auto ly = C[j], hy = C[j+1];
    if(lx==-INF || hx==INF || ly==-INF || hy==INF) return INF;
    auto res = (hx-lx) * (hy-ly);
    for(auto k=0; k<4; ++k) if(!ng[k][i][j]) res = min(INF, res+dfs(i+NEXT[k], j+NEXT[k+1]));
    return res;
  };

  auto area = dfs(C.index(0), C.index(0));
  if(area != INF) cout << area << endl;
  else            cout << "INF" << endl;
}