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 <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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值