HDU - 4419 Colourful Rectangle【扫描线】
Colourful Rectangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1902 Accepted Submission(s): 792
Problem Description
We use Red, Green and Blue to make new colours. See the picture below:
Now give you n rectangles, the colour of them is red or green or blue. You have calculate the area of 7 different colour. (Note: A region may be covered by same colour several times, but it’s final colour depends on the kinds of different colour)
Input
The first line is an integer T(T <= 10), the number of test cases. The first line of each case contains a integer n (0 < n <= 10000), the number of rectangles. Then n lines follows. Each line start with a letter C(R means Red, G means Green, B means Blue) and four integers x1, y1, x2, y2(0 <= x1 < x2 < 10^9, 0 <= y1 < y2 < 10^9), the left-bottom's coordinate and the right-top's coordinate of a rectangle.
Output
For each case, output a line "Case a:", a is the case number starting from 1,then 7 lines, each line contain a integer, the area of each colour. (Note: You should print the areas as the order: R, G, B, RG, RB, GB, RGB).
Sample Input
3 2 R 0 0 2 2 G 1 1 3 3 3 R 0 0 4 4 G 2 0 6 4 B 0 2 6 6 3 G 2 0 3 8 G 1 0 6 1 B 4 2 7 7
Sample Output
Case 1: 3 3 0 1 0 0 0 Case 2: 4 4 12 4 4 4 4 Case 3: 0 12 15 0 0 0 0
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
Recommend
liuyiding
带颜色的面积并,开始写的是用线段树维护颜色,更新到点上,T了。但仔细想想,每次查询的都是整体,不需要向下push,只需要向上传递信息即可。那么这样就可以解决问题了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include "bits/stdc++.h"
#define LOGN 10
#define MAXN (1<<LOGN)
#define MAXNODES 3*( (1<<(2*LOGN)) / 4 + 100)
#define son(x) (p*4-2+x)
using namespace std;
char tc[150];
struct line//扫描线
{
int x,l,r,color;
int flag;
bool friend operator < (line a,line b){return a.x<b.x;}
}l[20004];
int ubw[20004];//离散化数组
struct tre
{
int l,r,ylen,color[10],len[10],yl,yr;//color表示区间直接覆盖的颜色,len表示7种颜色的长度
}t[80004];
void pushup(int rt)
{
int sta=0;//当前区间被直接覆盖的颜色状态
for (int i = 1; i <= 3; ++i) if(t[rt].color[i]>0)sta|=(1<<(i-1));//更新sta
for (int i = 0; i < 10; ++i) t[rt].len[i]=0;//因为存在颜色的改变,所以清空重新计算
if(t[rt].l==t[rt].r-1) {t[rt].len[sta]=t[rt].ylen;return; }//叶子节点
//通过子节点更新,因为sta表示的是当前区间直接覆盖的信息,只需要用下面的信息更新当前信息即可,不需要将此处节点的信息push到子节点
for (int i = 0; i < 8; ++i) t[rt].len[sta|i]+=t[rt<<1].len[i]+t[rt<<1|1].len[i];
}
void build(int rt,int l,int r)
{
t[rt].l=l,t[rt].r=r,t[rt].ylen=ubw[r]-ubw[l],t[rt].yl=ubw[l],t[rt].yr=ubw[r];
for (int i = 0; i < 10; ++i){t[rt].color[i]=0,t[rt].len[i]=0;}
t[rt].len[0]=t[rt].ylen;//此处很关键,0表示没有颜色
if(l+1==r)return;
int mid=(l+r)>>1;
build(rt<<1,l,mid);build(rt<<1|1,mid,r);
}
void update(int rt,int l,int r,int color,int flag)
{
if(t[rt].yl>=r||t[rt].yr<=l)return;
if(t[rt].yl>=l&&t[rt].yr<=r)
{
if(flag>0)t[rt].color[color]++;
else t[rt].color[color]--;
pushup(rt);
}
else
{
update(rt<<1,l,r,color,flag);
update(rt<<1|1,l,r,color,flag);
pushup(rt);
}
}
int main()
{
int T;cin>>T;int KASE=1;tc['R']=1;tc['G']=2;tc['B']=3;
while(T--)
{
long long ans[10];
memset(ans,0, sizeof(ans));
int x1,x2,y1,y2;
int n;char ch;
cin>>n;
for (int i = 1; i <= n; ++i) {
cin>>ch;
cin>>x1>>y1>>x2>>y2;
ubw[i*2-1]=y1;ubw[i*2]=y2;//离散化
l[i*2-1].x=x1;l[i*2-1].l=y1;l[i*2-1].r=y2;l[i*2-1].flag=1;l[i*2-1].color=tc[ch];
l[i*2].x=x2;l[i*2].l=y1;l[i*2].r=y2;l[i*2].flag=-1;l[i*2].color=tc[ch];
}
sort(ubw+1,ubw+1+2*n);sort(l+1,l+1+2*n);
build(1,1,2*n);
for (int i = 1; i <= 2*n; ++i) {
for (int j = 1; j < 8; ++j) {
ans[j]+=(long long)(l[i].x-l[i-1].x)*(long long)t[1].len[j];
}
update(1,l[i].l,l[i].r,l[i].color,l[i].flag);
}
printf("Case %d:\n",KASE++);
printf("%lld\n%lld\n%lld\n%lld\n%lld\n%lld\n%lld\n",ans[1],ans[2],ans[4],ans[3],ans[5],ans[6],ans[7]);
}
}