hdu 4419 矩形面积并

#include <iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
typedef __int64 LL;
const int mmax = 20010;
map<char,int>q;
struct Rect
{
    LL x1,x2;
    LL y1,y2;
    char Col[2];
    void read()
    {
        scanf("%s",Col);
        scanf("%I64d %I64d %I64d %I64d",&x1,&y1,&x2,&y2);
        if(x1>x2)
            swap(x1,x2);
        if(y1>y2)
            swap(y1,y2);
    }
}R[110];
struct node
{
    int l,r;
    int cov[3];
    LL len[8];
    int mid()
    {
        return (l+r)>>1;
    }
}T[4*mmax];
struct Num
{
    LL x;
    int id;
    bool operator < (const Num &a) const
    {
        return x<a.x;
    }
}X[mmax];
struct Line
{
    int l,r;
    LL y;
    int fg;
    int Col;
    bool operator < (const Line &a) const
    {
        return y<a.y;
    }
}L[mmax];
LL pos[mmax];
void push_up(int id)
{
    if(T[id].l==T[id].r)
    {
        memset(T[id].len,0,sizeof T[id].len);
        int fg=0;
        for(int i=0;i<3;i++)
        {
            if(T[id].cov[i])
                fg+=(1<<i);
        }
        T[id].len[fg]=pos[T[id].r+1]-pos[T[id].l];
        return ;
    }
    for(int i=0;i<8;i++)
        T[id].len[i]=T[id<<1].len[i]+T[id<<1|1].len[i];
    int fg=0;
    for(int i=0;i<3;i++)
    {
        if(T[id].cov[i])
            fg+=(1<<i);
    }
    LL Len[8];
    for(int i=0;i<8;i++)
        Len[i]=T[id].len[i];
    memset(T[id].len,0,sizeof T[id].len);
    for(int i=0;i<8;i++)
    {
        T[id].len[i|fg]+=Len[i];
    }
}
void build(int id,int l,int r)
{
    T[id].l=l,T[id].r=r;
    memset(T[id].len,0,sizeof T[id].len);
    T[id].cov[0]=T[id].cov[1]=T[id].cov[2]=0;
    if(l==r)
    {
        T[id].len[0]=pos[r+1]-pos[l];
        return ;
    }
    int mid=T[id].mid();
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
    push_up(id);
}
void updata(int id,int l,int r,int fg,int Col)
{
    if(l<=T[id].l && T[id].r<=r)
    {
        T[id].cov[Col]+=fg;
        push_up(id);
        return ;
    }
    int mid=T[id].mid();
    if(mid>=l)
        updata(id<<1,l,r,fg,Col);
    if(mid<r)
        updata(id<<1|1,l,r,fg,Col);
    push_up(id);
}
int id[mmax];
int main()
{
    int n,ca=0,t;
    q.clear();
    q['R']=0;
    q['G']=1;
    q['B']=2;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            R[i].read();
            X[cnt].x=R[i].x1;
            X[cnt].id=cnt;
            cnt++;
            X[cnt].x=R[i].x2;
            X[cnt].id=cnt;
            cnt++;
        }
        sort(X,X+cnt);
        id[X[0].id]=1;
        pos[1]=X[0].x;
        for(int i=1;i<cnt;i++)
        {
            if(X[i].x==X[i-1].x)
                id[X[i].id]=id[X[i-1].id];
            else
                id[X[i].id]=id[X[i-1].id]+1;
            pos[id[X[i].id]]=X[i].x;
        }
        for(int i=0;i<n;i++)
        {
            L[i].l=id[2*i];
            L[i].r=id[2*i+1];
            L[i].y=R[i].y1;
            L[i].fg=1;
            L[i].Col=q[R[i].Col[0]];
            L[i+n].l=id[2*i];
            L[i+n].r=id[2*i+1];
            L[i+n].y=R[i].y2;
            L[i+n].fg=-1;
            L[i+n].Col=q[R[i].Col[0]];
        }



        sort(L,L+2*n);
        build(1,1,id[X[cnt-1].id]-1);
        updata(1,L[0].l,L[0].r-1,L[0].fg,L[0].Col);
        LL ans[8];
        memset(ans,0,sizeof ans);
        for(int i=1;i<2*n;i++)
        {
            for(int j=0;j<8;j++)
                ans[j]+=(L[i].y-L[i-1].y)*T[1].len[j];
            updata(1,L[i].l,L[i].r-1,L[i].fg,L[i].Col);
        }
        printf("Case %d:\n",++ca);
        printf("%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n",ans[1],ans[2],ans[4],ans[3],ans[5],ans[6],ans[7]);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值