http://acm.hdu.edu.cn/showproblem.php?pid=4419

题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积。

转化为二进制表示颜色:001 R ,010G,100B,011RG,101RB,....111RGB;

在结构体里面加上一个len[8]和cover[8]表示每种颜色所占的长度和在区间的覆盖次数。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
#define ll __int64
using namespace std; int t,n;
char ch;
ll Y[maxn];
struct node1
{
ll x,y1,y2;
int lr,c;
bool operator <(const node1 &a)const
{
return (x<a.x)||(x==a.x&&lr>a.lr);
}
}p[maxn];
struct node
{
int l,r;
ll len[];
int cover[];
} tree[maxn*]; void build(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
for(int j=; j<; j++)
{
tree[i].cover[j]=tree[i].len[j]=;
}
if(l==r-) return;
int mid=(l+r)>>;
build(i<<,l,mid);
build(i<<|,mid,r);
} void update(int i,int l,int r,int lr,int c)
{
if(tree[i].l==l&&tree[i].r==r)
{
tree[i].cover[c]+=lr;
if(tree[i].cover[c])
tree[i].len[c]=Y[tree[i].r]-Y[tree[i].l];
else if(tree[i].r-==tree[i].l)
tree[i].len[c]=;
else
tree[i].len[c]=tree[i<<].len[c]+tree[i<<|].len[c];
return ;
}
int mid=(tree[i].r+tree[i].l)>>;
if(r<=mid)
{
update(i<<,l,r,lr,c);
}
else if(l>=mid)
{
update(i<<|,l,r,lr,c);
}
else
{
update(i<<,l,mid,lr,c);
update(i<<|,mid,r,lr,c);
}
if(tree[i].cover[c])
tree[i].len[c]=Y[tree[i].r]-Y[tree[i].l];
else if(tree[i].r-==tree[i].l)
tree[i].len[c]=;
else
tree[i].len[c]=tree[i<<].len[c]+tree[i<<|].len[c];
} int main()
{
scanf("%d",&t);
int cas=;
while(t--)
{
scanf("%d",&n);
getchar();
int t1=;
for(int i=; i<=n; i++)
{
ll x1,y1,x2,y2;
scanf("%c %I64d%I64d%I64d%I64d",&ch,&x1,&y1,&x2,&y2);
if(ch=='R')
{
p[t1].c=; p[t1+].c=;
}
else if(ch=='G')
{
p[t1].c=; p[t1+].c=;
}
else if(ch=='B')
{
p[t1].c=; p[t1+].c=;
}
p[t1].x=x1; p[t1].y1=y1; p[t1].y2=y2;p[t1].lr=;Y[t1++]=y1;
p[t1].x=x2; p[t1].y1=y1; p[t1].y2=y2;p[t1].lr=-;Y[t1++]=y2;
getchar();
}
sort(Y,Y+t1);
int cnt=unique(Y,Y+t1)-Y;
sort(p,p+t1);
build(,,cnt-);
ll s[maxn]={};
for(int i=; i<t1; i++)
{
int l1=lower_bound(Y,Y+cnt,p[i].y1)-Y;
int rr=lower_bound(Y,Y+cnt,p[i].y2)-Y;
for(int j=; j<; j++)
{
if(p[i].c&j) update(,l1,rr,p[i].lr,j);
if(i+<t1) s[j]+=tree[].len[j]*(p[i+].x-p[i].x);
}
}
printf("Case %d:\n",cas);
cas++;
printf("%I64d\n",s[]-s[]);
printf("%I64d\n",s[]-s[]);
printf("%I64d\n",s[]-s[]);
printf("%I64d\n",s[]+s[]-s[]-s[]);
printf("%I64d\n",s[]+s[]-s[]-s[]);
printf("%I64d\n",s[]+s[]-s[]-s[]);
printf("%I64d\n",s[]+s[]+s[]-s[]-s[]-s[]+s[]);
}
return ;
}

hdu 4419 Colourful Rectangle的更多相关文章

  1. [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...

  2. hdu 4419 Colourful Rectangle (离散化扫描线线段树)

    Problem - 4419 题意不难,红绿蓝三种颜色覆盖在平面上,不同颜色的区域相交会产生新的颜色,求每一种颜色的面积大小. 比较明显,这题要从矩形面积并的方向出发.如果做过矩形面积并的题,用线段树 ...

  3. HDU 4419 Colourful Rectangle --离散化+线段树扫描线

    题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...

  4. HDU 4419 Colourful Rectangle(线段树+扫描线)

    题目链接 主要是pushup的代码,其他和区间更新+扫描线差不多. 那个区间如果要再刷一层x,那么sum[x][rt] = que[r+1] - que[l];但是如果原本有颜色为i,颜色将会变成i| ...

  5. 【HDU4419 Colourful Rectangle】 线段树面积并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...

  6. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  7. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  8. HDU-4419 Colourful Rectangle 矩形多面积并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 利用二进制,R为1.G为2.B为4,然后通过异或运算可以得到其它组合颜色.建立7颗线段树,每颗线 ...

  9. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

随机推荐

  1. 【转】深入理解Android的startservice和bindservice--不错

    原文网址:http://www.cnblogs.com/yejiurui/p/3429451.html 一.首先,让我们确认下什么是service?         service就是android系 ...

  2. pyqt托盘例子

    # -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...

  3. delphi 简单的删除字符串尾部数字的代码

    delphi  简单的删除字符串尾部数字的代码 方式一: function FilterShowName(const sName: String): String; var I: Integer; b ...

  4. Js获取元素样式值(getComputedStyle&currentStyle)兼容性解决方案

    因为:style(document.getElementById(id).style.XXX)只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. 一般js获取内部样式和外部样式使用 ...

  5. Qt Mac 下软件Release 公布dmg

    1.首先当然是用Qt Creator.编译一个Release版本号的软件 注意到编译出来的大小非常小,才420KB,由于一些类库还没包括进去的原因.如今还仅仅能在你本地执行,复制到其它Mac电脑就不能 ...

  6. 单片机C语言中标志位的经典应用

    /* 本例程是C语言的位域操作示例 这里为什么位域结构体与联合体一起使用? -->因为这样定义后,即可以单独使用标志位 也可同时使用整个字节数据 主要应用:单片机C语言 好处:用标志位可以节省R ...

  7. Ubuntu 10.04下安装Opengl glx

    1.安装OpenGL Library sudo apt-get install build-essential 2. 安装OpenGL Utilities sudo apt-get install l ...

  8. Centos7安装Oracle JDK

    查看Linux是否自带的JDK,如有openJDK,则卸载 java -version

  9. 监控工具cacti

    一. 安装 cacti服务端 1. 首先要安装epel扩展源yum install -y epel-release2. (lamp)然后分别安装httpd.php.mysqlyum install - ...

  10. 调用百度地图API实现手机自动定位 (逆地址解析)

    //声明地址解析器 var geoc = new BMap.Geocoder(); //自动定位 var autoLocation = function () { if (navigator.geol ...