hdu4419
对于这类面积覆盖的题,大致就两点要注意的
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的更多相关文章
- hdu4419 Colourful Rectangle 12年杭州网络赛 扫描线+线段树
题意:给定n个矩形,每个矩形有一种颜色,RGB中的一种.相交的部分可能为RG,RB,GB,RGB,问这n个矩形覆盖的面积中,7种颜色的面积分别为多少 思路:把x轴离散化做扫描线,线段树维护一个扫描区间 ...
- 【HDU4419 Colourful Rectangle】 线段树面积并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...
- HDU-4419 Colourful Rectangle 矩形多面积并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 利用二进制,R为1.G为2.B为4,然后通过异或运算可以得到其它组合颜色.建立7颗线段树,每颗线 ...
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
随机推荐
- 函数和常用模块【day06】:re模块(九)
本节内容 1.简述 2.常用的正则表达式符号 3.常用匹配方法 4.常用方法 5.反斜杠的困扰 6.其他匹配模式 7.总结 一.简述 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程 ...
- 如何使用无线连接来使Android调试手机
进入Android Studio.(我的是2.2版本) File->Setting->Plugins Browse repositories... 搜索 ADB WIFI 并安装 重启An ...
- 去除inline-block出现间距的几种方法
display:inline-block,简单来说就是将对象呈现为inline对象,但是对象的内容作为block对象呈现,之后的内联对象会排列在同一行 比如两个input,默认中间会产生一些间距 &l ...
- scapy官方文档
https://thepacketgeek.com/scapy-p-04-looking-at-packets/ http://biot.com/capstats/bpf.html filter语 ...
- Java——java错误(The Struts dispatcher cannot be found)
这通常是由于使用了struts标签,而没有配置相关联的filter.struts标签只有在http请求通过标签的servlet filter过滤器之后才可用,这些过滤器用来为这些标签初始化struts ...
- VS2017的安装和配置
VS2017专业版安装文件下载:链接:https://pan.baidu.com/s/1tJRYdj_9LzvTSDF5TkkMRg 提取码:tgh5 一些窗口:菜单栏--->视图 字 ...
- luogu P4162 [SCOI2009]最长距离
传送门 可以枚举两个点然后计算答案,至于是否合法,就要看可不可以通过移不超过\(t\)个箱子使得两点连通,也可以看做找一条路径使得路径上的1个数不超过\(t\) 所以可以考虑最短路,相邻的点两两连边, ...
- luogu P4360 [CEOI2004]锯木厂选址
斜率优化dp板子题[迫真] 这里从下往上标记\(1-n\)号点 记\(a_i\)表示前缀\(i\)里面树木的总重量,\(l_i\)表示\(i\)到最下面的距离,\(s_i\)表示\(1\)到\(i-1 ...
- CentOS6.8安装配置sonarqube6.4
下载最新版本的sonar(现在改名叫sonarqube) https://www.sonarqube.org/downloads/ 我下载的版本是Sonarqube6.4 1 使用前需要 ...
- 【Elasticsearch】Elasticsearch在windows下的安装方法
版本 elasticsearch-2.4.4 2.4.4版本下载地址 下载地址1 下载地址2 启动 进入bin目录,双击elasticsearch.bat: 在浏览器中输入http://localho ...