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: Accepted)

 

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <math.h>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
 
 
#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s
 
typedef long long ll;
 
using namespace std;

template<class T>
class depth_first_search_iterator {
  public:
    stack<pair<T,T>> todo;
    function<bool(T)> notvisited;
    function<void(T)> mark_visited;
    function<set<T>(T)> neighbors;

    depth_first_search_iterator() {}

    depth_first_search_iterator(T start, function<bool(T)> notv, function<void(T)> mark_v, function<set<T>(T)> neigh) :
      notvisited(notv), mark_visited(mark_v), neighbors(neigh), end_(false), current(start), prev(start) {
          todo.push(make_pair(start, start)); operator++();
    }

    void operator++() {
        end_ = true;
        while (!todo.empty()) {
            current = todo.top().first; prev = todo.top().second; todo.pop();
            if (notvisited(current)) {
                mark_visited(current);
                for (const T x : neighbors(current)) {  todo.push(make_pair(x, current));   }
                end_ = false;
                break;
            }
        }
    }

    T operator*() {     return current;    }

    T previous() {  return prev;  }

    bool end() {  return end_;  }

  private:
    T current; // contains current node
    T prev;    // contains previous node
    bool end_;
};
 
 
int main()   {
    cout.precision(17);
    ll modd = 1000 * 1000 * 1000 + 7;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no
    ll infinit = 10000000000000000;
 
//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(n); readint(m);
        map<ll,int> xcoord, ycoord;
        xcoord[0] = 0; ycoord[0] = 0;
        map<ll,vector<pair<ll,ll>>> hor_lines, vert_lines;
        forr(i,0,n) {
            readll(a); readll(b); readll(c);
            xcoord[a] = 0; xcoord[b] = 0; ycoord[c] = 0;
            hor_lines[c].push_back(make_pair(a,b));
        }
        forr(i,0,m) {
            readll(d); readll(e); readll(f);
            xcoord[d] = 0; ycoord[e] = 0; ycoord[f] = 0;
            vert_lines[d].push_back(make_pair(e,f));
        }
        vector<ll> dict_x, dict_y;
        for (auto p : xcoord) {
            dict_x.push_back(p.first);
        }
        for (auto p : ycoord) {
            dict_y.push_back(p.first);
        }
        forr(i,0,dict_x.size()) {
            xcoord[dict_x[i]] = i;
        }
        forr(i,0,dict_y.size()) {
            ycoord[dict_y[i]] = i;
        }
 
        vector<vector<bool>> visited(dict_x.size(), vector<bool>(dict_y.size(), false));
        depth_first_search_iterator<pair<int,int>> bfs(make_pair(xcoord[0], ycoord[0]), [&visited, &dict_x, &dict_y](pair<int,int> x) {
            if ((x.first >= 0) && (x.first < dict_x.size()) && (x.second >= 0) && (x.second < dict_y.size())) {
              return (!visited[x.first][x.second]);
            } else {  return true;  }
        },        [&visited, &dict_y, &dict_x](pair<int,int> x) {
            if ((x.first >= 0) && (x.first < dict_x.size()) && (x.second >= 0) && (x.second < dict_y.size())) {
              visited[x.first][x.second] = true; }
        }, [&hor_lines, &vert_lines, &dict_x, &dict_y](pair<int,int> point) {
            set<pair<int,int>> ret;
            // left
            bool can_go = true;
            if ((point.first >= 0) && (point.first < dict_x.size()) && (point.second >= 0) && (point.second < dict_y.size())) {
              for (auto seg : vert_lines[dict_x[point.first]]) {  
                if ((seg.first <= dict_y[point.second]) && (seg.second > dict_y[point.second])) { can_go = false; break;  }   }
            }
            if (can_go) {  ret.insert(make_pair(point.first-1, point.second));  }
            // down
            can_go = true;
            if ((point.first >= 0) && (point.first < dict_x.size()) && (point.second >= 0) && (point.second < dict_y.size())) {
              for (auto seg : hor_lines[dict_y[point.second]]) {  
                if ((seg.first <= dict_x[point.first]) && (seg.second > dict_x[point.first])) { can_go = false; break;  }   }
            }
            if (can_go) {  ret.insert(make_pair(point.first, point.second-1));  }
            // right
            can_go = true;
            if ((point.first+1 >= 0) && (point.first+1 < dict_x.size()) && (point.second >= 0) && (point.second < dict_y.size())) {
              for (auto seg : vert_lines[dict_x[point.first+1]]) {  
                if ((seg.first <= dict_y[point.second]) && (seg.second > dict_y[point.second])) { can_go = false; break;  }   }
            }
            if (can_go) {  ret.insert(make_pair(point.first+1, point.second));  }
            // up
            can_go = true;
            if ((point.first >= 0) && (point.first < dict_x.size()) && (point.second+1 >= 0) && (point.second+1 < dict_y.size())) {
              for (auto seg : hor_lines[dict_y[point.second+1]]) {  
                if ((seg.first <= dict_x[point.first]) && (seg.second > dict_x[point.first])) { can_go = false; break;  }   }
            }
            if (can_go) {  ret.insert(make_pair(point.first, point.second+1));  }
 
            return ret;
        });
 
        ll ret = 0;
        bool infin = false;
        while (!bfs.end()) {
            if ((*bfs).first < 0) {  infin = true; break;  }
            if ((*bfs).second < 0) {  infin = true; break;  }
            if ((*bfs).first+1 >= dict_x.size()) {  infin = true; break;  }
            if ((*bfs).second+1 >= dict_y.size()) {  infin = true; break;  }
            ret += (dict_x[(*bfs).first+1] - dict_x[(*bfs).first]) * (dict_y[(*bfs).second+1] - dict_y[(*bfs).second]);
            ++bfs;
        }
 
        if (infin) {
          cout << "INF" << endl;
        } else {
          cout << ret << endl;
        }
    }
    return 0;
}