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 ...
随机推荐
- [2019杭电多校第二场][hdu6602]Longest Subarray(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题目大意为求最长的区间,满足C种数字在区间内要么不出现,要么出现的次数都不小于K. 大致的分析一 ...
- Python内建函数reduce()用法
reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,下面讲述Python内建函数reduce()用法. ...
- Django - Xadmin (五) POP
Django - Xadmin (五) POP 功能及逻辑描述 pop 功能:在添加数据时,对于需要选择的多对多字段,在其 input 框边加上一个按钮,点击该按钮可以实现跳转到添加该字段数据的页面: ...
- 查看Xcode里的描述文件
iOS应用打包离不开描述文件,也就是mobileprovision文件. 一般我们的操作是双击,Xcode就会运行该文件.但是具体文件里是什么,Xcode又是否真的加载了该文件?文件里又描述了什么呢? ...
- ReentrantLock等待通知机制Condition介绍
Object类中的wait(),notify()和notifyAll()可以实现线程的等待通知模型,同样在ReentrantLock中可以借助Condition来完成这种机制.本篇就简要介绍Condi ...
- Python之文件的读
python中文件的读操作:以只读的形式打开文件->逐行读取文件中的内容->关闭文件 代码如下 #文件的读 f = file(u'F:\\python\\homework.txt', 'r ...
- 好玩的Linux命令-1
Ag:比grep.ack更快的归递搜索文件内容 1:首先在linux创建个sh文件->ag.sh 2:在ag.sh里面输入如下内容并保存 #!/bin/bash set -x TEMP_DIR= ...
- 利用C51单片机模拟SPI进行双机通信
SPI协议简述 SPI,是英语Serial Peripheral interface的缩写,顾名思义就是串行外围设备接口.由Motorola首创.SPI接口主要应用在 EEPROM,FLASH,实时时 ...
- python基础--局部变量与全局变量
#全局变量作用于全局或整个程序中,程序执行完毕后销毁,局部变量作用在当前函数中,调用函数执行完毕及销毁 #如果函数的内容无global关键字,优先读取同名局部变量,如果没有同名局部变量,只能读取同名全 ...
- bootstrap-table使用stickyHeader固定表头时,表头不跟随表体水平滚动问题解决
解决方法: onAll: function () { // 修复stickyHeader表头不跟随表体水平滚动的问题 if (params.stickyHeader) { var fixedTable ...