HDU1542线段树+扫描线

偏执的太偏执、 2021-12-12 14:47 397阅读 0赞

































Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts

Problem Archive
Realtime Judge Status
Authors Ranklist
 

     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China

Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Author xizighq
Mail Mail 0(0)
Control Panel Control Panel 
Sign Out Sign Out

如何参加“HDOJ暑期多校联合训练”?请加QQ群837241962咨询~

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22353    Accepted Submission(s): 8887


 

Problem Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

 

 

Input

The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.

 

 

Output

For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.

 

 

Sample Input


  1.  

2 10 10 20 20 15 15 25 25.5 0

 

 

Sample Output


  1.  

Test case #1 Total explored area: 180.00

 

题意:给出一些矩形的左下坐标和右上坐标,求这些矩形的总面积。

多组测试用例。

使用线段树+扫描线 :

代码如下:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. double b[1000],ans;
  5. struct point{
  6. double l,r,h;
  7. int f;
  8. bool operator<(const point& p )const{
  9. return h<p.h;
  10. }
  11. }e[2000];
  12. struct Node{
  13. int l,r,tag;
  14. double len;
  15. }t[2000];
  16. void build(int k,int l,int r){
  17. t[k].l=l;t[k].r=r;
  18. t[k].len=0;t[k].tag=0;
  19. if(l==r)return ;
  20. int mid=(l+r)/2;
  21. build(k*2,l,mid);
  22. build(k*2+1,mid+1,r);//建立线段树
  23. }
  24. void pushup(int k){
  25. if(t[k].tag) t[k].len=b[t[k].r+1]-b[t[k].l];
  26. else if(t[k].l==t[k].r)t[k].len=0;
  27. else t[k].len=t[k*2].len+t[k*2+1].len;
  28. }
  29. void update(int k,int l,int r,int w){
  30. // cout<<k<<":"<<l<<" "<<r<<" "<<t[k].l<<" "<<t[k].r<<" "<<w<<endl;
  31. if(l<=t[k].l&&t[k].r<=r){
  32. t[k].tag+=w;
  33. pushup(k);
  34. return ;
  35. }
  36. int mid=(t[k].l+t[k].r)/2;
  37. if(l<=mid)update(k*2,l,r,w);
  38. if(mid<r)update(k*2+1,l,r,w);
  39. pushup(k);
  40. }
  41. int main(){
  42. int tot,cas;
  43. while(scanf("%d",&n) != EOF && n){
  44. int tot=1,k=2;
  45. ans=0;
  46. double x1,x2,y1,y2;
  47. for(int i=1;i<=n;i++){
  48. scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
  49. e[tot].l=e[tot+1].l=x1;
  50. e[tot].r=e[tot+1].r=x2;
  51. e[tot].h=y1;
  52. e[tot+1].h=y2;
  53. e[tot].f=1;
  54. e[tot+1].f=-1;
  55. b[tot]=x1;
  56. b[tot+1]=x2;
  57. tot+=2;
  58. }
  59. sort(e+1,e+tot);
  60. sort(b+1,b+tot);
  61. // for(int i=1;i<tot;i++)cout<<e[i].l<<" "<<e[i].r<<" "<<e[i].h<<" "<<e[i].f<<" "<<endl;
  62. for(int i=2;i<tot;i++)
  63. if(b[i]!=b[i-1])
  64. b[k++]=b[i];
  65. // for(int i=1;i<k;i++)cout<<b[i]<<" ";cout<<endl;
  66. build(1,1,k);
  67. for(int i=1;i<tot;i++){
  68. int l=(int)(lower_bound(b+1,b+k,e[i].l)-b),r=(int)(lower_bound(b+1,b+k,e[i].r)-b-1);
  69. // cout<<i<<": "<<e[i].l<<" "<<e[i].r<<" "<<l<<" "<<r<<endl;
  70. update(1,l,r,e[i].f);
  71. // cout<<i<<":"<<t[1].len<<" "<<e[i].h<<" "<<e[i+1].h<<" "<<e[i].f<<endl;
  72. ans+=(e[i+1].h-e[i].h)*t[1].len;
  73. // for(int i=1;i<=10;i++)cout<<t[i].l<<" "<<t[i].r<<" "<<t[i].tag<<" "<<t[i].len<<" "<<endl;
  74. }
  75. printf("Test case #%d\n",++cas);
  76. printf("Total explored area: %.2lf\n\n",ans);
  77. }
  78. return 0;
  79. }

发表评论

表情:
评论列表 (有 0 条评论,397人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1754(线段入门)

    问题描述: 很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。  这让很多学生很反感。  不管你喜不喜欢,现在需要你做的是,就是