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 ...
随机推荐
- C Yuhao and a Parenthesis
Yuhao and a Parenthesis time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- P4514 上帝造题的七分钟(二维树状数组)
P4514 上帝造题的七分钟 二维树状数组 差分维护区间加法,区间求和 #include<cstdio> int read(){ ,f=; ') f=f&&(c!='-') ...
- Sql Server 2008安装时提示重启计算机失败解决办法
在键盘上按下组合键[Win]+[R],调出运行窗口. 在窗口中输入“regedit”,点击确定,打开注册表管理界面. 在注册表左侧目录栏中找到如下位置:“HKEY_LOCAL_MACHINE\ ...
- OGG复制进程延迟不断增长
1.注意通过进程查找sql_id时,进程号要查询两次 2.杀进程的连接 https://www.cnblogs.com/kerrycode/p/4034231.html 参考资料 1.https:// ...
- error: must use ‘class’ tag to refer to type ‘XXX’ in this scope
开发环境: Qt Creator 4.8.2 在写程序的时候,遇到了编译器报错 error: must use 'class' tag to refer to type 'XXX' in this s ...
- css 伪类选择器:checked实例讲解
css :checked伪类选择器介绍 css :checked伪类选择器用于选择匹配所有被选中的单选按钮(radio)或复选框(checkbox),你可以结合:checked伪类选择器和:not选择 ...
- Vue 组件间的传值(通讯)
组件之间的通讯分为三种 父给子传 子给父传 兄弟组件之间的通讯 1 父组件给子组件传值 子组件嵌套在父组件内部,父组件给子组件传递一个标识,在子组件内部用props接收,子组件在模板里可以通过{{}} ...
- 在 CentOS 上部署 GitLab (自托管的Git项目仓库)
参考资料https://github.com/mattias-ohlsson/gitlab-installer/blob/master/gitlab-install-el6.sh 环境准备OS: Ce ...
- 利用已控的标边界一台机器的 beacon对目标内网进行各种存活探测
本节的知识摘要: 基于常规 tcp / udp 端口扫描的内网存活探测 基于 icmp 的内网存活探测 基于 arp 的内网存活探测 加载外部脚本进行的各种存活探测 基础环境说明:: WebServe ...
- No module named flask 导包失败,Python3重新安装Flask模块
在部署环境过程中,通过pip install -r requirements.txt安装包,结果启动项目时总是报错,显示没有flask模块,通过pip install flask还是不行,于是下载fl ...