hdu 4419 Colourful Rectangle

http://acm.hdu.edu.cn/showproblem.php?pid=4419

题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积。

转化为二进制表示颜色:001 R ,010G,100B,011RG,101RB,....111RGB;

在结构体里面加上一个len[8]和cover[8]表示每种颜色所占的长度和在区间的覆盖次数。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #define maxn 100010
  5 #define ll __int64
  6 using namespace std;
  7 
  8 int t,n;
  9 char ch;
 10 ll Y[maxn];
 11 struct node1
 12 {
 13     ll x,y1,y2;
 14     int lr,c;
 15     bool operator <(const node1 &a)const
 16     {
 17         return (x<a.x)||(x==a.x&&lr>a.lr);
 18     }
 19 }p[maxn];
 20 struct node
 21 {
 22     int l,r;
 23     ll len[8];
 24     int cover[8];
 25 } tree[maxn*4];
 26 
 27 void build(int i,int l,int r)
 28 {
 29     tree[i].l=l;
 30     tree[i].r=r;
 31     for(int j=1; j<8; j++)
 32     {
 33         tree[i].cover[j]=tree[i].len[j]=0;
 34     }
 35     if(l==r-1) return;
 36     int mid=(l+r)>>1;
 37     build(i<<1,l,mid);
 38     build(i<<1|1,mid,r);
 39 }
 40 
 41 void update(int i,int l,int r,int lr,int c)
 42 {
 43     if(tree[i].l==l&&tree[i].r==r)
 44     {
 45         tree[i].cover[c]+=lr;
 46         if(tree[i].cover[c])
 47             tree[i].len[c]=Y[tree[i].r]-Y[tree[i].l];
 48         else if(tree[i].r-1==tree[i].l)
 49             tree[i].len[c]=0;
 50         else
 51             tree[i].len[c]=tree[i<<1].len[c]+tree[i<<1|1].len[c];
 52         return ;
 53     }
 54     int mid=(tree[i].r+tree[i].l)>>1;
 55     if(r<=mid)
 56     {
 57         update(i<<1,l,r,lr,c);
 58     }
 59     else if(l>=mid)
 60     {
 61         update(i<<1|1,l,r,lr,c);
 62     }
 63     else
 64     {
 65         update(i<<1,l,mid,lr,c);
 66         update(i<<1|1,mid,r,lr,c);
 67     }
 68     if(tree[i].cover[c])
 69         tree[i].len[c]=Y[tree[i].r]-Y[tree[i].l];
 70     else if(tree[i].r-1==tree[i].l)
 71         tree[i].len[c]=0;
 72     else
 73         tree[i].len[c]=tree[i<<1].len[c]+tree[i<<1|1].len[c];
 74 }
 75 
 76 int main()
 77 {
 78     scanf("%d",&t);
 79     int cas=1;
 80     while(t--)
 81     {
 82         scanf("%d",&n);
 83         getchar();
 84         int t1=0;
 85         for(int i=1; i<=n; i++)
 86         {
 87             ll x1,y1,x2,y2;
 88             scanf("%c %I64d%I64d%I64d%I64d",&ch,&x1,&y1,&x2,&y2);
 89             if(ch=='R')
 90             {
 91                 p[t1].c=1; p[t1+1].c=1;
 92             }
 93             else if(ch=='G')
 94             {
 95                 p[t1].c=2; p[t1+1].c=2;
 96             }
 97             else if(ch=='B')
 98             {
 99                 p[t1].c=4; p[t1+1].c=4;
100             }
101             p[t1].x=x1; p[t1].y1=y1; p[t1].y2=y2;p[t1].lr=1;Y[t1++]=y1;
102             p[t1].x=x2; p[t1].y1=y1; p[t1].y2=y2;p[t1].lr=-1;Y[t1++]=y2;
103             getchar();
104         }
105         sort(Y,Y+t1);
106         int cnt=unique(Y,Y+t1)-Y;
107         sort(p,p+t1);
108         build(1,0,cnt-1);
109         ll s[maxn]={0};
110         for(int i=0; i<t1; i++)
111         {
112             int l1=lower_bound(Y,Y+cnt,p[i].y1)-Y;
113             int rr=lower_bound(Y,Y+cnt,p[i].y2)-Y;
114             for(int j=1; j<8; j++)
115             {
116                 if(p[i].c&j) update(1,l1,rr,p[i].lr,j);
117                 if(i+1<t1) s[j]+=tree[1].len[j]*(p[i+1].x-p[i].x);
118             }
119         }
120         printf("Case %d:
",cas);
121         cas++;
122         printf("%I64d
",s[7]-s[6]);
123         printf("%I64d
",s[7]-s[5]);
124         printf("%I64d
",s[7]-s[3]);
125         printf("%I64d
",s[5]+s[6]-s[4]-s[7]);
126         printf("%I64d
",s[3]+s[6]-s[2]-s[7]);
127         printf("%I64d
",s[3]+s[5]-s[1]-s[7]);
128         printf("%I64d
",s[1]+s[2]+s[4]-s[3]-s[5]-s[6]+s[7]);
129     }
130     return 0;
131 }
View Code