1645: [Usaco2007 Open]City Horizon 城市地平线

1645: [Usaco2007 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个矩形块,交求面积并.

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 Input

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.

HINT

Source

Silver

题解:

这题的思路比较巧妙。
全部的图形被分成了2*n-1个矩形,所以只要用线段树维护每一个矩形的高即可,取max
代码:(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     else
62     {
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 }

时间: 2024-10-11 11:14:31

1645: [Usaco2007 Open]City Horizon 城市地平线的相关文章

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

本来想打线段树的说... 就是把坐标离散化了,然后区间最大求和即可... 后来觉得有点烦的说(silver题就要线段树...),于是看了下usaco的题解,发现了个高端的东西:善用STL里的容器和迭代器就可以了. 以下就是高端程序: 1 /************************************************************** 2 Problem: 1645 3 User: rausen 4 Language: C++ 5 Result: Accepted 6

BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

Description 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

bzoj1683[Usaco2005 Nov]City skyline 城市地平线

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格隔开的整数x,y,其意义如题中所述.输入中的x严格递增,并且第一个z总是x. Output 输出一个整数,表示城市中最少包含的建筑物数量. Sample Input 10 26 1 1 2 2 5 1 6 3 8 1 11 0 15 2 17 3 20 2 22 1 INPUT DETAILS: T

bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)

P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 $upd:$本题的底端点坐标简化为$(x,0)$ 蓝后对纵坐标建一棵线段树(本题需要对高度进行离散化). 每次对线段树进行覆盖$or$删除区间操作,顺便统计一下$k=$有多少点被覆盖到 而两次(线段)操作之间的长度为$r=x_{i}-x_{i-1}$ 于是两条线段之间被覆盖的面积即为$k*r$ (

BZOJ1628: [Usaco2007 Demo]City skyline

1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Submit][Status] Description The best part of the day for Farmer John's cows is when the sun sets. They can see the skyline of the distant city. Bessie w

POJ 3277 City Horizon

City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18555   Accepted: 5114 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 silhouette

xtu数据结构 H. City Horizon

H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main 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 silh

ACM基础训练题解4301 城市地平线

遍历线段树   线段树的插入和查询 1 //城市地平线(线段树) 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<cstdio> 7 using namespace std; 8 typedef __int64 LL; 9 struct building{ 10 LL x1; 11 LL x2; 12 LL

POJ 3277 City Horizon(线段树+扫描线+离散化)

题目地址:POJ 3277 水题..稍微处理一下然后用求面积并的方法求即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <