hdu 3265 线段树+扫描线

虽然也是矩形面积并~但让我更深刻了理解了~~

本来想的是将大矩形正常打标记,将小矩形反向打标记就是下面的是-1上面是1~~发现样例过不去~~发现思想错了~~~

只能分割成小矩形~~

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define lson pos<<1
#define rson pos<<1|1
using namespace std;
const int MAXN=50005;
struct Line
{
    long long l,r,h,f;
    Line(int a=0,int b=0,int c=0,int d=0):l(a),r(b),h(c),f(d){}
};
Line line[MAXN*8];
struct node
{
    long long l,r;
    long long len;
    long long cover;
    int mid()
    {
        return (l+r)>>1;
    }
};
node tree[MAXN*8];
bool cmp(const Line &lhs,const Line &rhs)
{
    return lhs.h<rhs.h;
}
void build(long long l,long long r,int pos)
{
    tree[pos].l=l;
    tree[pos].r=r;
    if(l==r)
    {
        tree[pos].len=0;
        tree[pos].cover=0;
        return ;
    }
    long long mid=tree[pos].mid();
    build(l,mid,lson);
    build(mid+1,r,rson);
    tree[pos].len=0;
    tree[pos].cover=0;
}
void pushup(int pos)
{
    if(tree[pos].cover)
    {
        int l=tree[pos].l;
        int r=tree[pos].r+1;
        tree[pos].len=(long long)r-l;
    }
    else if(tree[pos].l==tree[pos].r)
        tree[pos].len=0;
    else
    {
        tree[pos].len=tree[lson].len+tree[rson].len;

    }
}
void update(long long l,long long r,int f,int pos)
{
    if(l>r)     //没有这个无限RE
        return ;
    if(l==tree[pos].l&&r==tree[pos].r)
    {
        tree[pos].cover+=f;
        pushup(pos);
        return ;
    }
    long long mid=tree[pos].mid();
    if(r<=mid)
        update(l,r,f,lson);
    else if(l>mid)
        update(l,r,f,rson);
    else
    {
        update(l,mid,f,lson);
        update(mid+1,r,f,rson);
    }
    pushup(pos);
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        int i;
        int k=0;
        for(i=0;i<n;i++)
        {
            int x1,y1,x2,y2,x3,y3,x4,y4;
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            line[k++]=Line(x1,x3,y1,1);
            line[k++]=Line(x1,x3,y2,-1);
            line[k++]=Line(x4,x2,y1,1);
            line[k++]=Line(x4,x2,y2,-1);
            line[k++]=Line(x3,x4,y1,1);
            line[k++]=Line(x3,x4,y3,-1);
            line[k++]=Line(x3,x4,y4,1);
            line[k++]=Line(x3,x4,y2,-1);
        }
        sort(line,line+k,cmp);
        build(0,100005,1);
        long long res=0;
        for(i=0;i<k-1;i++)
        {
            int l=line[i].l;
            int r=line[i].r-1;
            //printf("%d %d \n",l,r);
            update(l,r,line[i].f,1);
            //printf("%d %d %d\n",line[i].h,tree[1].len,line[i].f);

            res+=(long long)(line[i+1].h-line[i].h)*tree[1].len;
        }
        printf("%I64d\n",res);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值