poj3277

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
#include<cmath>
#define L(x) (x<<1)
#define R(x) (x<<1 |1)
#define MAX 80000
using namespace std;
long long int cx[4*MAX];
class line
{
    public:
   long long int x1,x2,y;
    line(){};
    line(long long int a,long long int b,long long int c):x1(a),x2(b),y(c)
    {}
    bool operator <(line b)const
    {
        return y<b.y;
    }
}lines[4*MAX];
struct  node
{
    int l,r;
    long long int cover;
}a[MAX*4];
void build(int t, int l, int r)
{
    a[t].l=l;
    a[t].r=r;
    a[t].cover=0;
    if(r-l==1)return ;
    int mid=(l+r)/2;
    build(L(t),l,mid);
    build(R(t),mid,r);
}
void set(int t, line s)
{
    if(s.y<a[t].cover)return ;
    if(s.x1<=cx[a[t].l]&&s.x2>=cx[a[t].r])
    {
        a[t].cover=max(s.y,a[t].cover);
        return ;
    }
    if(a[t].r-a[t].l==1)return ;
    if(a[t].cover>0)
    {
        a[L(t)].cover=a[t].cover;
        a[R(t)].cover=a[t].cover;
        a[t].cover=0;
    }
    long long int mid=(a[t].r+a[t].l)/2;
    if(s.x1<=cx[mid])
      set(L(t),s);
    if(s.x2>cx[mid])
      set(R(t),s);
}
long long int query(int t,long long int l,long long int r)
{
    if(a[t].cover>0)
    {
        return (cx[a[t].r]-cx[a[t].l])*a[t].cover;
    }
    if(a[t].r-a[t].l==1)return 0;
    long long int mid = (a[t].r+a[t].l)/2;
    long long int ll=0;
    if(l<=mid)
      ll+=query(L(t),l,r);
    if(r>mid)
      ll+=query(R(t),l,r);
    return ll;
}
int main()
{
    int n,m,i,j;
    long long int x1,x2,y;
    while(scanf("%d",&n)!=EOF)
    {
        m=0;
        memset(a,0,sizeof(a));
        for(i=0;i<n;i++)
        {
            scanf("%lld%lld%lld",&x1,&x2,&y);
            lines[i]=line(min(x1,x2),max(x1,x2),y);
            cx[m++]=x1;
            cx[m++]=x2;
        }
        sort(lines,lines+n);
        sort(cx,cx+m);
        m=unique(cx,cx+m)-cx;
        build(1,0,m-1);
        for(i=0;i<n;i++)
          set(1,lines[i]);
        printf("%lld\n",query(1,0,m-1));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值