[HDU 4419] Colourful Rectangle (扫描线 矩形面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419
题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积。
矩形面积并的扫描线维护的是长度,这道题就是维护每个颜色的长度,写起来很蛋疼。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL; struct Node{
int l,r,h,d,c;
bool operator<(const Node& a) const{
return h<a.h;
}
Node(int L=,int R=,int H=,int D=,int C=):l(L),r(R),h(H),d(D),c(C){}
};
const int MAX_N = ;
int T,n;
Node t[MAX_N<<];
int dsum[MAX_N<<][],b[MAX_N<<];
LL sum[];
int scol[MAX_N<<],len[MAX_N<<][]; void push_up(int idx,int l,int r){
int state = (dsum[idx][]>?:)|(dsum[idx][]>?:)|(dsum[idx][]>?:);
if( state ) {
memset(len[idx],,sizeof(len[idx]));
len[idx][state] = b[r+] - b[l];
for(int i=;i<;i++){
if( state!=(state|i) ){
int tmp = len[idx<<][i]+len[idx<<|][i];
len[idx][state|i] += tmp;
len[idx][state] -= tmp;
}
}
} else if(l!=r){
for(int i=;i<;i++){
len[idx][i] = len[idx<<][i] + len[idx<<|][i];
}
} else {
memset(len[idx],,sizeof(len[idx]));
}
} void update(int L,int R,int x,int c,int idx,int l,int r){
if( L<=l&&R>=r ){
dsum[idx][c] += x;
} else {
int m = l+r>>;
if( L<=m ) update(L,R,x,c,idx<<,l,m);
if( R>m ) update(L,R,x,c,idx<<|,m+,r);
}
push_up(idx,l,r);
} int main(){
scanf("%d",&T);
int kase = ;
while(T--){
memset(dsum,,sizeof(dsum));
memset(len,,sizeof(len));
memset(scol,,sizeof(scol));
scanf("%d",&n);
char col[];
int x1,y1,x2,y2;
int ptr = , ptrb = ;
for(int i=;i<n;i++){
scanf("%s%d%d%d%d",col,&x1,&y1,&x2,&y2);
int cc;
if( col[]=='R' ) cc=;
else if( col[]=='G' ) cc = ;
else if( col[]=='B' ) cc = ;
t[ptr++] = Node(x1,x2,y1,,cc);
t[ptr++] = Node(x1,x2,y2,-,cc);
b[ptrb++] = x1;
b[ptrb++] = x2;
}
sort(t,t+ptr);
sort(b,b+ptrb);
int ub = unique(b,b+ptrb) - b;
memset(sum,,sizeof(sum)); for(int i=;i<ptr-;i++){
int l = lower_bound(b,b+ub,t[i].l) - b;
int r = lower_bound(b,b+ub,t[i].r) - b - ;
update(l,r,t[i].d,t[i].c,,,ub-);
if( t[i].h!=t[i+].h ){
for(int j=;j<;j++){
sum[j] += (LL)(t[i+].h-t[i].h)*(LL)(len[][j]);
}
}
}
printf("Case %d:\n",kase++);
printf("%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n",sum[],sum[],sum[],sum[],sum[],sum[],sum[]);
}
return ;
}
[HDU 4419] Colourful Rectangle (扫描线 矩形面积并)的更多相关文章
- hdu 4419 Colourful Rectangle (离散化扫描线线段树)
Problem - 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| ...
- hdu 4419 Colourful Rectangle
http://acm.hdu.edu.cn/showproblem.php?pid=4419 题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积. 转化为二进制表示颜色:001 ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- HDU1542 扫描线(矩形面积并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- [LeetCode]223. Rectangle Area矩形面积
/* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...
随机推荐
- Presto 来自Facebook的开源分布式查询引擎
Presto是一个分布式SQL查询引擎, 它被设计为用来专门进行高速.实时的数据分析.它支持标准的ANSI SQL,包括复杂查询.聚合(aggregation).连接(join)和窗口函数(windo ...
- Value Dispose() cannot be called while doing CreateHandle().
在backgroundWorker run之前show出了一个窗体_frm. _frmpw = new FrmPleaseWait(); _frmpw.SetMsg("正在请求") ...
- HackerRank "Poisonous Plants"
I had the same BFS idea as one of the AC code this: because dying pattern is spead from the initial ...
- lucene之排序、设置权重、优化、分布式搜索(转)
lucene之排序.设置权重.优化.分布式搜索(转) 1. 基本应用 using System;using System.Collections.Generic;using System.Text;u ...
- 51nod 1348 乘积之和
用(r-l+2)维向量f[l,r]表示区间[l,r]内选i个数(0<=i<=r-l+1)相乘的所有方案之和,可以发现f[l,r]=f[l,m]*f[m+1,r],题目模数100003较小, ...
- bzoj3034: Heaven Cow与God Bull
Description __int64 ago,there's a heaven cow called sjy...A god bull named wzc fell in love with her ...
- js 字符串转换为数值
原帖地址:http://www.cnblogs.com/jenney-qiu/archive/2012/02/27/2369848.html 使用parseInt()你可以从字符串中获取数值,该方法接 ...
- 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- tarjan算法 POJ3177-Redundant Paths
参考资料传送门 http://blog.csdn.net/lyy289065406/article/details/6762370 http://blog.csdn.net/lyy289065406/ ...
- 黄聪:wordpress如何获取当前分类页面的ID、名称、别名(slug)
<? global $wp_query; $cat_ID = get_query_var('cat'); $category = get_category($cat_ID); echo $cat ...