版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss https://blog.csdn.net/u012797220/article/details/37905117
简单的Lazy操作。统计的时候把全部的lazy都推到叶节点就能够了
City Horizon
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15973 | Accepted: 4336 |
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
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long int LL;
const LL maxn=204000;
LL r[maxn*2],md[maxn*2],val[maxn*2],n,m,vt,zhi[maxn*2],Hash[maxn*2],high[maxn];
bool cmpR(int a,int b)
{
return val[a]<val[b];
}
int Lisan(int n)
{
for(int i=0;i<n;i++) r[i]=i;
sort(r,r+n,cmpR);
md[0]=val[r[0]];
val[r[0]]=m=0;
for(int i=1;i<n;i++)
{
if(md[m]!=val[r[i]])
md[++m]=val[r[i]];
val[r[i]]=m;
}
return m;
}
LL tree[maxn<<2],cover[maxn<<2];
void push_down(int l,int r,int rt)
{
if(cover[rt])
{
tree[rt<<1]=max(tree[rt<<1],tree[rt]);
tree[rt<<1|1]=max(tree[rt<<1|1],tree[rt]);
cover[rt<<1]=cover[rt<<1|1]=1;
cover[rt]=0;
}
}
void update(int L,int R,LL c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
tree[rt]=max(tree[rt],c);
cover[rt]=1;
return ;
}
push_down(l,r,rt);
int m=(l+r)/2;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
}
LL ans;
void over_tree(int l,int r,int rt)
{
push_down(l,r,rt);
if(l==r)
{
ans+=(Hash[r+1]-Hash[l])*tree[rt];
return ;
}
int m=(l+r)/2;
over_tree(lson); over_tree(rson);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
vt=0;
memset(tree,0,sizeof(tree));
memset(cover,0,sizeof(cover));
for(int i=0;i<n;i++)
{
LL a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
val[vt]=zhi[vt]=a; vt++;
val[vt]=zhi[vt]=b; vt++;
high[i]=c;
}
int mx=Lisan(vt);
for(int i=0;i<vt;i++)
{
Hash[val[i]]=zhi[i];
}
for(int i=0;i<n;i++)
{
update(val[i*2],val[i*2+1]-1,high[i],0,mx,1);
}
ans=0;
over_tree(0,mx,1);
printf("%I64d
",ans);
}
return 0;
}