问题 K: 【USACO2012Feb】植草 {Bronze题2}
按着矩形周长的思路,到当前边的时候,前一层的覆盖数乘以高度加入 ans 就行,然而真正的算法可能并不是这个。。只能想到这个了
1 const maxe=20000;
2 type
3 node=record
4 l,r,mid,sum,delta:longint;
5 end;
6 arr=array[0..3] of longint;
7 var i,j,n,bs,x1,y1,x2,y2,b1,b2,l,r,ans,_sum,v:longint;
8 bx:array[0..maxe] of arr;
9 tree:array[0..4*maxe] of node;
10 procedure qsx(t,w:longint);
11 var mid1,mid2,l,r:longint;
12 tem:arr;
13 begin
14 l:=t; r:=w; mid1:=bx[(l+r) shr 1,3]; mid2:=bx[(l+r) shr 1,0];
15 //writeln(l,' ',r);
16 repeat
17 begin
18 while (bx[l,3]<mid1) or ((bx[l,3]=mid1) and (bx[l,0]>mid2)) do inc(l);
19 while (bx[r,3]>mid1) or ((bx[r,3]=mid1) and (bx[r,0]<mid2)) do dec(r);
20 if l<=r then
21 begin
22 tem:=bx[l];
23 bx[l]:=bx[r];
24 bx[r]:=tem;
25 inc(l); dec(r);
26 end;
27 end;
28 until l>r;
29 if t<r then qsx(t,r);
30 if l<w then qsx(l,w);
31 end;
32 procedure build(o,l,r:longint);
33 begin
34 tree[o].l:=l; tree[o].r:=r; tree[o].mid:=(l+r) shr 1;
35 tree[o].delta:=0; tree[o].sum:=0;
36 if l<>r then
37 begin
38 build(o*2,l,tree[o].mid);
39 build(o*2+1,tree[o].mid+1,r);
40 end;
41 end;
42 procedure maintain(o:longint);
43 begin
44 tree[o].sum:=0;
45 if tree[o].l<tree[o].r then tree[o].sum:=tree[2*o].sum+tree[2*o+1].sum;
46 if tree[o].delta>0 then tree[o].sum:=tree[o].r-tree[o].l+1;
47 end;
48 procedure update(o:longint);
49 begin
50 if (l<=tree[o].l) and (tree[o].r<=r) then inc(tree[o].delta,v)
51 else begin
52 if l<=tree[o].mid then update(2*o);
53 if r>tree[o].mid then update(2*o+1);
54 end;
55 maintain(o);
56 end;
57 begin
58 readln(n); bs:=2*n;
59 for i:=1 to n do
60 begin
61 readln(x1,y2,x2,y1);
62 inc(x1,10001); inc(x2,10001);
63 inc(y1,10001); inc(y2,10001);
64 b1:=2*i-1; b2:=2*i; //writeln(y1,' ',y2);
65 bx[b1,0]:=1; bx[b1,1]:=x1; bx[b1,2]:=x2; bx[b1,3]:=y1;
66 bx[b2,0]:=-1;bx[b2,1]:=x1; bx[b2,2]:=x2; bx[b2,3]:=y2;
67 end;
68 qsx(1,bs); ans:=0;
69 build(1,1,20000);
70 for i:=1 to bs do
71 begin
72 l:=bx[i,1]; r:=bx[i,2]-1; v:=bx[i,0];
73 update(1);
74 inc(ans,_sum*(bx[i,3]-bx[i-1,3]));
75 _sum:=tree[1].sum;
76 end;
77 writeln(ans);
78 end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)