hdu 4419 Colourful Rectangle (离散化扫描线线段树)
题意不难,红绿蓝三种颜色覆盖在平面上,不同颜色的区域相交会产生新的颜色,求每一种颜色的面积大小。
比较明显,这题要从矩形面积并的方向出发。如果做过矩形面积并的题,用线段树做的,这题就只差一个把每个区域计算单独出来的思路了。
这里不详细介绍扫描线,只是说一下针对这题的做法。其实网上有好多版本,都是直接单独计算答案的。然而,我稍微绕了个小弯,我觉得我这种处理方法也是比较容易想到以及实现的。如果我们用7棵线段树计算R、G、B、R||G、R||B、B||G、R||G||B这7种情况,我们并不能直接得到所要的答案。不过可以知道,ans(G)=S(R||G||B)-S(R||B)的(这里的||是两种颜色的面积并的意思)。同理我们可以得到ans(B)、ans(R)。然后根据这个,我们可以得到ans(RB)=S(R||G||B)-ans(R)-ans(B)-S(G)。同理可以得到ans(RG),ans(GB)。最后就只剩ans(RGB)了。
代码如下:
#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
#include <algorithm> using namespace std; map<char, int> id;
void preMap() {
id['R'] = ;
id['G'] = ;
id['B'] = ;
} const int N = ;
const int K = << ;
map<int, int> xid;
int rx[N], xn; struct Node {
int id, x1, x2, y;
bool end;
Node() {}
Node(int id, int x1, int x2, int y, bool end)
: id(id), x1(x1), x2(x2), y(y), end(end) {}
} node[N]; void input(int n) {
char buf[];
int a, b, c, d;
for (int i = ; i < n; i++) {
scanf("%s%d%d%d%d", buf, &a, &b, &c, &d);
if (a > c) swap(a, c);
if (b > d) swap(b, d);
rx[i << ] = a;
rx[i << | ] = c;
node[i << ] = Node(id[buf[]], a, c, b, false);
node[i << | ] = Node(id[buf[]], a, c, d, true);
}
n <<= ;
sort(rx, rx + n);
xn = unique(rx, rx + n) - rx;
// for (int i = 0; i < xn; i++) cout << rx[i] << ' '; cout << endl;
xid.clear();
for (int i = ; i < xn; i++) xid[rx[i]] = i;
for (int i = ; i < n; i++) {
node[i].x1 = xid[node[i].x1];
node[i].x2 = xid[node[i].x2];
}
} #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define root 0, xn - 1, 1 int sum[K][N << ];
int mk[K][N << ];
int cur; void up(int rt, int l, int r) {
if (mk[cur][rt]) sum[cur][rt] = rx[r + ] - rx[l];
else sum[cur][rt] = sum[cur][rt << ] + sum[cur][rt << | ];
} void build(int l, int r, int rt) {
sum[cur][rt] = mk[cur][rt] = ;
if (l == r) return ;
int m = l + r >> ;
build(lson);
build(rson);
} void update(int k, int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
mk[cur][rt] += k;
if (l != r) up(rt, l, r);
else {
if (mk[cur][rt]) sum[cur][rt] = rx[r + ] - rx[l];
else sum[cur][rt] = ;
}
return ;
}
int m = l + r >> ;
if (L <= m) update(k, L, R, lson);
if (m < R) update(k, L, R, rson);
up(rt, l, r);
} bool cmp(Node a, Node b) { return a.y < b.y || a.y == b.y && a.end < b.end;} typedef long long LL; void work(int n) {
n <<= ;
LL tt[K];
memset(tt, , sizeof(tt));
sort(node, node + n, cmp);
if (node[].end) { puts("shit!!"); while () ;}
for (cur = ; cur < K; cur++) {
build(root);
if (cur & << node[].id) update(, node[].x1, node[].x2 - , root);
}
for (int i = ; i < n; i++) {
for (cur = ; cur < K; cur++) {
if (sum[cur][] < || sum[cur][] > 1e9) { puts("shit!!!"); while () ;}
tt[cur] += (LL) sum[cur][] * (node[i].y - node[i - ].y);
if (cur & ( << node[i].id)) update(node[i].end ? - : , node[i].x1, node[i].x2 - , root);
}
// for (int i = 0; i < K; i++) cout << tt[i] << ' '; cout << endl;
}
LL sgl[], dbl[], lst = tt[K - ];
for (int i = ; i < ; i++) {
sgl[i] = tt[K - ] - tt[(K - ) ^ ( << i)];
lst -= sgl[i];
cout << sgl[i] << endl;
}
for (int i = ; i < ; i++) {
dbl[i] = tt[K - ];
for (int j = ; j < ; j++) {
if (i != j) dbl[i] -= sgl[j];
else dbl[i] -= tt[ << j];
}
lst -= dbl[i];
}
for (int i = ; i >= ; i--) cout << dbl[i] << endl;
cout << lst << endl;
} int main() {
// freopen("in", "r", stdin);
preMap();
int T, n;
scanf("%d", &T);
for (int cas = ; cas <= T; cas++) {
scanf("%d", &n);
input(n);
printf("Case %d:\n", cas);
work(n);
}
return ;
}
感觉这种方法写起来不会吃力,就是计算上面会稍微需要思考。不过我还是因为把xid写成xd导致wa了一个晚上。_(囧」∠)_
——written by Lyon
hdu 4419 Colourful Rectangle (离散化扫描线线段树)的更多相关文章
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- POJ_3470 Walls 【离散化+扫描线+线段树】
一.题面 POJ3470 二.分析 POJ感觉是真的老了. 这题需要一些预备知识:扫描线,离散化,线段树.线段树是解题的关键,因为这里充分利用了线段树区间修改的高效性,再加上一个单点查询. 为什么需要 ...
- HDU 1542:Atlantis(扫描线+线段树 矩形面积并)***
题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...
- HDU 4419 Colourful Rectangle(线段树+扫描线)
题目链接 主要是pushup的代码,其他和区间更新+扫描线差不多. 那个区间如果要再刷一层x,那么sum[x][rt] = que[r+1] - que[l];但是如果原本有颜色为i,颜色将会变成i| ...
- [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...
- poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)
题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...
- hdu 4419 Colourful Rectangle
http://acm.hdu.edu.cn/showproblem.php?pid=4419 题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积. 转化为二进制表示颜色:001 ...
- HDU 1828:Picture(扫描线+线段树 矩形周长并)
题目链接 题意 给出n个矩形,求周长并. 思路 学了区间并,比较容易想到周长并. 我是对x方向和y方向分别做两次扫描线.应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的 ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
随机推荐
- SQLyog12.0.9下载、安装和破解
原文转载连接:https://blog.csdn.net/lihua5419/article/details/73881837/ sqlyog百度云链接(永久有效):http://pan.baidu. ...
- eclipse修改中文注释的字体(亲测有用!)
Window –> Preferences –> General –> Appearance –> Colors and Fonts –> Basic –> Tex ...
- Object Pool 对象池的C++11使用(转)
很多系统对资源的访问快捷性及可预测性有严格要求,列入包括网络连接.对象实例.线程和内存.而且还要求解决方案可扩展,能应付存在大量资源的情形. object pool针对特定类型的对象循环利用,这些对象 ...
- const、引用与指针
前提 我们忽略掉了相同类型是否可以赋值的情况(我到现在的学习里都还可以相互赋值),以及类型兼容的情况.只考虑const.&.*等修饰符带来的影响 类型兼容: 强制类型转换 基类与子类间的兼容 ...
- dingo/api 使用 知识
Dingo 能为Laravel提供一整套包括从路由,到认证的RESTful API开发工具 Laravel & Lumen RESTFul API 扩展包:Dingo API(一) —— 安装 ...
- CMake学习笔记二
CMake预定义变量 PROJECT_SOURCE_DIR 工程的根目录 PROJECT_BINARY_DIR 运行cmake命令的目录,通常是${PROJECT_SOURCE_DIR}/build ...
- day5 from 金角大王
Python 之路 Day5 - 常用模块学习 本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...
- DHCP服务器安装、测试
df:disk free df -h 查询空余磁盘 find / -name TechSungWeiXin 查询TechSungWeiXin的位置 find / -name YunyueWeixin_ ...
- java定时(循环)执行一个方法
java中设置定时任务用Timer类可以实现. 一.延时执行 首先,我们定义一个类,给它取个名字叫TimeTask,我们的定时任务,就在这个类的main函数里执行.代码如下: package test ...
- PLAY2.6-SCALA(九) WebSockets
WebSockets是一种支持全双工通信的套接字.现代的html5通过js api使得浏览器天生支持webSocket.但是Websockets在移动端以及服务器之间的通信也非常有用,在这些情况下可以 ...