Question:
Calculates the area of the rectangle and each rectangle contains a small rectangle that is hollowed out.
Ideas:
The scanning line of the classic line segment tree has not been written for three hours... Really despise yourself !!
If you have learned the scanning line, you will have some ideas. Here is a wrong idea... (My ...)
If you assign a line weight in this way, it is a big mistake because the structure of the Line Segment tree makes operations very troublesome.
When you want to update a certain range, you do not know where to accurately go down or how to merge the ranges after update.
Of course, it is necessary to update to the leaf node at the beginning like me... Tat
The correct idea is as follows:
After Dividing four rectangles, why is that right? Because you do not need to go down
Consider that if the side of each rectangle + 1 comes, it will be enough to count. There will always be a-1 side here.
If you ask the first method, can you wait for-1? The answer is no.
The "can wait" nature needs to ensure that the small rectangle currently being considered will not be empty, and the first one will obviously be empty.
Code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define LL __int64#define N 200005#define M(x,y) ( (x+y)>>1 )#define L(x) ( x<<1 )#define R(x) ( (x<<1)|1 )int n,tot;struct line{int x,y1,y2;int flag;bool operator<(const line fa) const{if(x!=fa.x) return x<fa.x;return flag>fa.flag;}}l[N*2];struct node{int l,r,cov,sum;}tree[N*8];LL ans;void init(int l,int r,int i){tree[i].l=l;tree[i].r=r;tree[i].cov=0;tree[i].sum=0;if(l+1==r) return ;int mid=M(l,r);init(l,mid,L(i));init(mid,r,R(i));}void up(int i){ if(tree[i].cov>0) tree[i].sum=tree[i].r-tree[i].l; else { if(tree[i].l+1==tree[i].r) tree[i].sum=0; else tree[i].sum=tree[L(i)].sum+tree[R(i)].sum; }}void updata(int y1,int y2,int i,int flag){ if(tree[i].l==y1&&tree[i].r==y2) { tree[i].cov+=flag; up(i); return ; } int mid=M(tree[i].l,tree[i].r);if(y2<=mid) updata(y1,y2,L(i),flag); else if(y1>=mid) updata(y1,y2,R(i),flag); else { updata(y1,mid,L(i),flag); updata(mid,y2,R(i),flag); } up(i);}int main(){int i;int tx1,ty1,tx2,ty2,tx3,ty3,tx4,ty4;while(~scanf("%d",&n)){if(!n) break;tot=0;for(i=0;i<n;i++){scanf("%d%d%d%d%d%d%d%d",&tx1,&ty1,&tx2,&ty2,&tx3,&ty3,&tx4,&ty4);if(ty1!=ty3) { l[tot].x=tx1; l[tot].y1=ty1; l[tot].y2=ty3; l[tot].flag=1; tot++; l[tot].x=tx2; l[tot].y1=ty1; l[tot].y2=ty3; l[tot].flag=-1; tot++; }//-------------------------------- if(ty4!=ty2) { l[tot].x=tx1; l[tot].y1=ty4; l[tot].y2=ty2; l[tot].flag=1; tot++; l[tot].x=tx2; l[tot].y1=ty4; l[tot].y2=ty2; l[tot].flag=-1; tot++; }//--------------------------------if(ty3!=ty4) { l[tot].x=tx1; l[tot].y1=ty3; l[tot].y2=ty4; l[tot].flag=1; tot++; l[tot].x=tx3; l[tot].y1=ty3; l[tot].y2=ty4; l[tot].flag=-1; tot++; }//--------------------------------if(ty3!=ty4) { l[tot].x=tx4; l[tot].y1=ty3; l[tot].y2=ty4; l[tot].flag=1; tot++; l[tot].x=tx2; l[tot].y1=ty3; l[tot].y2=ty4; l[tot].flag=-1; tot++; }}sort(l,l+tot);init(0,50000,1);for(i=0,ans=0;i<tot;i++) { if(i) ans+=(LL)(l[i].x-l[i-1].x)*tree[1].sum; updata(l[i].y1,l[i].y2,1,l[i].flag); }printf("%I64d\n",ans);}return 0;}
HDU 3265 posters