标签:
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3310 Accepted Submission(s): 1723
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 5005 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 sum; 23 int ysum; 24 int val; 25 bool lv, rv; 26 }a[N*8]; 27 28 struct Line{ 29 int x1, x2, y; 30 int val; 31 Line(){} 32 Line(int a,int b,int c,int d){ 33 x1=a; 34 x2=b; 35 y=c; 36 val=d; 37 } 38 }line[N*2]; 39 40 bool cmp(Line a,Line b){ 41 return a.y<b.y; 42 } 43 int n, m; 44 int xx[N*2]; 45 46 int b_s(int key){ 47 int l=1, r=m; 48 while(l<=r){ 49 int mm=(l+r)/2; 50 if(xx[mm]==key) return mm; 51 else if(xx[mm]>key) r=mm-1; 52 else if(xx[mm]<key) l=mm+1; 53 } 54 } 55 56 void build(int l,int r,int root){ 57 a[root].l=l; 58 a[root].r=r; 59 a[root].ysum=a[root].sum=a[root].val=0; 60 a[root].lv=a[root].rv=false; 61 if(l==r) return; 62 build(l,mid,ll); 63 build(mid+1,r,rr); 64 } 65 66 void up(int root){ 67 if(a[root].val){ 68 a[root].sum=xx[a[root].r+1]-xx[a[root].l]; 69 a[root].lv=a[root].rv=true; 70 a[root].ysum=2; 71 } 72 else if(a[root].l==a[root].r) { 73 a[root].lv=a[root].rv=false; 74 a[root].ysum=a[root].sum=0; 75 } 76 else{ 77 a[root].sum=a[ll].sum+a[rr].sum; 78 a[root].lv=a[ll].lv; 79 a[root].rv=a[rr].rv; 80 a[root].ysum=a[ll].ysum+a[rr].ysum; 81 if(a[rr].lv&&a[ll].rv) a[root].ysum-=2; 82 } 83 } 84 85 void update(int l,int r,int val,int root){ 86 if(a[root].l==l&&a[root].r==r){ 87 a[root].val+=val; 88 up(root); 89 return; 90 } 91 if(l>=a[rr].l) update(l,r,val,rr); 92 else if(r<=a[ll].r) update(l,r,val,ll); 93 else{ 94 update(l,mid,val,ll); 95 update(mid+1,r,val,rr); 96 } 97 up(root); 98 } 99 100 main() 101 { 102 int i, j, k; 103 int x1, y1, x2, y2; 104 int kase=1; 105 while(scanf("%d",&n)==1&&n){ 106 m=1;k=0; 107 for(i=0;i<n;i++){ 108 scanf("%d %d %d %d",&x1,&y1,&x2,&y2); 109 line[k++]=Line(x1,x2,y1,1); 110 line[k++]=Line(x1,x2,y2,-1); 111 xx[m++]=x1;xx[m++]=x2; 112 } 113 sort(xx+1,xx+m); 114 m=unique(xx+1,xx+m)-xx-1; 115 sort(line,line+k,cmp); 116 build(1,m,1); 117 int ans=0; 118 int pre=0; 119 for(i=0;i<k-1;i++){ 120 update(b_s(line[i].x1),b_s(line[i].x2)-1,line[i].val,1); 121 ans+=abs(a[1].sum-pre)+(line[i+1].y-line[i].y)*a[1].ysum; 122 pre=a[1].sum; 123 } 124 update(b_s(line[k-1].x1),b_s(line[k-1].x2)-1,line[k-1].val,1); 125 ans+=abs(a[1].sum-pre); 126 printf("%d\n",ans); 127 } 128 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4622378.html