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

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.

离散+线段树各种搞都能过……但是我写了个最得瑟的

先搞一个快排+判重,然后再把区间修改按高度排一下……我有优越感

#include<cstdio>
#include<algorithm>
#define LL long long
#define N 50010
#define mod 1000007
using namespace std;
struct trees{
int l,r,mx;
}tree[8*N];
struct add{
int l,r,mx;
}a[N];
int n,treesize;
LL ans;
int num[2*N];
inline int 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;
}
inline void pushdown(int now)
{
if (tree[now].l==tree[now].r)return;
int mx=tree[now].mx;tree[now].mx=0;
if (mx)
{
tree[now<<1].mx=mx;
tree[now<<1|1].mx=mx;
}
}
inline void buildtree(int now,int l,int r)
{
tree[now].l=l;
tree[now].r=r;
if (l==r)return;
int mid=(l+r)>>1;
buildtree(now<<1,l,mid);
buildtree(now<<1|1,mid+1,r);
}
inline void change(int now,int x,int y,int mx)
{
pushdown(now);
int l=tree[now].l,r=tree[now].r;
if (l==x&&r==y)
{
tree[now].mx=mx;
return;
}
int mid=(l+r)>>1;
if (y<=mid) change(now<<1,x,y,mx);
else if(x>mid)change(now<<1|1,x,y,mx);
else
{
change(now<<1,x,mid,mx);
change(now<<1|1,mid+1,y,mx);
}
}
inline void dfs(int now)
{
int l=tree[now].l,r=tree[now].r;
if (tree[now].mx)
{
ans+=(LL)tree[now].mx*(num[r+1]-num[l]);
return;
}
if (l==r)return;
dfs(now<<1);
dfs(now<<1|1);
}
//----------------------------------离散
struct hashing{
int num,next,rnk;
}hash[mod];
int ha[2*N],len,cnt,rating;
int head[mod];
inline void insert(int u,int v,int w)
{
hash[++cnt].num=v;
hash[cnt].rnk=w;
hash[cnt].next=head[u];
head[u]=cnt;
}
inline int find(int x)
{
int s=x%mod;
for (int i=head[s];i;i=hash[i].next)
if (hash[i].num==x)return hash[i].rnk;
}
inline bool cmp(const add &a,const add &b)
{return a.mx<b.mx||a.mx==b.mx&&a.l<b.l||a.mx==b.mx&&a.l==b.l&&a.r<b.r;}
//----------------------------------end
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
a[i].l=read();
a[i].r=read();
a[i].mx=read();
ha[++len]=a[i].l;
ha[++len]=a[i].r;
}
sort(ha+1,ha+len+1);
for (int i=1;i<=len;i++)
if (ha[i]!=ha[i-1])
{
num[++rating]=ha[i];
insert(ha[i]%mod,ha[i],rating);
}
for (int i=1;i<=n;i++)
{
a[i].l=find(a[i].l);
a[i].r=find(a[i].r);
}
sort(a+1,a+n+1,cmp);
buildtree(1,1,rating-1);
for (int i=1;i<=n;i++)
change(1,a[i].l,a[i].r-1,a[i].mx);
dfs(1);
printf("%lld",ans);
}
上一篇:curl命令实现上网认证登录


下一篇:学习 | canvas实现图片懒加载 && 下滑底部加载