您的位置:首页 > 编程语言 > C语言/C++

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

2014-08-12 17:57 369 查看
题目地址:POJ 3277

水题。。稍微处理一下然后用求面积并的方法求即可。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define LL __int64
LL sum[310000], c[310000], cnt, lazy[310000];
struct node
{
int l, r, h;
int f;
}edge[10000000];
void add(int l ,int r, int h, int f)
{
edge[cnt].l=l;
edge[cnt].r=r;
edge[cnt].h=h;
edge[cnt++].f=f;
}
int cmp(node x, node y)
{
return x.h<y.h;
}
void PushUp(int rt, int l, int r)
{
if(lazy[rt])
sum[rt]=c[r+1]-c[l];
else if(l==r)
sum[rt]=0;
else
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int ll,int rr, int x, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
lazy[rt]+=x;
PushUp(rt,l,r);
return ;
}
int mid=l+r>>1;
if(ll<=mid) update(ll,rr,x,lson);
if(rr>mid) update(ll,rr,x,rson);
PushUp(rt,l,r);
}
int erfen(int x, int high)
{
int low=0, mid;
while(low<=high)
{
mid=low+high>>1;
//printf("--%d\n",mid);
if(x==c[mid]) return mid;
else if(x<c[mid]) high=mid-1;
else low=mid+1;
}
}
int main()
{
int n, i, j, a, b, d, k=0, cnt=0;
LL ans=0;
scanf("%d",&n);
memset(lazy,0,sizeof(lazy));
memset(sum,0,sizeof(sum));
for(i=0;i<n;i++)
{
scanf("%d%d%d",&a,&b,&d);
c[k++]=a;
c[k++]=b;
add(a,b,0,1);
add(a,b,d,-1);
}
sort(edge,edge+2*n,cmp);
sort(c,c+2*n);
for(i=0;i<2*n-1;i++)
{
int l=erfen(edge[i].l,2*n-1);
int r=erfen(edge[i].r,2*n-1);
//printf("%d %d\n",edge[i].l,edge[i].r);
update(l,r-1,edge[i].f,0,2*n-1,1);
ans+=sum[1]*(edge[i+1].h-edge[i].h);
}
printf("%I64d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息