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 ...
随机推荐
- Arrays.asList使用指南和一些坑(转)
一.java.util.Arrays.asList() 的一般用法 List 是一种很有用的数据结构,如果需要将一个数组转换为 List 以便进行更丰富的操作的话,可以这么实现: String[] m ...
- 在eclipse上配置多个jdk
在实际生产中,可能会遇到这样的问题:在eclipse中存在多个项目时,可能不同的项目需要不同的jdk版本.这时,我们就可以给eclipse配置多个jdk进行切换. 注:貌似只有较新版eclipse才能 ...
- P3191 [HNOI2007]紧急疏散EVACUATE
传送门 这一题很容易想到网络流 一开始傻逼地模拟整个图每一个时间的情况,显然会爆炸 发现我们只要考虑起点到门之间的距离,不用每一步只走一格 所以直接 $BFS$ 预处理距离然后二分答案,网络流判断即可 ...
- Neo4j 修改关系类型 type
没有直接修改的函数,也不需要,下面代码就可以: MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar&qu ...
- Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation
原文:Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...
- centos7卸载YUM后重装过程 -bash: yum: command not found / -bash: yum: 未找到命令
[root@localhost ~]# rpm -qa |grep yum yum-3.4.3-158.el7.centos.noarch yum-plugin-fastestmirror-1.1.3 ...
- Linux性能优化从入门到实战:14 文件系统篇:Linux 文件系统基础
磁盘为系统提供了最基本的持久化存储. 文件系统则在磁盘的基础上,提供了一个用来管理文件的树状结构. 文件系统:索引节点和目录项 文件系统是对存储设备上的文件,进行组织管理的机制.组织方式不 ...
- [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
Problems meet in the project: [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序((IM002) [Microso ...
- php内置函数分析之ucfirst()、lcfirst()
ucfirst($str) 将 str 的首字符(如果首字符是字母)转换为大写字母,并返回这个字符串. 源码位于 ext/standard/string.c /* {{{ php_ucfirst Up ...
- alert(1) to win 2
function escape(s) { s = s.replace(/"/g, '\\"'); return '<script>console.log("' ...