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