HDOJ 题目3265 Posters(线段树+扫描线)

来源:互联网 发布:网页传奇游戏源码 编辑:程序博客网 时间:2024/05/24 07:32

Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5660    Accepted Submission(s): 1327


Problem Description
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 

However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 

Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.

To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 
 

Input
The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.

The input ends with a line of single zero.
 

Output
For each test case, output a single line with the total area of window covered by posters.
 

Sample Input
20 0 10 10 1 1 9 92 2 8 8 3 3 7 70
 

Sample Output
56
 

Source
2009 Asia Ningbo Regional Contest Hosted by NIT
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3268 3264 3262 3269 3263 
 
题目大意:给个n,下边n个海报,每个海报都有一个矩形的洞,n行分别输入,海报的左下角,右上角,然后求矩形面积并。。。
ac代码
#include<stdio.h>   #include<stdlib.h>   #include<string.h>   #include<algorithm>   using namespace std;  struct s  {      int x1,x2,y;      int flag;  }a[50880<<3];  int __hash[50805<<5];__int64 sum[50805<<5];int k;  int col[50805<<5];  int cmp(s a,s b)  {      return a.y<b.y;  }  void Add(int x1,int y1,int x2,int y2){    if(x1==x2||y1==y2)        return;    a[k].x1=x1;      a[k].x2=x2;      a[k].y=y1;      a[k].flag=1;      __hash[k++]=x1;      a[k].x1=x1;      a[k].x2=x2;      a[k].y=y2;      a[k].flag=-1;      __hash[k++]=x2;  }int bseach(double key,int n)  {      int l=1,r=n;      while(l<=r)      {          int mid=(l+r)>>1;          if(__hash[mid]==key)              return mid;          if(__hash[mid]<key)              l=mid+1;          else              r=mid-1;      }      return l;  }  void pushup(int tr,int l,int r)  {      if(col[tr])          sum[tr]=__hash[r+1]-__hash[l];      else          if(l==r)              sum[tr]=0;          else              sum[tr]=sum[tr<<1]+sum[tr<<1|1];  }  void update(int L,int R,int l,int r,int tr,int flag)  {      if(L<=l&&r<=R)      {          col[tr]+=flag;          pushup(tr,l,r);          return;      }      int mid=(l+r)>>1;      if(L<=mid)          update(L,R,l,mid,tr<<1,flag);      if(R>mid)          update(L,R,mid+1,r,tr<<1|1,flag);      pushup(tr,l,r);  }  int main()  {      int n,c=0;      while(scanf("%d",&n)!=EOF)      {          if(n==0)              break;          memset(col,0,sizeof(col));          memset(sum,0,sizeof(sum));          int x1,x2,y1,y2,x3,y3,x4,y4;          int i;        k=1;          for(i=1;i<=n;i++)          {              scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);  Add(x1,y2,x3,y1);            Add(x3,y3,x4,y1);            Add(x3,y2,x4,y4);            Add(x4,y2,x2,y1);/*Add(x1,y1,x2,y3);Add(x1,y4,x2,y2);Add(x1,y3,x3,y4);Add(x4,y3,x2,y4);*/        }          sort(a+1,a+k,cmp);       //    int cnt=unique(__hash+1,__hash+k)-(__hash+1);          sort(__hash+1,__hash+k);          __int64 ans=0;          for(i=1;i<k-1;i++)          {              int x,y;        //    if(a[i].x1>=a[i].x2)          //      continue;            x=bseach(a[i].x1,k-1);              y=bseach(a[i].x2,k-1)-1;              if(x<=y)                  update(x,y,1,k-1,1,a[i].flag);              ans+=sum[1]*(a[i+1].y-a[i].y);          }          printf("%I64d\n",ans);      }  }  


0 0
原创粉丝点击