bzoj1382: [Baltic2001]Mars Maps 2011-12-20
1382: [Baltic2001]Mars Maps
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 85 Solved: 38
[Submit][Status][Discuss]
Description
给出N个矩形,N<=10000.其坐标不超过10^9.求其面积并
Input
先给出一个数字N,代表有N个矩形. 接下来N行,每行四个数,代表矩形的坐标.
Output
输出面积并
Sample Input
2
10 10 20 20
15 15 25 30
Sample Output
225
HINT
Source
____________________________________
线段树求矩形面积并,同poj1151,坐标为实数,不需要离散化处理。
____________________________________
1 Program Stone; 2 3 var i,j,k,n,m,lc,rc:longint; 4 5 x1,y1,x2,y2:array[1..100000]of longint; 6 7 s,p:array[0..200000]of longint; 8 9 io:array[1..200000]of longint; 10 11 ans:int64; 12 13 b:array[1..500000]of longint; 14 15 a:array[1..500000]of int64; 16 17 procedure kp(t,w:longint); 18 19 var i,j,l,k,mid:longint; 20 21 begin 22 23 i:=t;j:=w;mid:=s[(t+w)div 2]; 24 25 repeat 26 27 while s[i]<mid do inc(i); 28 29 while s[j]>mid do dec(j); 30 31 if i<=j then begin 32 33 k:=s[i];s[i]:=s[j];s[j]:=k; 34 35 l:=io[i];io[i]:=io[j];io[j]:=l; 36 37 inc(i);dec(j); 38 39 end; 40 41 until i>j; 42 43 if i<w then kp(i,w); 44 45 if j>t then kp(t,j); 46 47 end; 48 49 procedure upadd(head,tail,num:longint); 50 51 var i,j,k:longint; 52 53 begin 54 55 if (lc<=head)and(tail<=rc) then begin 56 57 a[num]:=p[tail]-p[head-1]; 58 59 inc(b[num]); 60 61 exit; 62 63 end; 64 65 k:=(head+tail)div 2; 66 67 if lc<=k then upadd(head,k,num*2); 68 69 if k<rc then upadd(k+1,tail,num*2+1); 70 71 a[num]:=a[num*2]+a[num*2+1]; 72 73 if b[num]>0 then a[num]:=p[tail]-p[head-1]; 74 75 end; 76 77 procedure updel(head,tail,num:longint); 78 79 var i,j,k:longint; 80 81 begin 82 83 if (lc<=head)and(tail<=rc) then begin 84 85 dec(b[num]); 86 87 if b[num]=0 then a[num]:=a[num*2]+a[num*2+1]; 88 89 exit; 90 91 end; 92 93 k:=(head+tail)div 2; 94 95 if lc<=k then updel(head,k,num*2); 96 97 if k<rc then updel(k+1,tail,num*2+1); 98 99 if b[num]=0 then a[num]:=a[num*2]+a[num*2+1]; 100 101 end; 102 103 104 105 procedure init; 106 107 var i,j,k:longint; 108 109 begin 110 111 readln(n); 112 113 for i:=1 to n do 114 115 begin 116 117 readln(x1[i],y1[i],x2[i],y2[i]); 118 119 s[i]:=y1[i];s[i+n]:=y2[i]; 120 121 io[i]:=i;io[i+n]:=i+n; 122 123 end; 124 125 kp(1,2*n); 126 127 m:=1;y1[io[1]]:=1; 128 129 for i:=2 to 2*n do 130 131 begin 132 133 if s[i]<>s[i-1] then begin 134 135 p[m]:=s[i]-s[i-1]; 136 137 inc(m); 138 139 end; 140 141 if io[i]>n then y2[io[i]-n]:=m 142 143 else y1[io[i]]:=m; 144 145 end; 146 147 148 149 for i:=2 to m do p[i]:=p[i]+p[i-1]; 150 151 for i:=1 to n do 152 153 begin 154 155 s[i]:=x1[i];s[i+n]:=x2[i]; 156 157 io[i]:=i;io[i+n]:=i+n; 158 159 end; 160 161 kp(1,2*n); 162 163 ans:=0; 164 165 for i:=1 to 2*n do 166 167 begin 168 169 if s[i]<>s[i-1] then ans:=ans+a[1]*(s[i]-s[i-1]); 170 171 if io[i]>n then j:=io[i]-n else j:=io[i]; 172 173 lc:=y1[j];rc:=y2[j]-1; 174 175 if io[i]<=n then upadd(1,m,1) 176 177 else updel(1,m,1); 178 179 end; 180 181 end; 182 183 begin 184 185 assign(input,'input.in');reset(input); 186 187 assign(output,'output.out');rewrite(output); 188 189 init; 190 191 writeln(ans); 192 193 close(input);close(output); 194 195 end. 196 197
_____MildTheorem