[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 ...
随机推荐
- Maven中Spring-Data-Redis存储对象(redisTemplate)
Redis是一种nosql数据库,在开发中常用做缓存.Jedis是Redis在java中的redis- client.在此之前,希望已经了解redis的基本使用和Maven的使用.建立Maven Pr ...
- js net 除法取整
1.js中 在编程运算中,除法取整数是比较常用的!一般的编程语言都有内置的函数,JS 脚本也不例外.在JavaScript 中,实现除法取整数有两种方法,即是两个内置函数:Math.floor 和Ma ...
- java单例模式和双例模式
今天朋友找我给做道题,双例模式,我是没听说过,都说是单例模式和多例模式, 也不知道双例模式什么时候用,就简单写了一个案例,不知道对不对,个人感觉蛮对的,双例就是单例+单例,废话不说了!!!! /* * ...
- ActionScript ArrayCollection sort
var sortByOrderId:Sort = new Sort; sortByOrderId.fields = [new SortField("orderId")]; orde ...
- 05文件与IO
这节主要学习了read.write.lseek.目录访问(opendir.readdir.closedir)这几个系统调用及其简单的应用. 一旦有了与一个打开文件描述相连的文件描述符,只要该文件是用O ...
- LintCode "Heapify"
My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...
- POJ #1141 - Brackets Sequence - TODO: POJ website issue
A bottom-up DP. To be honest, it is not easy to relate DP to this problem. Maybe, all "most&quo ...
- Avant Browser
Avant Browser Avant 浏览器友好的用户界面为你的网络冲浪带来全新的效率和透明性.软件版本的不断升级使产品的可靠性稳步提高. 没有广告.没有恶意软件! Avant 浏览器是免费的.10 ...
- Puppet's Commands 3.7
Puppet's Commands Puppet’s command line interface consists of a single puppet command with many subc ...
- java中用中国网建提供的SMS短信平台发送短信
接下来的项目需求中提到需要短信发送功能,以前没有做过,因此便在网上搜了一下.大体上说的都是有三种方法,分别是sina提供的webservice接口.短信mao和中国网建提供的SMS短信平台. 这三种方 ...