链接

分别求出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的更多相关文章

  1. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  5. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. Maximal Rectangle

    很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...

  8. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  9. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

随机推荐

  1. hdu 2671 shǎ崽 OrOrOrOrz(排序)

    题意:排序后按题目要求输出 思路:排序 #include<iostream> #include<stdio.h> #include<algorithm> using ...

  2. Android工程的目录结构

    1.最大限度的将不需要出现在Java代码中的文件和代码本身分离开来 2.使用XML标记语言定义UI和数据结构 3.对于工程中的文件存储在工程目录中的那个位置有着严格的规定,在编译过程中Android会 ...

  3. 并不对劲的BJOI2019

    一些感想 现实并非游戏,并不支持反复刷关 猎人和防御工事一起被老山龙摧毁了: 猎人惨死雨中,结云村永无放晴之日: 猎人被狂龙病毒侵蚀,天空山上黑蚀龙泛滥. 好像这才是怪物猎人系列的真实结局呢 day ...

  4. CodeForces - 597C:Subsequences (主席树+DP)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...

  5. [Codeforces 787D] Legacy

    [题目链接] https://codeforces.com/contest/787/problem/D [算法] 线段树优化建边 ,  然后用Dijkstra算法求单源最短路 时间复杂度 : O((N ...

  6. 非旋treap套线段树

    BZOJ3065. 去年用pascal 块链过了.. 今年来试了试非旋treap大法   注定被块链完爆 代码留这. 第一份 :辣鸡的  垃圾回收做法  跑得极慢 #include <bits/ ...

  7. checkbox的几种遍历方法

    html代码如下: <div> <input type="checkbox" name="ckb" value="1" / ...

  8. HDOj-1425

    sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. JAVA通过信号量避免死锁

    死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 导致死锁的根源在于不适当地运用"synchronized ...

  10. python常用框架及第三方库

    python常用框架及第三方库 一.Web框架 1.Django: 开源web开发框架,它鼓励快速开发,并遵循MVC设计,比较庞大,开发周期短.Django的文档最完善.市场占有率最高.招聘职位最多. ...