hdu4419Colourful Rectangle
分别求出7种颜色覆盖的面积。
做法:每种颜色设定一个标号,以二进制表示R:100 G:010 B:001 。这样很明显可以知道RG:110 GB:011 以此类推。
求解时,需要开一个二维标记数组,标记了这一段的某种颜色被标记了几次,然后类似状压的方式求解。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define N 41010
#define LL __int64
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
map<int,int>f;
int s[N<<][];
int fs[N<<],a[N],ff[N<<][];
int val[N];
int id[];
LL num[N];
struct node
{
int x1,x2,y;
int f;
node(){}
node(int x1,int x2,int y,int f):x1(x1),x2(x2),y(y),f(f){}
bool operator <(const node &S)const
{
return y<S.y;
}
}p[N];
void up(int w,int l,int r)
{
int i;
if(fs[w]==)
{
for(i = ; i <= ; i++)
{
if(l==r)
s[w][i] = ;
else
s[w][i] = s[w<<][i]+s[w<<|][i];
}
}
else
{
int res = val[r+]-val[l];
int cnt[] = {};
for(i = ; i <= ; i++)
{
if(l==r)
{
if(i==fs[w])
cnt[i] += val[r+]-val[l];
else
cnt[i] += ;
}
else
{
cnt[i|fs[w]] += s[w<<][i]+s[w<<|][i];
}
}
for(i = ; i <= ; i++)
res-=cnt[i];
cnt[fs[w]]+=res;
for(i = ; i <= ; i++)
s[w][i] = cnt[i];
}
}
void build(int l,int r,int w)
{
int i;
for(i = ;i<= ; i++)
{s[w][i] = ff[w][i] = ;}
fs[w] = ;
if(l==r)
{
return ;
}
int m = (l+r)>>;
build(l,m,w<<);
build(m+,r,w<<|);
}
void update(int a,int b,int d,int l,int r,int w)
{
if(a<=l&&b>=r)
{
ff[w][abs(d)]+=d/abs(d);
if(d>)
fs[w]|=d;
else if(ff[w][-d]==) fs[w]+=d;
up(w,l,r);
return ;
}
int m = (l+r)>>;
if(a<=m) update(a,b,d,l,m,w<<);
if(b>m) update(a,b,d,m+,r,w<<|);
up(w,l,r);
}
int main()
{
id['R'] = ;
id['G'] = ;
id['B'] = ;
int i,j,t,n,cas = ;
char sr[];
cin>>t;
while(t--)
{
f.clear();
scanf("%d",&n);
memset(num,,sizeof(num));
int g= ,dd[];
for(i = ; i <= n; i++)
{
int x1,x2,y1,y2,k;
scanf("%s%d%d%d%d",sr,&x1,&y1,&x2,&y2);
if(sr[]=='R') k = id['R'];
else if(sr[]=='G') k = id['G'];
else k = id['B'];
p[++g] = node(x1,x2,y1,k);
a[g] = x1;
p[++g] = node(x1,x2,y2,-k);
a[g] = x2;
}
sort(a+,a+g+);
sort(p+,p+g+);
int o = ;
f[a[]] = ++o;
val[o] = a[];
for(i = ; i <= g ; i++)
if(a[i]!=a[i-])
{
f[a[i]] = ++o;
val[o] = a[i];
}
build(,o-,); for(i = ; i < g; i++)
{
int l = f[p[i].x1];
int r = f[p[i].x2]-; //cout<<l<<" "<<r<<endl;
if(l<=r)
{
update(l,r,p[i].f,,o-,);
}
for(j = ; j <= ; j++)
num[j] += (LL)(p[i+].y-p[i].y)*s[][j];
}
dd[] = ,dd[] = ,dd[] = ,dd[] = ,dd[] = ,dd[] = ,dd[] = ;
printf("Case %d:\n",cas++);
for(i = ; i <= ; i++)
printf("%I64d\n",num[dd[i]]);
}
return ;
}
hdu4419Colourful Rectangle的更多相关文章
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Maximal Rectangle
很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
随机推荐
- BZOJ-2257:瓶子和燃料(裴蜀定理)
jyy就一直想着尽快回地球,可惜他飞船的燃料不够了. 有一天他又去向火星人要燃料,这次火星人答应了,要jyy用飞船上的瓶子来换.jyy的飞船上共有 N个瓶子(1<=N<=1000) ,经过 ...
- 工艺(SAM)
传送门 用SAM可以非常轻松的解决问题. 只要把原串向SAM中插入两次,之后直接从\(t_0\)状态开始每次贪心跑最小就可以了. 因为这个题要用map,所以之前取begin即可. // luogu-j ...
- C++之函数适配器--绑定器bind原理图解
转自:http://www.cnblogs.com/xusd-null/p/3698969.html#3081606 本文解释了bind 是如何工作的.为了清晰,图中的语法作了一些简化(例如,省略函数 ...
- mysql函数之七:replace() MySQL批量替换指定字段字符串
mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); REPLACE(str,from_str,to_str) 在字符串 st ...
- js联动
html: <!-- 省 --> <div class="col-sm-2"> <select name="p_id"> & ...
- Ubuntu16.04 安装Python3.6 报错
问题: 在安装Python 3.6,执行make install 时出现以下错误: zipimport.ZipImportError: can't decompress data; zlib not ...
- UVaLive 3905 Meteor (扫描线)
题意:给定上一个矩形照相机和 n 个流星,问你照相机最多能拍到多少个流星. 析:直接看,似乎很难解决,我们换一个思路,我们认为流星的轨迹就没有用的,我们可以记录每个流星每个流星在照相机中出现的时间段, ...
- 201621123016 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键字:接口,多态,Comparable,Comparator 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不 ...
- 分解gif图片并保存
/** Gif的步骤 1. 拿到Gifd的数据 2. 将Gif分解为一帧帧 3. 将单帧数据转为UIImage 4. 单帧图片保存 */ #import <ImageIO/ImageIO.h&g ...
- 小程序隐藏或自定义 scroll-view滚动条
css 隐藏滚动条 ::-webkit-scrollbar { width:; height:; color: transparent; } 自定义滚动条样式 ::-webkit-scrollbar ...