题目链接: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 (扫描线 矩形面积并)的更多相关文章

  1. hdu 4419 Colourful Rectangle (离散化扫描线线段树)

    Problem - 4419 题意不难,红绿蓝三种颜色覆盖在平面上,不同颜色的区域相交会产生新的颜色,求每一种颜色的面积大小. 比较明显,这题要从矩形面积并的方向出发.如果做过矩形面积并的题,用线段树 ...

  2. HDU 4419 Colourful Rectangle --离散化+线段树扫描线

    题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...

  3. HDU 4419 Colourful Rectangle(线段树+扫描线)

    题目链接 主要是pushup的代码,其他和区间更新+扫描线差不多. 那个区间如果要再刷一层x,那么sum[x][rt] = que[r+1] - que[l];但是如果原本有颜色为i,颜色将会变成i| ...

  4. hdu 4419 Colourful Rectangle

    http://acm.hdu.edu.cn/showproblem.php?pid=4419 题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积. 转化为二进制表示颜色:001 ...

  5. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  6. HDU1542 扫描线(矩形面积并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  8. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  9. [LeetCode]223. Rectangle Area矩形面积

    /* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...

随机推荐

  1. 关于ExpandableListView用法的一个简单小例子

    喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...

  2. C#执行DOS命令(CMD命令)

    在c#程序中,有时会用到调用cmd命令完成一些功能,于是在网上查到了如下方法,实现了c#执行DOS命令,并返回结果.         //dosCommand Dos命令语句         publ ...

  3. [mysql] mysql explain 使用

    explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 先解析一条sql语句,看出现什么内容 EXPLAINSELECTs.uid, ...

  4. Ext JS treegrid 发生的在tree上增加itemclick 与在其它列上增加actioncolumn 发生事件冲突(event conflict)的解决办法

    Ext JS treegrid 发生的在tree上增加itemclick 与在其它列上增加actioncolumn 发生事件冲突(event conflict)的解决办法 最近在适用Ext JS4开发 ...

  5. jQuery实现登录提示

    实现效果:将鼠标聚焦到邮箱地址文本框时,文本框 内的"请输入邮箱地址"文字将被清除:   若没有输入任何内容,鼠标移除后邮箱地址文本框被还原. <!DOCTYPE html& ...

  6. C++ 11中的右值引用以及std::move

    看了很多篇文章,现在终于搞懂了C++ 中的右值以及std::move   左值和右值最重要的区别就是右值其实是一个临时的变量 在C++ 11中,也为右值引用增加了新语法,即&&   比 ...

  7. POI按照源单元格设置目标单元格格式

    原文:http://jjw198874.blog.163.com/blog/static/1889845522011102401854234/ POI按照源单元格设置目标单元格格式 poi按照一个源单 ...

  8. Akka(二) - Future

    1. future的所有方法都是非阻塞立即返回的 (1)future都要有TimeOut和ExecutionContextExecutor这2个隐士参数 (2)打印future object Hell ...

  9. [CSS]如何正确使用ID和Class?

    作者:DarkZone链接:https://www.zhihu.com/question/19550864/answer/23440690来源:知乎 以下摘自<精通CSS:高级Web标准解决方案 ...

  10. 502 bad gateway 可能的错误原因

    1.PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间 #http代码段中增加 fastcgi_connect_timeout ...