Colourful Rectangle【扫描线】
很明显的可以发现是一个扫描线的问题,但是怎么处理区域呢,发现只有三种颜色,也就是最多也就是7种状态,那么我们可以进行一个状态压缩即可。
但是,在向上pushup的时候,存在我们要以子树的状态来向上推,也就意味着一开始的目前节点是要去清空的,然后再去更新其覆盖的线长。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e4 + ;
int N, tot, _UP;
ll X[maxN<<], ans[];
struct node
{
ll lx, rx, y;
int typ, val;
node(ll a=, ll b=, ll c=, int f=, int d=):lx(a), rx(b), y(c), typ(f), val(d) {}
}line[maxN<<];
bool cmp(node e1, node e2) { return e1.y < e2.y; }
struct Tree
{
int siz[];
ll col[];
void clear() { memset(siz, , sizeof(siz)); memset(col, , sizeof(col)); }
}t[maxN<<];
inline void pushup(int rt, int l, int r)
{
memset(t[rt].col, , sizeof(t[rt].col));
int state = ;
for(int i=; i<=; i++) if(t[rt].siz[i]) state |= (<<(i - ));
if(l == r) t[rt].col[state] = X[r + ] - X[l];
else for(int i=; i<=; i++) { t[rt].col[i|state] += t[lsn].col[i] + t[rsn].col[i]; }
}
inline void buildTree(int rt, int l, int r)
{
t[rt].clear();
if(l == r) { t[rt].col[] = X[r + ] - X[l]; return; }
int mid = HalF;
buildTree(Lson);
buildTree(Rson);
t[rt].col[] = t[lsn].col[] + t[rsn].col[];
}
inline void update(int rt, int l, int r, int ql, int qr, int typ, int val)
{
if(ql <= l && qr >= r)
{
t[rt].siz[typ] += val;
pushup(myself);
return;
}
int mid = HalF;
if(qr <= mid) update(QL, typ, val);
else if(ql > mid) update(QR, typ, val);
else { update(QL, typ, val); update(QR, typ, val); }
pushup(myself);
}
inline void init()
{
tot = ;
memset(ans, , sizeof(ans));
}
char s[];
int main()
{
int T; scanf("%d", &T);
for(int Cas=; Cas<=T; Cas++)
{
scanf("%d", &N);
init();
ll lx, ly, rx, ry;
for(int i=, tmp; i<=N; i++)
{
scanf("%s%lld%lld%lld%lld", s, &lx, &ly, &rx, &ry);
if(s[] == 'R') tmp = ;
else if(s[] == 'G') tmp = ;
else tmp = ;
line[++tot] = node(lx, rx, ly, tmp, );
X[tot] = lx;
line[++tot] = node(lx, rx, ry, tmp, -);
X[tot] = rx;
}
sort(X + , X + tot + );
sort(line + , line + tot + , cmp);
_UP = (int)(unique(X + , X + tot + ) - X - );
buildTree(, , _UP);
for(int i=, l, r; i<tot; i++)
{
l =(int)(lower_bound(X + , X + _UP + , line[i].lx) - X);
r = (int)(lower_bound(X + , X + _UP + , line[i].rx) - X - );
update(, , _UP, l, r, line[i].typ, line[i].val);
for(int col=; col<=; col++) ans[col] += (line[i + ].y - line[i].y) * t[].col[col];
}
swap(ans[], ans[]);
printf("Case %d:\n", Cas);
for(int i=; i<=; i++) printf("%lld\n", ans[i]);
}
return ;
}
Colourful Rectangle【扫描线】的更多相关文章
- [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- HDU 4419 Colourful Rectangle(线段树+扫描线)
题目链接 主要是pushup的代码,其他和区间更新+扫描线差不多. 那个区间如果要再刷一层x,那么sum[x][rt] = que[r+1] - que[l];但是如果原本有颜色为i,颜色将会变成i| ...
- hdu4419 Colourful Rectangle 12年杭州网络赛 扫描线+线段树
题意:给定n个矩形,每个矩形有一种颜色,RGB中的一种.相交的部分可能为RG,RB,GB,RGB,问这n个矩形覆盖的面积中,7种颜色的面积分别为多少 思路:把x轴离散化做扫描线,线段树维护一个扫描区间 ...
- hdu 4419 Colourful Rectangle (离散化扫描线线段树)
Problem - 4419 题意不难,红绿蓝三种颜色覆盖在平面上,不同颜色的区域相交会产生新的颜色,求每一种颜色的面积大小. 比较明显,这题要从矩形面积并的方向出发.如果做过矩形面积并的题,用线段树 ...
- 【HDU4419 Colourful Rectangle】 线段树面积并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...
- HDU-4419 Colourful Rectangle 矩形多面积并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 利用二进制,R为1.G为2.B为4,然后通过异或运算可以得到其它组合颜色.建立7颗线段树,每颗线 ...
- hdu 4419 Colourful Rectangle
http://acm.hdu.edu.cn/showproblem.php?pid=4419 题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积. 转化为二进制表示颜色:001 ...
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
随机推荐
- 旧接口注册LED字符驱动设备(动态映射)
#include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...
- [SPOJ]Count on a tree II(树上莫队)
树上莫队模板题. 使用欧拉序将树上路径转化为普通区间. 之后莫队维护即可.不要忘记特判LCA #include<iostream> #include<cstdio> #incl ...
- ./configure 配置文件时出错checking for g++... no
checking for g++... no checking for c++... no checking for gpp... no checking for aCC... no 缺少C++编译器 ...
- html表格单元格添加斜下框线的方法
一.分隔单元格的方法 1.用“transform: rotate(-55deg);”把一条水平线旋转一定角度就成斜线了 2.利用以下命令调整分割线位置等. :after :before transfo ...
- Ubuntu 中查找软件安装的位置
Ubuntu 中查找软件安装的位置 执行该程序 直接执行该程序,有时候一些程序执行时会显示出自己的位置,比如: 用命令 ps -e 找到该程序的名字 用 find 或 whereis 命令查找文件位置 ...
- redis 安装和运行
安装redis 在CentOs虚拟机上:yum install redis,可能会出现以下错误: 解决方式:先输入yum install epel-release,之后输入:redis-server启 ...
- k3 cloud单据体首行过滤功能
#实现单据体首行过滤 clr.AddReference('System') clr.AddReference('Kingdee.BOS.Core') from Kingdee.BOS.Core.Dy ...
- “没有找到mfc100u.dll”的解决方法
现在需要安装 MindManager 2016 思维导图软件时,打开软件提示找不到 mfc100u.dll,无法执行程序.之前一直好好的,现在换电脑了安装提示这个问题,然后百度找的解决方案: 需要去微 ...
- ubuntu移动分区,修改目录挂在点
由于/tmp目录空间有点小,导致安装一个大软件的时候提示/tmp空间不足,最后通过创建新分区,并将新分区挂在到/tmp下,把/tmp空间扩大. 安装gparted 输入如下命令: sudo apt-g ...
- Spring3.x 升级至 Spring4.x 详解
1 升级依赖包 1.1 Maven 项目 1.1.1 更新 spring 依赖版本 打开 pom.xml,把所有 spring3.x 的版本号更新为 spring4.x.建议使用属性配置,形如: &l ...