HDU-4419 Colourful Rectangle 矩形多面积并

时间:2021-02-09 16:05:19

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419

  利用二进制,R为1、G为2、B为4,然后通过异或运算可以得到其它组合颜色。建立7颗线段树,每颗线段树保存每种颜色的长度。。。

 //STATUS:C++_AC_203MS_4780KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Seg{
int y,x1,x2;
int c,col;
Seg(){}
Seg(int a,int b,int c,int d,int color):y(a),x1(b),x2(c),c(d),col(color){}
bool operator < (const Seg& a)const{
return y<a.y;
}
}seg[N];
int hs[N],len[N<<][];
int cnt[N<<][];
LL ans[];
int T,n,m; void pushup(int l,int r,int rt)
{
int i,col=((cnt[rt][]?:) | (cnt[rt][]?:) | (cnt[rt][]?:));
if(col){
int ls=rt<<,rs=rt<<|;
mem(len[rt],);
len[rt][col]=hs[r+]-hs[l];
i=;
for(i=;i<=;i++){
if(col==(col|i))continue;
int t=len[ls][i]+len[rs][i];
len[rt][col|i]+=t;
len[rt][col]-=t;
}
}
else if(l==r)mem(len[rt],);
else {
int ls=rt<<,rs=rt<<|;
for(i=;i<=;i++){
len[rt][i]=len[ls][i]+len[rs][i];
}
}
} void update(int a,int b,int c,int col,int l,int r,int rt)
{
if(a<=l && r<=b){
cnt[rt][col]+=c;
pushup(l,r,rt);
return;
}
int mid=(l+r)>>;
if(a<=mid)update(a,b,c,col,lson);
if(b>mid)update(a,b,c,col,rson);
pushup(l,r,rt);
} int binary(int l,int r,int tar)
{
int mid;
while(l<r){
mid=(l+r)>>;
if(hs[mid]==tar)return mid;
else if(hs[mid]>tar)r=mid;
else l=mid+;
}
return -;
} int main()
{
// freopen("in.txt","r",stdin);
int key[];
key['R']=,key['G']=,key['B']=;
int i,j,k,l,r,ca=,a,b,c,d;
char s[];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
m=;
for(i=;i<n;i++){
scanf("%s%d%d%d%d",s,&a,&b,&c,&d);
hs[m]=a;
seg[m++]=Seg(b,a,c,,key[s[]]);
hs[m]=c;
seg[m++]=Seg(d,a,c,-,key[s[]]);
}
sort(hs,hs+m);
sort(seg,seg+m);
for(i=,k=;i<m;i++)
if(hs[i]!=hs[k])hs[++k]=hs[i];
mem(len,);mem(cnt,);mem(ans,);
for(i=;i<m-;i++){
l=binary(,k+,seg[i].x1);
r=binary(,k+,seg[i].x2)-;
if(l<=r)update(l,r,seg[i].c,seg[i].col,,k,);
for(j=;j<=;j++){
ans[j]+=(LL)len[][j]*(LL)(seg[i+].y-seg[i].y);
}
} printf("Case %d:\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n",
ca++,ans[],ans[],ans[],ans[],ans[],ans[],ans[]);
}
return ;
}