hdu 1823 Luck and Love(二维线段树)

认真+小心

#include <stdio.h>

 

#define MAXSUB 1000*3
#define MAXMAIN 100*3
#define INF -1.1

inline double Max(double a,double b)
{
    return a>b?a:b;
}

struct SubNode
{
    int l,r;
    double maxv;
};
class SubTree
{public:
    SubNode p[MAXSUB];
    void create(int A1,int A2,int idx)
    {
        p[idx].l=A1;
        p[idx].r=A2;
   //     p[idx].maxv=INF;
        if(A1 == A2)
        {
            p[idx].maxv=INF;
            return;
        }
        int mid = (A1 + A2) / 2;
        create(A1,mid,idx*2);
        create(mid+1,A2,idx*2+1);
        p[idx].maxv = Max( p[idx*2].maxv , p[idx*2+1].maxv );
    }
    void cutin(int A,double yf,int idx)
    {
        if( A == p[idx].l && p[idx].r == A)
        {
            p[idx].maxv = Max( p[idx].maxv, yf );
            return;
        }
        if( A <= p[idx*2].r ) cutin(A,yf,idx*2);
        else cutin(A,yf,idx*2+1);
        p[idx].maxv = Max( p[idx*2].maxv, p[idx*2+1].maxv );
    }
    double ask(int A1,int A2,int idx)
    {
        if( A1 <= p[idx].l && p[idx].r <= A2)
        {
            return p[idx].maxv;
        }
        if( A2 <= p[idx*2].r ) return ask(A1,A2,idx*2);
        else if( A1 >= p[idx*2+1].l ) return ask(A1,A2,idx*2+1);
        else return Max( ask(A1,p[idx*2].r,idx*2), ask(p[idx*2+1].l,A2,idx*2+1));
    }
};

struct MainNode
{
    int l,r;
    SubTree st;
};
class MainTree
{public:
    MainNode node[MAXMAIN];
    void build(int H1,int H2,int idx)
    {
        node[idx].l=H1;
        node[idx].r=H2;
        node[idx].st.create(0,1000,1);
        if(H1 == H2)
        {
            return;
        }
        int mid = (H1 + H2) /2;
        build(H1,mid,idx*2);
        build(mid+1,H2,idx*2+1);
    }
    void insert(int H,int A,double yf,int idx)
    {
        node[idx].st.cutin(A,yf,1);
        if( H == node[idx].l && node[idx].r == H )
        {
            return;
        }
        if( H <= node[idx*2].r ) insert(H,A,yf,idx*2);
        else insert( H,A,yf,idx*2+1 );
    }
    double query(int H1,int H2,int A1,int A2,int idx)
    {
        if( H1 <= node[idx].l && node[idx].r <= H2 )
        {
            return node[idx].st.ask(A1,A2,1);
        }
        if( H2 <= node[idx*2].r ) return query(H1,H2,A1,A2,idx*2);
        else if( H1 >= node[idx*2+1].l ) return query(H1,H2,A1,A2,idx*2+1);
        else return Max( query(H1,node[idx*2].r,A1,A2,idx*2) , query(node[idx*2+1].l,H2,A1,A2,idx*2+1));
    }
};

inline void swap(int &a,int &b)
{
    int t=a;
    a=b;
    b=t;
}

MainTree segTree;

int main()
{
    int M,H1,H2,a1,a2;
    double A1,A2,L;
    char ch;
  //  freopen("testdata.txt","r",stdin);
    while(scanf("%d",&M),M)
    {
        getchar();
        segTree.build(100,200,1);
        while(M--)
        {
            ch=getchar();
            if(ch=='I')
            {
                scanf("%d %lf %lf",&H1,&A1,&L);
                a1=A1*10;
         //       printf("%d %d %f\n",H1,a1,L);
                segTree.insert(H1,a1,L,1);
            }
            else
            {
                scanf("%d %d %lf %lf",&H1,&H2,&A1,&A2);
                a1=A1*10;
                a2=A2*10;
                if(H1>H2) swap(H1,H2);
                if(a1>a2) swap(a1,a2);
       //         printf("%d %d %d %d\n",H1,H2,a1,a2);

                double ans = segTree.query(H1,H2,a1,a2,1);
                if(ans==INF) printf("-1\n");//靠!!!
                else printf("%.1f\n",ans);
            }
            getchar();
        }
    }
    return 0;
}

posted @ 2010-08-30 22:40  菜到不得鸟  阅读(325)  评论(0)    收藏  举报