http://poj.org/problem?id=2760

My Code
  1 #include <cstdio>
  2 #include <cmath>
  3 #include <algorithm>
  4 using namespace std;
  5 #define lson l,m,rt<<1
  6 #define rson m+1,r,rt<<1|1
  7 #define maxn 1005
  8 #define eps 1e-6
  9 struct node
 10 {
 11     double len;
 12     int c;
 13 }setree[maxn<<2];
 14 struct op{
 15     double l,r,h;
 16     int cnt;
 17 }mes[maxn];
 18 double point[maxn];
 19 bool cmp(struct op a,struct op b)
 20 {
 21     return a.h<b.h;
 22 }
 23 void build(int l,int r,int rt)
 24 {
 25     setree[rt].len=0;
 26     setree[rt].c=0;
 27     if(l==r)
 28     return;
 29     int m=(l+r)>>1;
 30     build(lson);
 31     build(rson);
 32 }
 33 int binsearch(int l,int r,double num)
 34 {
 35     int m=(l+r)>>1;
 36     if(num==point[m])
 37     return m;
 38     if(num<point[m])
 39     return binsearch(l,m-1,num);
 40     return binsearch(m+1,r,num);
 41 }
 42 void pushup(int rt,int l,int r)
 43 {
 44     if(setree[rt].c>0)
 45     setree[rt].len=point[r]-point[l-1];
 46     else
 47     setree[rt].len=setree[rt<<1].len+setree[rt<<1|1].len;
 48 }
 49 void update(int l,int r,int rt,int L,int R,int c)
 50 {
 51     if(L<=l&&r<=R){
 52         setree[rt].c+=c;
 53         if(setree[rt].c==0){
 54             if(l==r)
 55             setree[rt].len=0;
 56             else
 57             setree[rt].len=setree[rt<<1].len+setree[rt<<1|1].len;
 58         }
 59         else
 60         setree[rt].len=point[r]-point[l-1];
 61         return;
 62     }
 63     int m=(l+r)>>1;
 64     if(L<=m)
 65     update(lson,L,R,c);
 66     if(R>m)
 67     update(rson,L,R,c);
 68     pushup(rt,l,r);
 69 }
 70 int main()
 71 {
 72     int n;
 73     while(~scanf("%d",&n)){
 74         double a,b,c,d,lx,ly,lh;
 75         scanf("%lf%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&lx,&ly,&lh);
 76         int cn=0;
 77         for(int i=0;i<n;i++){
 78             double x1,y1,x2,y2,h;
 79             scanf("%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&h);
 80             double x0=lx+lh/(lh-h)*(x1-lx);
 81             double y0=ly+lh/(lh-h)*(y1-ly);
 82             double x00=lx+lh/(lh-h)*(x2-lx);
 83             double y00=ly+lh/(lh-h)*(y2-ly);
 84             if(x00<a||x0>=c||y0>=d||y00<=b)
 85             continue;
 86             if(x0<a)
 87             x0=a;
 88             if(x00>c)
 89             x00=c;
 90             if(y0<b)
 91             y0=b;
 92             if(y00>d)
 93             y00=d;
 94             mes[cn].l=x0;mes[cn].r=x00;mes[cn].h=y0;point[cn]=x0;mes[cn++].cnt=1;
 95             mes[cn].l=x0;mes[cn].r=x00;mes[cn].h=y00;point[cn]=x00;mes[cn++].cnt=-1;
 96         }
 97         sort(mes,mes+cn,cmp);
 98         sort(point,point+cn);
 99         int k=1;
100         for(int i=1;i<cn;i++)
101         if(fabs(point[i]-point[i-1])>eps)
102         point[k++]=point[i];
103         build(1,k,1);
104         //printf("%d**********\n",cn);
105         double ans=0;
106         for(int i=0;i<cn;i++){
107             int l=binsearch(0,k-1,mes[i].l);
108             int r=binsearch(0,k-1,mes[i].r);
109             //printf("l=%d r=%d\n",l,r);
110             update(1,n,1,l+1,r,mes[i].cnt);
111             ans+=setree[1].len*(mes[i+1].h-mes[i].h);
112         }
113         printf("%.4lf\n",(c-a)*(d-b)-ans);
114     }
115     return 0;
116 }