HDU 1823 二维线段树

HDU 1823
题目链接:
http://www.bnuoj.com/v3/problem_show.php?pid=5978
题意:
中文题
思路:
裸二维线段树,题目坑在输入部分已注释
源码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int MAXH = 100 + 5;
const int MAXA = 1001 + 5;
double tree[MAXH * 4][MAXA * 4];
char str[10];
void push_upx(int ox, int oy){tree[ox][oy] = max(tree[ox << 1][oy], tree[(ox << 1) | 1][oy]);}
void push_upy(int ox, int oy){tree[ox][oy] = max(tree[ox][oy << 1], tree[ox][oy << 1 | 1]);}
void updatey(int ox, int oy, int ly, int ry, int y, double val, int flag)
{
// printf("ox = %d, oy = %d, ly = %d, ry = %d\n", ox, oy, ly, ry);
// system("pause");
    if(ly == ry){
// printf("flag = %d\n", flag);
        if(flag) tree[ox][oy] = max(tree[ox][oy], val);
        else push_upx(ox, oy);
    }
    else{
        int mid = (ly + ry) >> 1;
        if(y <= mid) updatey(ox, oy << 1, ly, mid, y, val, flag);
        else    updatey(ox, oy << 1 | 1, mid + 1, ry, y, val, flag);
        push_upy(ox, oy);
    }
}
void updatex(int ox, int lx, int rx, int x, int y, double val)
{
// printf("ox = %d, lx =%d, rx = %d, x = %d, y = %d\n", ox, lx, rx, x, y);
// system("pause");
    if(lx != rx){
        int mid = (lx + rx) >> 1;
        if(x <= mid) updatex(ox << 1, lx, mid, x, y, val);
        else updatex((ox << 1) | 1, mid + 1, rx, x, y, val);
    }
    updatey(ox, 1, 1, 1001, y, val, lx == rx);
}
double queryy(int ox, int oy, int ly, int ry, int y1, int y2)
{
    if(ly >= y1 && ry <= y2) return tree[ox][oy];
    int mid = (ly + ry) >> 1;
    double ans = -1;
    if(y1 <= mid) ans = max(ans, queryy(ox, oy << 1, ly, mid, y1, y2));
    if(y2 > mid) ans = max(ans, queryy(ox, oy << 1 | 1, mid + 1, ry, y1, y2));
    return ans;
}
double queryx(int ox, int lx, int rx, int x1, int x2, int y1, int y2)
{
    if(lx >= x1 && rx <= x2) return queryy(ox, 1, 1, 1001, y1, y2);
    else{
        int mid = (lx + rx) >> 1;
        double ans = -1;
        if(x1 <= mid) ans = max(ans, queryx(ox << 1, lx, mid, x1, x2, y1, y2));
        if(x2 > mid) ans = max(ans, queryx(ox << 1 | 1, mid + 1, rx, x1, x2, y1, y2));
        return ans;
    }
}
int main()
{
    int m;
    while(scanf("%d", &m) != EOF && m){
        for(int i = 0 ; i < 4 * MAXH ; i++) for(int j = 0 ; j < 4 * MAXA ; j++) tree[i][j] = -1;
        while(m--){
            scanf("%s", str);
            if(str[0] == 'I'){
                double h, a, l;
                scanf("%lf%lf%lf", &h, &a, &l);
                updatex(1, 1, 101, h - 99, a * 10 + 1, l);
            }
            else{
                double h1, h2, a1, a2;
                scanf("%lf%lf%lf%lf", &h1, &h2, &a1, &a2);
                if(h1 > h2) swap(h1, h2);
                if(a1 > a2) swap(a1, a2);
                double ans = queryx(1, 1, 101, h1 - 99, h2 - 99, a1 * 10 + 1, a2 * 10 + 1);
                if(ans == -1.0) printf("-1\n");
                else printf("%.1f\n", ans);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(HDU 1823 二维线段树)