Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16206 | Accepted: 4414 |
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 Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
Input
Output
Sample Input
4 2 5 1 9 10 4 6 8 2 4 6 3
Sample Output
16
Hint
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define maxn 81000 #define LL __int64 struct node1 { LL x, y1 , y2 ; LL flag ; } p[maxn<<2]; struct node2 { LL l , r ; LL sum ; } cl[maxn<<2]; LL lazy[maxn<<2]; LL s[maxn<<2] ; bool cmp(node1 a,node1 b) { return a.x < b.x ; } void push_up(int rt) { if( lazy[rt] ) cl[rt].sum = cl[rt].r - cl[rt].l ; else cl[rt].sum = cl[rt<<1].sum + cl[rt<<1|1].sum ; } void creat(int rt,int l,int r) { cl[rt].l = s[l] ; cl[rt].r = s[r] ; if( r - l > 1 ) { creat(rt<<1,l,(l+r)/2); creat(rt<<1|1,(l+r)/2,r); } else cl[rt].sum = 0 ; return ; } void update(int rt,LL l,LL r,int flag) { if( l == cl[rt].l && r == cl[rt].r ) { lazy[rt] += flag ; push_up(rt); return ; } else { if( cl[rt<<1].r > l ) { LL k = cl[rt<<1].r < r ? cl[rt<<1].r : r ; update(rt<<1,l,k,flag); } if( cl[rt<<1|1].l < r ) { LL k = cl[rt<<1|1].l > l ? cl[rt<<1|1].l : l ; update(rt<<1|1,k,r,flag); } push_up(rt); } } int main() { LL i , n , x1 , x2 , h , low , ans ; while(scanf("%I64d", &n)!=EOF) { for(i = 0 ; i < n ; i++) { scanf("%I64d %I64d %I64d", &x1, &x2, &h); p[i].x = x1 ; p[i].y1 = 0 ; p[i].y2 = h ; p[i].flag = 1 ; p[i+n].x = x2 ; p[i+n].y1 = 0 ; p[i+n].y2 = h ; p[i+n].flag = -1 ; s[i+1] = h ; } s[n+1] = 0 ; sort(s+1,s+(n+2)); sort(p,p+2*n,cmp); creat(1,1,n+1); update(1,p[0].y1,p[0].y2,p[0].flag); ans = 0 ; for(i = 1 ; i < 2*n ; i++) { ans += ( p[i].x - p[i-1].x )*cl[1].sum; update(1,p[i].y1,p[i].y2,p[i].flag); } printf("%I64d\n", ans); } return 0; }