Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1255

面积并

View Code
  1 //1255
2 #include <cstdio>
3 #include <algorithm>
4 using namespace std;
5
6 const int N=1010*2;
7 struct segment
8 {
9 double x1,x2,h;
10 int v;
11 segment(){}
12 segment(double t1,double t2,double th,double tv):
13 x1(t1),x2(t2),h(th),v(tv){}
14 bool operator < (const segment& t)const
15 {
16 return h<t.h;
17 }
18 }seg[N];
19 struct segtree
20 {
21 int l,r,c;
22 double s1,s2;
23 int m() {return (l+r)>>1;}
24 }st[N*4];
25 double val[N*2];
26 int bfind(double x,int l,int r)
27 {
28 while(l<r)
29 {
30 int m=(l+r)>>1;
31 if(x==val[m]) return m;
32 else if(x<val[m]) r=m;
33 else l=m+1;
34 }
35 return l;
36 }
37 void build(int l,int r,int rt)
38 {
39 st[rt].l=l; st[rt].r=r;
40 st[rt].s1=st[rt].s2=st[rt].c=0;
41 if(l==r) return;
42 int m=st[rt].m();
43 build(l,m,rt*2);
44 build(m+1,r,rt*2+1);
45 }
46 void pushup(int rt)
47 {
48 int l=st[rt].l, r=st[rt].r;
49 if(st[rt].c>1)
50 {
51 st[rt].s2=val[r]-val[l-1];
52 st[rt].s1=0;
53 }
54 else if(st[rt].c==1)
55 {
56 if(l==r) st[rt].s2=0;
57 else st[rt].s2=st[rt*2].s1+st[rt*2].s2+st[rt*2+1].s1+st[rt*2+1].s2;
58 st[rt].s1=val[r]-val[l-1]-st[rt].s2;
59 }
60 else
61 {
62 if(l==r) st[rt].s2=st[rt].s1=0;
63 else
64 {
65 st[rt].s1=st[rt*2].s1+st[rt*2+1].s1;
66 st[rt].s2=st[rt*2].s2+st[rt*2+1].s2;
67 }
68 }
69 }
70 void update(int a,int b,int x,int rt)
71 {
72 int l=st[rt].l, r=st[rt].r;
73 if(a<=l && r<=b)
74 {
75 st[rt].c+=x;
76 pushup(rt);
77 return;
78 }
79 int m=st[rt].m();
80 if(a<=m) update(a,b,x,rt*2);
81 if(b>m) update(a,b,x,rt*2+1);
82 pushup(rt);
83 }
84 int main()
85 {
86 int T;
87 scanf("%d",&T);
88 while(T--)
89 {
90 int n;
91 scanf("%d",&n);
92 int nx=0,ny=0;
93 for(int i=1;i<=n;i++)
94 {
95 double x1,x2,y1,y2;
96 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
97 val[nx++]=x1; val[nx++]=x2;
98 seg[ny++]=segment(x1,x2,y1,1);
99 seg[ny++]=segment(x1,x2,y2,-1);
100 }
101 sort(seg,seg+ny);
102 sort(val,val+nx);
103 int m=1;
104 for(int i=1;i<nx;i++)
105 if(val[i]!=val[i-1]) val[m++]=val[i];
106 m--;
107 double ans=0;
108 build(1,m,1);
109 for(int i=0;i<ny;i++)
110 {
111 int x1=bfind(seg[i].x1,0,m)+1;
112 int x2=bfind(seg[i].x2,0,m);
113 update(x1,x2,seg[i].v,1);
114 ans+=(seg[i+1].h-seg[i].h)*st[1].s2;
115 }
116 printf("%.2lf\n",ans);
117 }
118 return 0;
119 }

 

posted on 2012-03-28 18:50  Qiuqiqiu  阅读(157)  评论(0编辑  收藏  举报