题目链接: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 using namespace std; 7 typedef pair<int,int> PII; 8 typedef long long LL; 9 10 struct Node{ 11 int l,r,h,d,c; 12 bool operator<(const Node& a) const{ 13 return h<a.h; 14 } 15 Node(int L=0,int R=0,int H=0,int D=0,int C=0):l(L),r(R),h(H),d(D),c(C){} 16 }; 17 const int MAX_N = 100010; 18 int T,n; 19 Node t[MAX_N<<1]; 20 int dsum[MAX_N<<2][8],b[MAX_N<<1]; 21 LL sum[8]; 22 int scol[MAX_N<<2],len[MAX_N<<2][8]; 23 24 void push_up(int idx,int l,int r){ 25 int state = (dsum[idx][1]>0?1:0)|(dsum[idx][2]>0?2:0)|(dsum[idx][4]>0?4:0); 26 if( state ) { 27 memset(len[idx],0,sizeof(len[idx])); 28 len[idx][state] = b[r+1] - b[l]; 29 for(int i=1;i<8;i++){ 30 if( state!=(state|i) ){ 31 int tmp = len[idx<<1][i]+len[idx<<1|1][i]; 32 len[idx][state|i] += tmp; 33 len[idx][state] -= tmp; 34 } 35 } 36 } else if(l!=r){ 37 for(int i=1;i<8;i++){ 38 len[idx][i] = len[idx<<1][i] + len[idx<<1|1][i]; 39 } 40 } else { 41 memset(len[idx],0,sizeof(len[idx])); 42 } 43 } 44 45 void update(int L,int R,int x,int c,int idx,int l,int r){ 46 if( L<=l&&R>=r ){ 47 dsum[idx][c] += x; 48 } else { 49 int m = l+r>>1; 50 if( L<=m ) update(L,R,x,c,idx<<1,l,m); 51 if( R>m ) update(L,R,x,c,idx<<1|1,m+1,r); 52 } 53 push_up(idx,l,r); 54 } 55 56 int main(){ 57 scanf("%d",&T); 58 int kase = 1; 59 while(T--){ 60 memset(dsum,0,sizeof(dsum)); 61 memset(len,0,sizeof(len)); 62 memset(scol,0,sizeof(scol)); 63 scanf("%d",&n); 64 char col[2]; 65 int x1,y1,x2,y2; 66 int ptr = 0 , ptrb = 0; 67 for(int i=0;i<n;i++){ 68 scanf("%s%d%d%d%d",col,&x1,&y1,&x2,&y2); 69 int cc; 70 if( col[0]==‘R‘ ) cc=1; 71 else if( col[0]==‘G‘ ) cc = 2; 72 else if( col[0]==‘B‘ ) cc = 4; 73 t[ptr++] = Node(x1,x2,y1,1,cc); 74 t[ptr++] = Node(x1,x2,y2,-1,cc); 75 b[ptrb++] = x1; 76 b[ptrb++] = x2; 77 } 78 sort(t,t+ptr); 79 sort(b,b+ptrb); 80 int ub = unique(b,b+ptrb) - b; 81 memset(sum,0,sizeof(sum)); 82 83 for(int i=0;i<ptr-1;i++){ 84 int l = lower_bound(b,b+ub,t[i].l) - b; 85 int r = lower_bound(b,b+ub,t[i].r) - b - 1; 86 update(l,r,t[i].d,t[i].c,1,0,ub-1); 87 if( t[i].h!=t[i+1].h ){ 88 for(int j=1;j<8;j++){ 89 sum[j] += (LL)(t[i+1].h-t[i].h)*(LL)(len[1][j]); 90 } 91 } 92 } 93 printf("Case %d:\n",kase++); 94 printf("%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n",sum[1],sum[2],sum[4],sum[3],sum[5],sum[6],sum[7]); 95 } 96 return 0; 97 }
[HDU 4419] Colourful Rectangle (扫描线 矩形面积并)
原文:http://www.cnblogs.com/llkpersonal/p/4082470.html