hdu 1823 Luck and Love 二维线段树

题目链接

很裸的题, 唯一需要注意的就是询问时给出的区间并不是l<r, 需要判断然后交换一下, WA了好多发...

#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1005;
int maxx[1005][maxn<<2], max_ans, n;
void pushUp(int pos, int rt) {
    maxx[pos][rt] = max(maxx[pos][rt<<1], maxx[pos][rt<<1|1]);
}
void sub_update(int sign, int pos, int y, int l, int r, int rt, int val) {
    if(l == r) {
        if(!sign) {
            maxx[pos][rt] = max(val, maxx[pos][rt]);
        } else {
            maxx[pos][rt] = max(maxx[pos<<1][rt], maxx[pos<<1|1][rt]);
        }
        return ;
    }
    int m = l+r>>1;
    if(y<=m)
        sub_update(sign, pos, y, lson, val);
    else
        sub_update(sign, pos, y, rson, val);
    pushUp(pos, rt);
}
void update(int x, int y, int l, int r, int rt, int val) {
    if(l == r) {
        sub_update(0, rt, y, 1, n, 1, val);
        return ;
    }
    int m = l+r>>1;
    if(x<=m)
        update(x, y, lson, val);
    else
        update(x, y, rson, val);
    sub_update(1, rt, y, 1, n, 1, val);
}
void sub_query(int pos, int L, int R, int l, int r, int rt) {
    if(L<=l&&R>=r) {
        max_ans = max(max_ans, maxx[pos][rt]);
        return ;
    }
    int m = l+r>>1;
    if(L<=m)
        sub_query(pos, L, R, lson);
    if(R>m)
        sub_query(pos, L, R, rson);
}
void query(int LX, int RX, int LY, int RY, int l, int r, int rt) {
    if(LX<=l&&RX>=r) {
        sub_query(rt, LY, RY, 1, n, 1);
        return ;
    }
    int m = l+r>>1;
    if(LX<=m)
        query(LX, RX, LY, RY, lson);
    if(RX>m)
        query(LX, RX, LY, RY, rson);
}
int main()
{
    int t, x, y, l, q;
    char c[2];
    n = 1000;
    while (cin>>q&&q) {
        mem(maxx);
        while(q--) {
            int LX, RX, LY, RY;
            double a, b;
            scanf("%s", c);
            max_ans = 0;
            if(c[0] == 'I') {
                scanf("%d%lf%lf", &x, &a, &b);
                update(x, int(a*10), 1, 200, 1, int(b*10));
            } else {
                scanf("%d%d%lf%lf", &LX, &RX, &a, &b);
                LY = a*10, RY = b*10;
                if(LX>RX)
                    swap(LX, RX);
                if(LY>RY)
                    swap(LY, RY);
                query(LX , RX, LY, RY, 1, 200, 1);
                if(max_ans == 0) {
                    puts("-1");
                } else {
                    printf("%.1f
", 1.0*max_ans/10);
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/yohaha/p/5026487.html