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:\n",cas);
121         cas++;
122         printf("%I64d\n",s[7]-s[6]);
123         printf("%I64d\n",s[7]-s[5]);
124         printf("%I64d\n",s[7]-s[3]);
125         printf("%I64d\n",s[5]+s[6]-s[4]-s[7]);
126         printf("%I64d\n",s[3]+s[6]-s[2]-s[7]);
127         printf("%I64d\n",s[3]+s[5]-s[1]-s[7]);
128         printf("%I64d\n",s[1]+s[2]+s[4]-s[3]-s[5]-s[6]+s[7]);
129     }
130     return 0;
131 }

hdu 4419 Colourful Rectangle

时间: 2024-10-19 22:40:31

hdu 4419 Colourful Rectangle的相关文章

[HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题就是维护每个颜色的长度,写起来很蛋疼. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6

HDU 4419 Colourful Rectangle --离散化+线段树扫描线

题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何维护每种颜色的长度着实让我伤透了脑筋.后来看了一位朋友的题解,才幡然醒悟. 开始想到了用二进制表示颜色,R用001表示,G用010表示,B用100表示.那么就可以用十进制1~7表示7种不同颜色了. 维护 cov[rt][1~3] 表示此区间内3种原色各有多少个, Len[rt][i]表示每种颜色的长

Hdu 4419 Colourful Rectangle(线段树扫描线)

题目大意: 给出多个不同颜色的矩形,求最后覆盖的颜色的面积. 思路分析: 我是自己手动暴力枚举. 比赛的时候漏了一种情况. RGB 可以从 RG+RB组合来(只是举例,就是说可以从两种颜色组合而来). 然后就只需要维护所有的颜色 用扫描线来判断. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define MAXN 42222 using name

HDU 4419 Colourful Rectangle 扫描线

题目链接:点击打开链接 题意:给定由红绿蓝组成的一些矩阵 输出每种颜色对应的面积. 思路: 如果颜色只有一种就是扫描线. 这里先求出包含各类颜色的面积,然后容斥一下得到答案. 画个图就能快速得到答案了 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #include <set> #inclu

HDU 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

1506题意:给你连续的直方图(底边边长为1),求连续的矩阵面积. 对每个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <sta

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

hdu 1506 Largest Rectangle in a Histogram 构造

题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rec

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of