posts - 74,  comments - 33,  trackbacks - 0
City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4976 Accepted: 1195

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

Line 1: A single integer: N
Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi

Output

Line 1: The total area, in square units, of the silhouettes formed by all N buildings

Sample Input

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

Sample Output

16

Hint

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


USACO 2007 Open Silver

这道题目属于区间覆盖,和count colour那一道题目属于同一类型,我就偷懒了,就直接把那道题的代码直接copy过来,
没想到在改的时候,多删了一句话导致TLE了20+次,很不happy
主要思路代码如下:
void Build(int now,int l,int r){
    ST[now].l
=l,ST[now].r=r,ST[now].h=0,ST[now].mark=true;
    
if(l+1>=r)return;
    
int mid=(l+r)>>1;
    Build(
2*now,l,mid);
    Build(
2*now+1,mid,r);    
    
return ;
}

void insert(int now,int l,int r,int h){    
    
if(ST[now].mark&&ST[now].h>h)return;
    
if(ST[now].mark&&ST[now].l==l&&ST[now].r==r){
        ST[now].h
=h;return ;
    }
    
    
if(ST[now].mark&&ST[now].l+1<ST[now].r){
        ST[
2*now].h=ST[2*now+1].h=ST[now].h;
        ST[
2*now].mark=ST[2*now+1].mark=true;
        ST[now].mark
=false;
    }

    
int mid=(ST[now].l+ST[now].r)>>1;
    
if(l>=mid)insert(2*now+1,l,r,h);
    
else if(r<=mid)insert(2*now,l,r,h);
    
else {    
        insert(
2*now,l,mid,h);
        insert(
2*now+1,mid,r,h);
    }
    
    
return ;
}

posted on 2009-04-08 14:57 KNIGHT 阅读(109) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2009年4月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

常用链接

留言簿(8)

随笔档案

文章档案

Friends

OJ

搜索

  •  

最新评论

阅读排行榜

评论排行榜