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 ...
随机推荐
- yii中调取字段名称时label与labelEx的区别
$form = $this->beginWidget('CActiveForm',array('id' => 'userRegisterForm')); echo $form->la ...
- bind (ERROR 502): bind(0.0.0.0:9501) failed. Error: Address already in use [98] (端口被占用)
运行 swoole_server 服务报错显示端口被占用 解决思路: 1.用命令查看该端口 看是否存在 netstat -anp | grep 9501 2.如果存在 就用 kill对应端口号 ...
- Linux下C语音实现socket发送和接收的小程序
1.什么是socket套接字 socket其实就是计算机通信的端口,可以实现两个计算机之间的通信的一个接口,应用程序在网络上传输就是通过这个借口实现. socket分为三种类型: 字节流套接字(Str ...
- AndroidManifest中的Intent-filter标签
经过测试,intent-filter标签中的: 1. <action android:name="android.intent.action.MAIN" /> 代表这是 ...
- bzoj4455
容斥原理+dp 首先考虑暴力做法,我们希望点和点一对一,那么自然要保存当前点集的状态,需要状压,据说要3^n,那么自然不行 考虑容斥原理,刚才一一对应的限制太强了,我们不要一一对应,只要满足边存在就行 ...
- Struts过滤器
StrutsPrepareAndExecuteFilter过滤器其实是包含2部分的 StrutsPrepareFilter:做准备 StrutsExecuteFilter:进入Struts2的核心处理 ...
- git 的安装使用以及协作流程
git安装: sudo apt-get install git-core git使用: 转:https://www.liaoxuefeng.com/wiki/0013739516305929606dd ...
- C#实现简易ajax调用后台方法
在当前WEB当中,有些人都会抛弃asp.net的服务器控件,转而使用ajax来进行数据的交互和存储. 当我们大量使用ajax的时候,对于新手而言,肯定会创建很多的ashx或aspx页面,通过拼接参数, ...
- BAT或赌在当下或押在未来,谁是王者?
转自:http://www.tmtpost.com/97132.html 百度阿里和腾讯三家本来的核心业务并不冲突,各守一方阵地,但随着各自的收购注资加上业务的延展而慢慢有了交际,阿里和腾讯在移动支付 ...
- hdu1850(nim博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1850 题意:中文题诶- 思路:nim博弈 可以将本题抽象成一般nim博弈,那么有: 1. 对于所有元素 ...