There is a square of size 1 0 6 × 1 0 6 10^6×10^6 106×106 on the coordinate plane with four points ( 0 , 0 ) (0,0) (0,0), ( 0 , 1 0 6 ) (0,10^6) (0,106), ( 1 0 6 , 0 ) (10^6,0) (106,0), and ( 1 0 6 , 1 0 6 ) (10^6,10^6) (106,106) as its vertices.
You are going to draw segments on the plane. All segments are either horizontal or vertical and intersect with at least one side of the square.
Now you are wondering how many pieces this square divides into after drawing all segments. Write a program calculating the number of pieces of the square.
Input
The first line contains two integers n n n and m m m ( 0 ≤ n , m ≤ 1 0 5 ) (0≤n,m≤10^5) (0≤n,m≤105) — the number of horizontal segments and the number of vertical segments.
The next n n n lines contain descriptions of the horizontal segments. The i i i-th line contains three integers y i y_i yi, l x i lx_i lxi and r x i rx_i rxi ( 0 < y i < 1 0 6 ; 0 ≤ l x i < r x i ≤ 1 0 6 ) (0
The next m m m lines contain descriptions of the vertical segments. The i i i-th line contains three integers x i x_i xi, l y i ly_i lyi and r y i ry_i ryi ( 0 < x i < 1 0 6 ; 0 ≤ l y i < r y i ≤ 1 0 6 ) (0
It’s guaranteed that there are no two segments on the same line, and each segment intersects with at least one of square’s sides.
Output
Print the number of pieces the square is divided into after drawing all the segments.
Example
input
3 3
2 3 1000000
4 0 4
3 0 1000000
4 0 1
2 0 5
3 1 1000000
output
7
Note
The sample is like this:
对于每条纵线,他会与多条横线相交,如果每条线与一个边界相交,那么每个交点都贡献一个封闭区域,对于两边都与边界相交的线,可与等效于多个这种情况。利用扫描线原理用树状数组维护。复杂度为 O ( N log N ) O(N\log N) O(NlogN)。
#include
#define pl(a) printf("%lld\n",a)
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define ll long long
#define pii pair
#define lowbit(x) ((x)&(-(x)))
#define fi first
#define se second
#define all(x) x.begin(),x.end()
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 1e6 + 10;
struct BIT {
int c[N];
int n = 1000000;
inline int get_sum(int k) {
int ans = 0;
while (k > 0) {
ans += c[k];
k -= lowbit(k);
}
return ans;
}
inline void add(int t, int v) {
while (t <= n) {
c[t] += v;
t += lowbit(t);
}
}
} t;
int n, m;
vector<pair<int, pii>> op2, op1;
int main() {
n = qr(), m = qr();
op1.resize(n * 2), op2.resize(m);
ll ans = 1;
repi(i, 0, n - 1) {
op1[i].se.fi = op1[i + n].se.fi = qr();
op1[i].fi = qr(), op1[i].se.se = 1;
op1[i + n].fi = qr() + 1, op1[i + n].se.se = -1;
if (op1[i].fi == 0 && op1[i + n].fi == 1000001)ans++;
}
repi(i, 0, m - 1) {
op2[i].fi = qr(), op2[i].se.fi = qr(), op2[i].se.se = qr();
if (op2[i].se.fi == 0 && op2[i].se.se == 1000000)ans++;
}
sort(all(op1)), sort(all(op2));
int j = 0;
for (auto i:op2) {
while (j < 2 * n && op1[j].fi <= i.fi)t.add(op1[j].se.fi, op1[j].se.se), j++;
ans += t.get_sum(i.se.se) - t.get_sum(i.se.fi - 1);
}
pl(ans);
return 0;
}