分别求出7种颜色覆盖的面积。
做法:每种颜色设定一个标号,以二进制表示R:100 G:010 B:001 。这样很明显可以知道RG:110 GB:011 以此类推。
求解时,需要开一个二维标记数组,标记了这一段的某种颜色被标记了几次,然后类似状压的方式求解。
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 #include<map> 11 using namespace std; 12 #define N 41010 13 #define LL __int64 14 #define INF 0xfffffff 15 const double eps = 1e-8; 16 const double pi = acos(-1.0); 17 const double inf = ~0u>>2; 18 map<int,int>f; 19 int s[N<<2][8]; 20 int fs[N<<2],a[N],ff[N<<2][8]; 21 int val[N]; 22 int id[500]; 23 LL num[N]; 24 struct node 25 { 26 int x1,x2,y; 27 int f; 28 node(){} 29 node(int x1,int x2,int y,int f):x1(x1),x2(x2),y(y),f(f){} 30 bool operator <(const node &S)const 31 { 32 return y<S.y; 33 } 34 }p[N]; 35 void up(int w,int l,int r) 36 { 37 int i; 38 if(fs[w]==0) 39 { 40 for(i = 1; i <= 7 ; i++) 41 { 42 if(l==r) 43 s[w][i] = 0; 44 else 45 s[w][i] = s[w<<1][i]+s[w<<1|1][i]; 46 } 47 } 48 else 49 { 50 int res = val[r+1]-val[l]; 51 int cnt[8] = {0}; 52 for(i = 1; i <= 7; i++) 53 { 54 if(l==r) 55 { 56 if(i==fs[w]) 57 cnt[i] += val[r+1]-val[l]; 58 else 59 cnt[i] += 0; 60 } 61 else 62 { 63 cnt[i|fs[w]] += s[w<<1][i]+s[w<<1|1][i]; 64 } 65 } 66 for(i = 1; i <= 7; i++) 67 res-=cnt[i]; 68 cnt[fs[w]]+=res; 69 for(i = 1; i <= 7 ; i++) 70 s[w][i] = cnt[i]; 71 } 72 } 73 void build(int l,int r,int w) 74 { 75 int i; 76 for(i = 1;i<= 7; i++) 77 {s[w][i] = ff[w][i] = 0;} 78 fs[w] = 0; 79 if(l==r) 80 { 81 return ; 82 } 83 int m = (l+r)>>1; 84 build(l,m,w<<1); 85 build(m+1,r,w<<1|1); 86 } 87 void update(int a,int b,int d,int l,int r,int w) 88 { 89 if(a<=l&&b>=r) 90 { 91 ff[w][abs(d)]+=d/abs(d); 92 if(d>0) 93 fs[w]|=d; 94 else if(ff[w][-d]==0) fs[w]+=d; 95 up(w,l,r); 96 return ; 97 } 98 int m = (l+r)>>1; 99 if(a<=m) update(a,b,d,l,m,w<<1); 100 if(b>m) update(a,b,d,m+1,r,w<<1|1); 101 up(w,l,r); 102 } 103 int main() 104 { 105 id['R'] = 4; 106 id['G'] = 2; 107 id['B'] = 1; 108 int i,j,t,n,cas = 1; 109 char sr[10]; 110 cin>>t; 111 while(t--) 112 { 113 f.clear(); 114 scanf("%d",&n); 115 memset(num,0,sizeof(num)); 116 int g= 0 ,dd[8]; 117 for(i = 1; i <= n; i++) 118 { 119 int x1,x2,y1,y2,k; 120 scanf("%s%d%d%d%d",sr,&x1,&y1,&x2,&y2); 121 if(sr[0]=='R') k = id['R']; 122 else if(sr[0]=='G') k = id['G']; 123 else k = id['B']; 124 p[++g] = node(x1,x2,y1,k); 125 a[g] = x1; 126 p[++g] = node(x1,x2,y2,-k); 127 a[g] = x2; 128 } 129 sort(a+1,a+g+1); 130 sort(p+1,p+g+1); 131 int o = 0; 132 f[a[1]] = ++o; 133 val[o] = a[1]; 134 for(i = 2; i <= g ; i++) 135 if(a[i]!=a[i-1]) 136 { 137 f[a[i]] = ++o; 138 val[o] = a[i]; 139 } 140 build(1,o-1,1); 141 142 for(i = 1; i < g; i++) 143 { 144 int l = f[p[i].x1]; 145 int r = f[p[i].x2]-1; //cout<<l<<" "<<r<<endl; 146 if(l<=r) 147 { 148 update(l,r,p[i].f,1,o-1,1); 149 } 150 for(j = 1; j <= 7; j++) 151 num[j] += (LL)(p[i+1].y-p[i].y)*s[1][j]; 152 } 153 dd[1] = 4,dd[2] = 2,dd[3] = 1,dd[4] = 6,dd[5] = 5,dd[6] = 3,dd[7] = 7; 154 printf("Case %d: ",cas++); 155 for(i = 1; i <= 7; i++) 156 printf("%I64d ",num[dd[i]]); 157 } 158 return 0; 159 }