题目链接: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. UCloud内核热补丁技术揭秘

  2. SSH_框架整合3-删除

    一.普通删除 1 完善src中 类: (1)EmployeeDao.java中: //2 删除 public void delete(Integer id){ String hql="DEL ...

  3. Oracle数据库中实现mysql数据库中auto-increment功能

    在Mysql数据库中,想要实现一条数据的自增一功能(即插入此数据时填写null即可,系统自动+1),可直接在所在列使用语句auto-increment. id int primary key auto ...

  4. EINTR、ERESTARTSYS和SIGINT

    1. 驱动使用down_interruptible,并在该函数返回非零值时返回-EINTR:应用程序不处理signal,使用CTRL-C退出应用程序: 驱动从down_interruptible返回, ...

  5. MySQL存储引擎【InnoDB、MyISAM、Memory】

    数据库,MySQL这样存在多存储引擎的数据库软件,清楚常见的存储引擎的区别,使用合适的存储引擎,使得项目跑的更顺畅,有时候对于一个项目,甚至比项目本身都重要.这篇文章,旨在浅谈常见的三种存储引擎的区别 ...

  6. 【VB技巧】VB ListView 控件功能使用详解

    来源:http://lcx.cc/?i=494 ListView控件 在工具箱上击鼠标右键,选择快捷菜单的Components(部件)项,在控件列表中选择Microsoft Windows Commo ...

  7. 4. Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  8. 3. c的输入输出

    putchar与getchar操作输入输出通道 #include <stdio.h> #include <ctype.h> main(){ int c; while((c = ...

  9. NSString类的相关用法

    一.NSString字符串连接NSString* string; // 结果字符串 NSString* string1, string2; //已存在的字符串 1. string = [NSStrin ...

  10. 获取OpenCV中RotatedRect的绝对角度

    opencv中RotatedRect的angle这个成员变量总是诡异的不同寻常(http://stackoverflow.com/questions/15956124/minarearect-angl ...