题目链接:
题意:给出平面上一些矩形,这些矩形的颜色有三种R、G、B。相交的区域分别标记为 RG, RB, GB, RGB。输出颜色为R, G, B, RG, RB, GB, RGB的面积各为多少?
思路:设上面7种颜色的面积分别为1,2,3,4,5,6,7:
a=1+2+3+4+5+6+7(把所有的矩形求一次面积交)
b=1+2+4+5+6+7(把R和G的矩形求一次)
c=2+3+4=5+6+7(把G和B的矩形求一次)
d=1+3+4+5+6+7(把R和B的矩形求一次)
e=1+4+5+7(把R的矩形求一次)
f=2+4+6+7(把G的矩形求一次)
g=3+5+6+7(把B的矩形求一次)
由以上七个式子可以得到:
1=a-c;
2=a-d;3=a-b;6=a-2-3-e;5=a-1-3-f;4=a-1-2-g;7=a-1-2-3-4-5-6;struct NODE { char color[5]; __int64 x1,y1,x2,y2; }; struct Node { __int64 L,R; __int64 Len; //当前,本区间上有多长的部分是落在那些矩形中的 __int64 cover; //本区间当前被多少个矩形完全包含 }; struct node { __int64 x,y1,y2; bool bLeft; node(){} node(__int64 _x,__int64 _y1,__int64 _y2,bool _bLeft) { x=_x; y1=_y1; y2=_y2; bLeft=_bLeft; } }; const int MAX=10005; NODE p[MAX]; node L[MAX*2]; Node a[MAX*10]; __int64 y[MAX*2]; int n; bool cmp(node a,node b) { return a.x0?1:-1; } int find(__int64 y[],int n,__int64 val) { int low=0,high=n-1,mid; while(low<=high) { mid=(low+high)>>1; if(DB(val-y[mid])==0) return mid; if(DB(val-y[mid])==1) low=mid+1; else high=mid-1; } if(DB(val-y[0])==0) return 0; return n-1; } void insert(int t,int L,int R) { if(a[t].L==L&&a[t].R==R) { a[t].Len=y[R+1]-y[L]; a[t].cover++; return; } int mid=(a[t].L+a[t].R)>>1; if(R<=mid) insert(t*2,L,R); else if(L>mid) insert(t*2+1,L,R); else { insert(t*2,L,mid); insert(t*2+1,mid+1,R); } if(a[t].cover==0) a[t].Len=a[t*2].Len+a[t*2+1].Len; } void del(int t,int L,int R) { if(a[t].L==L&&a[t].R==R) { a[t].cover--; if(a[t].cover==0) { if(a[t].L==a[t].R) a[t].Len=0; else a[t].Len=a[t*2].Len+a[t*2+1].Len; } return; } int mid=(a[t].L+a[t].R)>>1; if(R<=mid) del(t*2,L,R); else if(L>mid) del(t*2+1,L,R); else { del(t*2,L,mid); del(t*2+1,mid+1,R); } if(a[t].cover==0) a[t].Len=a[t*2].Len+a[t*2+1].Len; } void build(int t,int L,int R) { a[t].L=L; a[t].R=R; a[t].cover=0; a[t].Len=0; if(L==R) return; int mid=(L+R)>>1; build(t*2,L,mid); build(t*2+1,mid+1,R); } __int64 cal_cross_area(NODE p[],int n) { if(!n) return 0; int i,k; __int64 x1,y1,x2,y2; for(i=0;i