[hdu1823]Luck and Love(二维线段树)

解题关键:二维线段树模板题(单点修改、查询max)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int n,s[1005][1005<<2];

void subBuild(int xrt,int rt,int l,int r){
    if(l==r){
        s[xrt][rt]=-1;
        return;
    }
    int mid=l+r>>1;
    subBuild(xrt,lson);
    subBuild(xrt,rson);
    s[xrt][rt]=max(s[xrt][rt<<1],s[xrt][rt<<1|1]);
}

void build(int rt,int l,int r){
    subBuild(rt,1,0,1000);
    if(l!=r){
        int mid=l+r>>1;
        build(lson);
        build(rson);
    }
}

void subUpdate(int xrt,int rt,int l, int r, int y, int c) {
    if(l==r){
        s[xrt][rt]=max(s[xrt][rt],c);
        return;
    }
    int mid=l+r>>1;
    if(y<=mid) subUpdate(xrt,lson,y,c);
    else subUpdate(xrt,rson,y,c);
    s[xrt][rt]=max(s[xrt][rt<<1],s[xrt][rt<<1|1]);
}

void update(int rt,int l,int r,int x, int y, int c) {
    subUpdate(rt,1,0,1000,y,c);//update的区间都包含更新区间,update只有一个点
    if(l!=r){
        int mid=l+r>>1;
        if(x<=mid) update(lson,x, y,c);
        else update(rson,x, y,c);
    }
}

int subQuery(int xrt,int rt,int l,int r,int yl, int yr){
    if(yl<=l&&r<=yr) return s[xrt][rt];
    int mid=l+r>>1;
    int res=-1;
    if(yl<=mid) res=subQuery(xrt,lson, yl, yr);
    if(yr>mid) res=max(res, subQuery(xrt,rson,yl, yr));
    return res;
}

int query(int rt,int l,int r,int xl, int xr, int yl, int yr) {
    if(xl<=l&&r<=xr) return subQuery(rt,1,0,n,yl,yr);
    int mid =l+r>>1,res=-1;
    if(xl<=mid) res=query(lson,xl, xr, yl, yr);
    if(xr>mid) res=max(res, query(rson,xl, xr, yl, yr));
    return res;
}
int main(){
    int t;
    while(scanf("%d", &t) && t) {
        n = 1000;
        //build(1,100,200);
        memset(s,-1,sizeof s);
        while(t--){
            char ch[2];
            int a, b;
            double c, d;
            scanf("%s",ch);
            if(ch[0] == 'I') {
                scanf("%d%lf%lf", &a, &c, &d);
                update(1,100,200,a, c*10, d*10);
            } else {
                scanf("%d%d%lf%lf", &a, &b, &c, &d);
                int cc = c * 10, dd = d * 10;
                if(a > b) swap(a, b);
                if(cc > dd) swap(cc, dd);
                int ans = query(1,100,200,a, b, cc, dd);
                if(ans == -1) printf("-1\n");
                else printf("%.1f\n", ans / 10.0);
            }
        }
    }
    return 0;
}

 

posted @ 2019-02-25 20:41  Elpsywk  阅读(170)  评论(0编辑  收藏  举报