1645: [usaco open] city horizon time limit: 5 sec memory limit: 64 MB

Submit: 315 solved: 157

[Submit] [Status] 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 rectangle blocks, intersection area and.

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

Sample input4

2 5 1

9 10 4

6 8 2

4 6 3

Sample output16

Output details:

The first building overlaps with the fourth building for an area of 1

Square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3-1 = 16. hintsource

Silver

Question: the idea of this question is clever.

All the images are divided into 2 * n-1 rectangles. Therefore, you only need to maintain the height of each rectangle using the line segment tree.

Code: (copy)

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define ll long long 7 #define inf 10000000000 8 using namespace std; 9 inline ll read()10 {11 int x=0,f=1;char ch=getchar();12 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}13 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}14 return x*f;15 }16 int n;17 int x[40005],y[40005],val[40005],disc[80005];18 struct seg{int l,r,mx,tag;}t[320005];19 int find(int x)20 {21 int l=1,r=2*n;22 while(l<=r)23 {24 int mid=(l+r)>>1;25 if(disc[mid]<x)l=mid+1;26 else if(disc[mid]==x)return mid;27 else r=mid-1;28 }29 }30 void pushdown(int k)31 {32 if(t[k].l==t[k].r)return;33 int tag=t[k].tag;t[k].tag=0;34 if(tag)35 {36 t[k<<1].tag=max(t[k<<1].tag,tag);37 t[k<<1|1].tag=max(t[k<<1|1].tag,tag);38 t[k<<1].mx=max(t[k<<1].mx,tag);39 t[k<<1|1].mx=max(t[k<<1|1].mx,tag);40 }41 }42 void build(int k,int l,int r)43 {44 t[k].l=l;t[k].r=r;45 if(l==r)return;46 int mid=(l+r)>>1;47 build(k<<1,l,mid);build(k<<1|1,mid+1,r);48 }49 void update(int k,int x,int y,int val)50 {51 pushdown(k);52 int l=t[k].l,r=t[k].r;53 if(l==x&&y==r)54 {55 t[k].tag=val;t[k].mx=max(t[k].mx,val);56 return;57 }58 int mid=(l+r)>>1;59 if(y<=mid)update(k<<1,x,y,val);60 else if(x>mid)update(k<<1|1,x,y,val);61 else62 {63 update(k<<1,x,mid,val);update(k<<1|1,mid+1,y,val);64 }65 }66 int query(int k,int x)67 {68 pushdown(k);69 int l=t[k].l,r=t[k].r;70 if(l==r)return t[k].mx;71 int mid=(l+r)>>1;72 if(x<=mid)return query(k<<1,x);73 else return query(k<<1|1,x);74 }75 int main()76 {77 n=read();build(1,1,n<<1);78 for(int i=1;i<=n;i++)79 {80 x[i]=read(),y[i]=read(),val[i]=read();81 disc[(i<<1)-1]=x[i];disc[i<<1]=y[i];82 }83 sort(disc+1,disc+(n<<1)+1);84 for(int i=1;i<=n;i++)85 x[i]=find(x[i]),y[i]=find(y[i]);86 for(int i=1;i<=n;i++)87 {88 update(1,x[i],y[i]-1,val[i]);89 }90 ll ans=0;91 for(int i=1;i<2*n;i++)92 {93 ans+=(ll)query(1,i)*(disc[i+1]-disc[i]);94 }95 printf("%lld",ans);96 return 0;97 }

View code

1645: [usaco open] city horizon