zoukankan      html  css  js  c++  java
  • hdu 1823 Luck and Love 二维线段树

    母线段树每个节点保存一个子线段树。

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    #define pb(a) push_back(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in.txt","r",stdin);
       // freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    
    const int maxh=110;
    const int maxa=1100;
    struct Tree
    {
        double maxv[maxh<<2];
        int build(int idx,int l,int r)
        {
            maxv[idx]=-1;
            if(l==r)return 0;
            int mid=(r+l)>>1;
            build(lson);
            build(rson);
            return 0;
        }
        int update(int idx,int l,int r,int pos,double v)
        {
            if(l==r&&l==pos)
            {
                maxv[idx]=max(maxv[idx],v);
                return 0;
            }
            int mid=(r+l)>>1;
            if(pos<=mid)update(lson,pos,v);
            else update(rson,pos,v);
            maxv[idx]=max(maxv[idx<<1],maxv[idx<<1|1]);
            return 0;
        }
        double query(int idx,int l,int r,int tl,int tr)
        {
            if(tl<=l&&tr>=r)
                return maxv[idx];
            int mid=(r+l)>>1;
            double x=-1;
            if(tl<=mid)x=max(x,query(lson,tl,tr));
            if(tr>mid)x= max(x,query(rson,tl,tr));
            return x;
        }
    };
    Tree tree[maxa<<2];
    int build(int idx,int l,int r)
    {
        tree[idx].build(1,100,200);
        if(l==r)return 0;
        int mid=(r+l)>>1;
        build(lson);
        build(rson);
        return 0;
    }
    int update(int idx,int l,int r,int a,int h,double v)
    {
        tree[idx].update(1,100,200,h,v);
        if(l==r&&l==a)
        {
            return 0;
        }
        int mid=(r+l)>>1;
        if(a<=mid)update(lson,a,h,v);
        else update(rson,a,h,v);
        return 0;
    }
    double query(int idx,int l,int r,int tla,int tra,int tlh,int trh)
    {
        if(tla<=l&&tra>=r)
            return tree[idx].query(1,100,200,tlh,trh);
        int mid=(r+l)>>1;
        double x=-1;
        if(tla<=mid)x=max(x,query(lson,tla,tra,tlh,trh));
        if(tra>mid)x=max(x,query(rson,tla,tra,tlh,trh));
        return x;
    }
    int main()
    {
       // debug();
        int n;
        while(scanf("%d",&n)!=EOF&&n)
        {
            build(1,0,1000);
            for(int i=1;i<=n;i++)
            {
                char op[10];
                scanf("%s",op);
                if(op[0]=='I')
                {
                    int h;
                    double a,v;
                    scanf("%d %lf %lf",&h,&a,&v);
                    update(1,0,1000,(int)((a+0.00001)*10),h,v);
                }else
                {
                    int h1,h2;
                    double a1,a2;
                    scanf("%d%d%lf%lf",&h1,&h2,&a1,&a2);
                    if(h1>h2)swap(h1,h2);
                    if(a1>a2)swap(a1,a2);
                    double num=query(1,0,1000,(int)((a1+0.00001)*10),(int)((a2+0.00001)*10),h1,h2);
                    if(num>=0)printf("%.1lf
    ",num+0.00001);
                    else printf("-1
    ");
    
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    算法与设计模式
    Python开源应用系统
    ASP.NET MVC配置Redis服务
    常用3个框架
    Visual Studio 2015 编译错误 File 的值+乱码的解决方法
    SQL Server2008 R2命令行启动及停止SQL服务的方法
    Linux Shell查看物理CPU个数、核数、逻辑CPU个数
    SQL SERVER 2008R2 执行大脚本文件时,管理工具提示“内存不足”的解决方法
    MVC中未能加载程序集System.Web.Http/System.Web.Http.WebHost
    Windows10中启用原来的Windows照片查看器方法
  • 原文地址:https://www.cnblogs.com/BMan/p/3312478.html
Copyright © 2011-2022 走看看