Description
Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings. The entire horizon is represented by a number line with N (1 <= N <= 40,000) buildings. Building i's silhouette has a base that spans locations A_i through B_i along the horizon (1 <= A_i < B_i <= 1,000,000,000) and has height H_i (1 <= H_i <= 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
N个矩形块,交求面积并.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: A_i, B_i, and H_i
Output
* Line 1: The total area, in square units, of the silhouettes formed by all N buildings
#include#include #include #define maxn 1030000 #define inf 300000 #define ll long long using namespace std;void setIO(string s){ string in=s+".in"; freopen(in.c_str(),"r",stdin); }ll Arr[maxn]; namespace tr{ #define mid ((l+r)>>1) #define lson t[x].l #define rson t[x].r struct Node { int l,r,sum; ll len; }t[maxn<<2]; int tot; int newnode(){ return ++tot; } void pushup(int x,int l,int r) { if(t[x].sum) { t[x].len=Arr[r]-Arr[l-1]; } else { t[x].len=t[lson].len+t[rson].len; } } // 应为 > L (左面是开的) void Update(int &x,int l,int r,int L,int R,int v) { if(!x) x = newnode(); if(l>=L&&r<=R) { t[x].sum+=v; pushup(x,l,r); return; } if(L<=mid) Update(lson,l,mid,L,R,v); if(R>mid) Update(rson,mid+1,r,L,R,v); pushup(x,l,r); } void re() { for(int i=0;i<=tot;++i) t[tot].l=t[tot].r=t[tot].sum=t[tot].len=0; tot=0; }}; struct Edge{ ll l,r,h; int L,R; int flag; }edges[maxn]; bool cmp(Edge a,Edge b){ return a.h==b.h?a.flag>b.flag:a.h