hdu 3265 Posters

题目


线段树求面积的并,虽然每个矩形有个孔,但我们可以把他分成四个矩形,然后就.....


环境不支持__int64,敲完编译才发现==


#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
#define N 50005
#define __int64 long long

inline int input()
{
    bool IsN=0;
    int ret=0;
    char c;
    c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-') IsN=1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        ret=ret*10+c-'0';
        c=getchar();
    }
    return IsN?-ret:ret;
}

map<int,int>m;
int n,x,y,x2,y2,x3,y3,x4,y4,k,num,X[N];
int h[N];
struct Line
{
    int s,t,y;
    int cov;
}l[N*8];
bool cmp(Line i,Line j)
{
    return i.y<j.y;
}

struct node
{
    int l,r,cov;
    __int64 len;
}root[50000*10];

inline void build(int t,int x,int y)
{
    root[t].l=x;
    root[t].r=y;
    root[t].len=root[t].cov=0;
    if(x+1==y) return;
    int m=(x+y)>>1;
    build(t*2,x,m);
    build(t*2+1,m,y);
}

inline void Push_up(int t)
{
    if(root[t].cov>0) root[t].len=h[root[t].r]-h[root[t].l];
    else if(root[t].l+1==root[t].r) root[t].len=0;
    else root[t].len=root[t*2].len+root[t*2+1].len;
}

inline void Modefiy(int t,int x,int y,int cov)
{
    int l=root[t].l;
    int r=root[t].r;
    if(r<x||l>y) return;
    if(x<=l&&r<=y)
    {
        root[t].cov+=cov;
        Push_up(t);
        return;
    }
    Modefiy(t*2,x,y,cov);
    Modefiy(t*2+1,x,y,cov);
    Push_up(t);
}

int main()
{
    while(scanf("%d",&n)&&n)
    {
        m.clear();
        //  h.clear();
        k=1;
        num=0;
        for(int i=1;i<=n;i++)
        {
            x=input(),y=input(),x2=input(),y2=input(),x3=input(),y3=input(),x4=input(),y4=input();
            l[num].s=x,l[num].t=x4,l[num].y=y,l[num++].cov=1;
            l[num].s=x,l[num].t=x4,l[num].y=y3,l[num++].cov=-1;
            l[num].s=x4,l[num].t=x2,l[num].y=y,l[num++].cov=1;
            l[num].s=x4,l[num].t=x2,l[num].y=y4,l[num++].cov=-1;
            l[num].s=x3,l[num].t=x2,l[num].y=y4,l[num++].cov=1;
            l[num].s=x3,l[num].t=x2,l[num].y=y2,l[num++].cov=-1;
            l[num].s=x,l[num].t=x3,l[num].y=y3,l[num++].cov=1;
            l[num].s=x,l[num].t=x3,l[num].y=y2,l[num++].cov=-1;
            m[x]=1;
            m[x2]=1;
            m[x3]=1;
            m[x4]=1;
        }
        map<int,int>::iterator it=m.begin();
        for(;it!=m.end();++it)
        {
            X[(*it).first]=k;
            h[k]=(*it).first;
            k++;
        }
        sort(l,l+num,cmp);
        build(1,1,k);
        __int64 ans=0;
        for(int i=0;i<num-1;i++)
        {
            int a=X[l[i].s];
            int b=X[l[i].t];
            if(a!=b)  Modefiy(1,a,b,l[i].cov);
            ans+=root[1].len*(l[i+1].y-l[i].y);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值