hdu 1542 扫描线线段树

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<sstream>
#include<string>
#include<climits>
#include<stack>
#include<set>
#include<bitset>
#include<cmath>
#include<deque>
#include<map>
#include<queue>
#define iinf 2000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define eps 1e-5
#define all(v) (v).begin(),(v).end()
#define sz(x)  x.size()
#define pb push_back
#define mp make_pair
#define lng long long
#define sqr(a) ((a)*(a))
#define pii pair<int,int>
#define pll pair<lng,lng>
#define pss pair<string,string>
#define pdd pair<double,double>
#define X first
#define Y second
#define pi 3.14159265359
#define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i)
#define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i)
#define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next)
#define cc(i,j) memset(i,j,sizeof(i))
#define two(x)			((lng)1<<(x))
#define N 2222
#define M 1000000
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define Mod  n
#define Pmod(x) (x%Mod+Mod)%Mod
using namespace std;
typedef vector<int>  vi;
typedef vector<string>  vs;
typedef unsigned int uint;
typedef unsigned lng ulng;
template<class T> inline void checkmax(T &x,T y){if(x<y) x=y;}
template<class T> inline void checkmin(T &x,T y){if(x>y) x=y;}
template<class T> inline T Min(T x,T y){return (x>y?y:x);}
template<class T> inline T Max(T x,T y){return (x<y?y:x);}
template<class T> T gcd(T a,T  b){return (a%b)==0?b:gcd(b,a%b);}
template<class T> T lcm(T a,T b){return a*b/gcd(a,b);}
template<class T> T Abs(T a){return a>0?a:(-a);}
template<class T> inline T lowbit(T n){return (n^(n-1))&n;}
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}
template<class T> inline bool isPrimeNumber(T n)
{if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
int ncase=0,n,cnt[N<<2];
double Xo[2*N];
double sum[N<<2];
struct ps
{
    int id;
    double val;
}so[2*N];
bool cmp(ps p,ps q)
{
    return p.val<q.val;
}
struct seg
{
    double h;
    int l,r;
    int x;
    seg(){}
    seg(double _h,int _l,int _r,int _x):h(_h),l(_l),r(_r),x(_x){}
}a[2*N];
bool cmp2(seg p,seg q)
{
    return p.h<q.h;
}
inline void pushup(int rt,int l,int r)
{
    if(cnt[rt]) sum[rt]=Xo[r+1]-Xo[l];
    else
    if(l==r) sum[rt]=0;
    else
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        cnt[rt]+=c;
        pushup(rt,l,r);
        return ;
    }
    int mid=(l+r)>>1;
    if(L<=mid) update(L,R,c,lson);
    if(R>mid) update(L,R,c,rson);
    pushup(rt,l,r);
}
int main()
{
   while(scanf("%d",&n)==1,n)
   {
       int y=0;
       cc(a,0);
       ff(i,1,n)
       {
           double x1,x2,y1,y2;
           scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
           if(y1>y2) swap(y1,y2);
           if(x1>x2) swap(x1,x2);
           a[2*i-1].h=y1,a[2*i-1].x=1,a[2*i].h=y2,a[2*i].x=-1;
           so[++y].id=i,so[y].val=x1,so[++y].id=i,so[y].val=x2;
       }
       sort(so+1,so+1+2*n,cmp);
       so[0].val=so[1].val+1;
       int num=0;
       ff(i,1,2*n)
       {
           if(so[i].val!=so[i-1].val)    ++num,Xo[num]=so[i].val;
              int id=so[i].id;
               if(a[2*id-1].l==0)
               a[2*id-1].l=num;
               else
               a[2*id-1].r=num;
                if(a[2*id].l==0)
               a[2*id].l=num;
               else
               a[2*id].r=num;
       }
       sort(a+1,a+1+2*n,cmp2);
      cc(cnt,0);
      cc(sum,0);
      double ans=0;
      ff(i,1,2*n-1)
      {
          int l=a[i].l,r=a[i].r-1;
          if(l<=r) update(l,r,a[i].x,1,n*2,1);
        ans+=sum[1]*(a[i+1].h-a[i].h);
      }
      printf("Test case #%d\nTotal explored area: %.2lf\n\n",++ncase,ans);
   }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值