对于这类面积覆盖的题,大致就两点要注意的

1.同一把矩形放在笛卡尔坐标系上做

2.pushup函数要注意下细节:及在统计子区间和之前要先判断是否有子区间

用sum数组来保存区间被覆盖的情况,如果遇到多次覆盖问题,那就开多个sum数组分别保存被覆盖n次的情况

用cnt数组保存区间被完全覆盖的次数,如果是不同类型的矩形需要分别统计或者有特殊要求,那就开多个cnt数组分别保存

pushup如果cnt[rt]超过了k次,满足要求,那么就直接把sum[k]赋值为当前区间长度,然后其他sum数组归零,结束返回

否则如果cnt[rt]不为0,先把所有该区间的sum置零,然后把覆盖了该区间cnt[rt]次对应的sum赋值为当前区间长度如果rt没有子区间就返回,有子区间 i 就从1循环到k,如果cnt[rt]+i>=k,那么对应的sum[k]就是两个子区间的被覆盖i次的长度和,否则sum[i+cnt[rt]]就是两个子区间被覆盖i次的和。结束这次循环后sum[cnt[rt]]还要再减去本区间被覆盖大于cnt[rt]次对应的sum

最后如果cnt[rt]=0,i从1到k循环,如果没有子区间,sum就是0,有的话就是子区间的和

/*
颜色覆盖,多了颜色融合,,
用七个sum去记录七种颜色,三个cnt记录三种不同颜色的覆盖
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 20005
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1
#define ll long long
struct Seg{
int x,y1,y2,c;
Seg(){}
Seg(int a,int b,int c,int d):x(a),y1(b),y2(c),c(d){}
bool operator<(const Seg & a)const
{return x<a.x;}
}segs[maxn];
int y[maxn],toty,tot;
int sum[maxn<<][],cnt[maxn<<][];
map<int,int>mp;
void pushup(int rt,int l,int r){
int tmp=;
if(cnt[rt][]) tmp|=;
if(cnt[rt][]) tmp|=;
if(cnt[rt][]) tmp|=;
//cout << tmp << endl;
for(int i=;i<=;i++)
sum[rt][i]=;
if(tmp){
sum[rt][tmp]=y[r]-y[l];
for(int i=;i<=;i++)
if(l+!=r && tmp!=(tmp|i)){ //如果有更高级的颜色
sum[rt][tmp|i]+=sum[rt<<][i]+sum[rt<<|][i];
sum[rt][tmp]-=sum[rt<<][i]+sum[rt<<|][i];
}
}
else if(l+!=r)
for(int i=;i<=;i++) sum[rt][i]=sum[rt<<][i]+sum[rt<<|][i];
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l && R>=r){
//cout<<c<<endl;
if(c>) cnt[rt][c]+=;
else cnt[rt][-c]-=;
pushup(rt,l,r);
return;
}
int m=l+r>>;
if(L<m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
pushup(rt,l,r);
}
void init(){
tot=toty=;
mp.clear();
memset(sum,,sizeof sum);
memset(cnt,,sizeof cnt);
}
int main(){
int T,x1,x2,y1,y2,c,n;
char col[];
cin >> T;
for(int tt=;tt<=T;tt++){
init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s%d%d%d%d",col,&x1,&y1,&x2,&y2);
if(col[]=='R') c=;
if(col[]=='G') c=;
if(col[]=='B') c=;
segs[tot++]=Seg(x1,y1,y2,c);
segs[tot++]=Seg(x2,y1,y2,-c);
y[toty++]=y1,y[toty++]=y2;
}
sort(y,y+toty);
toty=unique(y,y+toty)-y;
for(int i=;i<toty;i++) mp[y[i]]=i;
sort(segs,segs+tot); ll res[]={};
for(int i=;i<tot;i++){
if(i!=){
for(int j=;j<=;j++)
res[j]+=(ll)(segs[i].x-segs[i-].x)*sum[][j]; }
int y1=mp[segs[i].y1];
int y2=mp[segs[i].y2];
update(y1,y2,segs[i].c,,toty-,); } printf("Case %d:\n",tt);
printf("%lld\n",res[]);
printf("%lld\n",res[]);
printf("%lld\n",res[]);
printf("%lld\n",res[]);
printf("%lld\n",res[]);
printf("%lld\n",res[]);
printf("%lld\n",res[]); }
return ;
}

hdu4419的更多相关文章

  1. hdu4419 Colourful Rectangle 12年杭州网络赛 扫描线+线段树

    题意:给定n个矩形,每个矩形有一种颜色,RGB中的一种.相交的部分可能为RG,RB,GB,RGB,问这n个矩形覆盖的面积中,7种颜色的面积分别为多少 思路:把x轴离散化做扫描线,线段树维护一个扫描区间 ...

  2. 【HDU4419 Colourful Rectangle】 线段树面积并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...

  3. HDU-4419 Colourful Rectangle 矩形多面积并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 利用二进制,R为1.G为2.B为4,然后通过异或运算可以得到其它组合颜色.建立7颗线段树,每颗线 ...

  4. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

随机推荐

  1. eclipse中编辑properties文件无法看到中文

    如果在eclipse中编辑properties文件无法看到中文则参考“Eclipse开发环境配置-indigo.docx”添加propedit插件.

  2. H5新特性之video audio

    1.标签 <video src="~~~" autoplay loop controls controlslist="nodownload nofullscreen ...

  3. yolov2-tiny-voc.cfg 参数解析

    一.参数解析 [net] batch=64 # number of images pushed with a forward pass through the network subdivisions ...

  4. secure CRT the remote system refused the connection 解决办法

    1.安装ssh服务器和客户端 apt-get install openssh-server apt-get install openssh-client 2.重启ssh /etc/init.d/ssh ...

  5. <T extends Comparable<? super T>>什么意思

    <T extends Comparable<? super T>>首先这是运用了java的泛型①extends后面跟的类型如<任意字符 extends 类/接口>表 ...

  6. 微信现金红包 python

    微信现金红包发送接口,好像没法限制一个用户一个活动只能领取一次红包,在调用红包接口上,自己做了限制 REDPACK_RECORD 建表sql -- Create table create table ...

  7. BZOJ3527 [Zjoi2014]力 【fft】

    题目 给出n个数qi,给出Fj的定义如下: 令Ei=Fi/qi,求Ei. 输入格式 第一行一个整数n. 接下来n行每行输入一个数,第i行表示qi. 输出格式 n行,第i行输出Ei.与标准答案误差不超过 ...

  8. oracle sum(x) over( partition by y ORDER BY z ) 分析

    之前用过row_number(),rank()等排序与over( partition by ... ORDER BY ...),这两个比较好理解: 先分组,然后在组内排名. 今天突然碰到sum(... ...

  9. mysql案例-sysbench安装测试

    一 地址 githup地址https://github.com/akopytov/sysbench二 版本 sysbench 1.0.15 curl -s https://packagecloud.i ...

  10. VMware12虚拟机中Ubuntu16.04安装CPU版本Caffe

    首先,可以自行下载VMware12进行安装,基本上都是直接点击‘下一步’直到安装完成,这里重点讲一下Ubuntu16及Caffe的安装步骤 第一步: 下载Ubuntu16.04版本的文件,这里给出链接 ...