标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5230 Accepted Submission(s): 1220
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 50005 12 #define ll root<<1 13 #define rr root<<1|1 14 #define mid (a[root].l+a[root].r)/2 15 16 int max(int x,int y){return x>y?x:y;} 17 int min(int x,int y){return x<y?x:y;} 18 int abs(int x,int y){return x<0?-x:x;} 19 20 struct node{ 21 int l, r; 22 int val; 23 __int64 sum; 24 }a[N*16]; 25 26 struct Line{ 27 int x1, x2, y; 28 int val; 29 Line(){} 30 Line(int a,int b,int c,int d){ 31 x1=a; 32 x2=b; 33 y=c; 34 val=d; 35 } 36 }line[N*8]; 37 38 bool cmp(Line a,Line b){ 39 return a.y<b.y; 40 } 41 42 int xx[N*4]; 43 int n, m; 44 45 int b_s(int key){ 46 int l=1, r=m; 47 while(l<=r){ 48 int mm=(l+r)/2; 49 if(xx[mm]==key) return mm; 50 else if(xx[mm]<key) l=mm+1; 51 else if(xx[mm]>key) r=mm-1; 52 } 53 } 54 55 void build(int l,int r,int root){ 56 a[root].l=l; 57 a[root].r=r; 58 a[root].sum=a[root].val=0; 59 if(l==r) return; 60 build(l,mid,ll); 61 build(mid+1,r,rr); 62 } 63 64 void up(int root){ 65 if(a[root].val) a[root].sum=xx[a[root].r+1]-xx[a[root].l]; 66 else if(a[root].l==a[root].r) a[root].sum=0; 67 else a[root].sum=a[ll].sum+a[rr].sum; 68 } 69 70 void update(int l,int r,int val,int root){ 71 if(l>r) return; 72 if(a[root].l==l&&a[root].r==r){ 73 a[root].val+=val; 74 up(root); 75 return; 76 } 77 if(r<=a[ll].r) update(l,r,val,ll); 78 else if(l>=a[rr].l) update(l,r,val,rr); 79 else{ 80 update(l,mid,val,ll); 81 update(mid+1,r,val,rr); 82 } 83 up(root); 84 } 85 main() 86 { 87 int x1, y1, x2, y2, x3, y3, x4, y4; 88 int i, j, k; 89 while(scanf("%d",&n)&&n){ 90 k=0; 91 m=1; 92 for(i=0;i<n;i++){ 93 scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); 94 line[k++]=Line(x1,x3,y1,1); 95 line[k++]=Line(x1,x3,y2,-1); 96 line[k++]=Line(x3,x4,y1,1); 97 line[k++]=Line(x3,x4,y3,-1); 98 line[k++]=Line(x3,x4,y4,1); 99 line[k++]=Line(x3,x4,y2,-1); 100 line[k++]=Line(x4,x2,y1,1); 101 line[k++]=Line(x4,x2,y2,-1); 102 xx[m++]=x1; 103 xx[m++]=x2; 104 xx[m++]=x3; 105 xx[m++]=x4; 106 } 107 sort(xx+1,xx+m); 108 m=unique(xx+1,xx+m)-xx-1; 109 sort(line,line+k,cmp); 110 build(1,m,1); 111 __int64 ans=0; 112 for(i=0;i<k-1;i++){ 113 update(b_s(line[i].x1),b_s(line[i].x2)-1,line[i].val,1); 114 ans+=a[1].sum*(__int64)(line[i+1].y-line[i].y); 115 } 116 printf("%I64d\n",ans); 117 } 118 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4622494.html