# 【poj 3277 bzoj 1645 luogu 2061】[Usaco2007 Open Silver]City Horizon 城市地平线 （线段树）

## [Usaco2007 Open]City Horizon 城市地平线

Time Limit: 5 Sec  Memory Limit: 64 MB

## 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.

## 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

4
2 5 1
9 10 4
6 8 2
4 6 3

## Sample Output

16

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.

## Source

Silver

【题目大意】

【分析】

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std;

int n,cnt;
struct sdt
{
int lef,rig,h;
}a[3200005];
int pos[800005],old[800005];
int lef[800005],rig[800005];
long long data[800005],delta[800005];

bool cmp(sdt x,sdt y)
{
return x.h<y.h;
}

void build(int root,int l,int r)
{
lef[root]=l;
rig[root]=r;
if(l==r)
{
return ;
}
int mid=(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
}

void check(int root)
{
if(!delta[root])return ;
data[root]=delta[root];
if(lef[root]==rig[root])
{
delta[root]=0;
return ;
}
if(delta[root*2]<delta[root])
delta[root*2]=delta[root];
if(delta[root*2+1]<delta[root])
delta[root*2+1]=delta[root];
delta[root]=0;
}

void add(int root,int l,int r,int x)
{
check(root);
if(lef[root]==l && rig[root]==r)
{
delta[root]=x;
return ;
}
int mid=(lef[root]+rig[root])/2;
else
{
}
check(root*2);
check(root*2+1);
data[root]=max(data[root*2],data[root*2+1]);
}

long long query(int root,int l,int r)
{
check(root);
if(lef[root]==l && rig[root]==r)
{
return data[root];
}
int mid=(lef[root]+rig[root])/2;
if(l>mid)return query(root*2+1,l,r);
else if(r<=mid)return query(root*2,l,r);
else return max(query(root*2,l,mid),query(root*2+1,mid+1,r));
}

int binarysearch(int x)
{
int L=1,R=cnt;
while(L<=R)
{
int mid=(L+R)>>1;
if(pos[mid]==x)return mid;
if(pos[mid]>x)R=mid-1;
else if(pos[mid]<x)L=mid+1;
}
}

int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&a[i].lef,&a[i].rig,&a[i].h);
pos[++cnt]=a[i].lef;
pos[++cnt]=a[i].rig;
}
sort(pos+1,pos+1+cnt);
cnt=unique(pos+1,pos+cnt+1)-(pos+1);
//for(int i=1;i<=cnt;i++)
//cout<<pos[i]<<" ";
//cout<<endl;
sort(a+1,a+n+1,cmp);
build(1,1,cnt);
for(int i=1;i<=n;i++)
{
int xx=binarysearch(a[i].lef);
int yy=binarysearch(a[i].rig);
//cout<<xx<<" "<<yy<<endl;
old[xx]=a[i].lef;
old[yy]=a[i].rig;
}
long long ans=0;
for(int i=1;i<cnt;i++)
{
//cout<<i<<" "<<old[i]<<" "<<old[i+1]<<" "<<query(1,i,i)<<endl;
ans+=query(1,i,i)*(long long)(old[i+1]-old[i]);
}
printf("%lld\n",ans);
return 0;
}


posted @ 2017-03-27 22:06  winmt  阅读(194)  评论(0编辑  收藏  举报