HDOJ-1823(矩形树,点更新 + 区间查询)

本文分享了一个关于矩形树区间查询的优化经验,重点介绍了如何处理输入数据的特殊情况,并通过一个具体的实现案例来说明如何避免常见的错误。文章还探讨了查询区间方向不确定时的解决方案,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没注意到几个细节,WA了一天啊:

(1)输入的活泼值和缘分值,虽然说是浮点数,但可能是以整数形式输入的(为避免浮点精度降低,我把它们当做字符串输入,却忽略了没有小数点的情况,WA死了)

(2)看了discuss才知道,查询区间A1、A2和H1、H2,题目中没有说哪个是左哪个是右(我以为先输入的是左,后输入的是右,当左大于右时,输出-1,WA了好几次)

矩形树的思想,参考了这篇博文:http://blog.sina.com.cn/s/blog_88705ca20101367f.html


#include <cstdio>
#include <algorithm>
using namespace std;

struct Point{
    int x, y;
    
    Point(){}
    void set(int a, int b){
        x = a, y = b;
    }
    bool operator == (const Point& other)const{
        return x == other.x && y == other.y;
    }
};
struct Rectangle{
    Point leftDown, rightUp;
    
    Rectangle(){}
    void setBounds(int x1, int y1, int x2, int y2){
        leftDown.x = x1;
        leftDown.y = y1;
        rightUp.x = x2;
        rightUp.y = y2;
    }
    int getLeft()const{
        return leftDown.x;
    }
    int getRight()const{
        return rightUp.x;
    }
    int getTop()const{
        return rightUp.y;
    }
    int getBottom()const{
        return leftDown.y;
    }
    bool isPoint()const{
        return leftDown == rightUp;
    }
    bool contains(const Point& p)const{
        return getLeft() <= p.x && p.x <= getRight() &&
               getBottom() <= p.y && p.y <= getTop() ;
    }
    bool intersects(const Rectangle& other)const{
        return !(getLeft() > other.getRight() ||
                 getRight() < other.getLeft() ||
                 getBottom() > other.getTop() ||
                 getTop() < other.getBottom() );
    }
    bool liesIn(const Rectangle& other)const{
        return other.contains(leftDown) && other.contains(rightUp);
    }
};

struct Node{
    Rectangle rec;
    int value;
    Node* children[4];
    Node(): value(-1){
        children[0] = NULL;//left down
        children[1] = NULL;//right up
        children[2] = NULL;//left up
        children[3] = NULL;//right down
    }
};
Node arr[100 * 1000 * 2];
int  indexOfArr = 0;

Node* build(int x1, int y1, int x2, int y2)
{
    Node* p = &arr[indexOfArr++];
    p->rec.setBounds(x1, y1, x2, y2);
    if(p->rec.isPoint()) return p;
    
    int midx = (x1 + x2) >> 1, midy = (y1 + y2) >> 1;
    //definitely have left down part
    p->children[0] = build(x1, y1, midx, midy);
    //other children may not exist
    if(midx+1 <= x2 && midy+1 <= y2) p->children[1] = build(midx+1, midy+1, x2, y2);
    if(midy+1 <= y2) p->children[2] = build(x1, midy+1, midx, y2);
    if(midx+1 <= x2) p->children[3] = build(midx+1, y1, x2, midy);
    return p;
}
void update(const Point& point, int v, Node* p)
{
    p->value = max(p->value, v);
    if(p->rec.isPoint()) return;
    
    for(int i = 0; i < 4; ++i){
        if(p->children[i] && p->children[i]->rec.contains(point)){
            update(point, v, p->children[i]);
        }
    }
}
int query(const Rectangle& rect, const Node* p)
{
    if(p->value == -1 || p->rec.liesIn(rect)) return p->value;
    
    int m = -1;
    for(int i = 0; i < 4; ++i){
        if(p->children[i] && p->rec.intersects(rect)){
            m = max(m, query(rect, p->children[i]));
        }
    }
    return m;
}
void reset()
{
    for(int i = 0; i < indexOfArr; ++i) arr[i].value = -1;
}
int parse(const char* s)
{
    int a, b = 0;//注意如果没有小数点,b的值不变,所以一定要初始化为0啊
    sscanf(s, "%d.%d", &a, &b);
    return a * 10 + b;
}

int main()
{
    int N, H1, H2, res;
    char A1[8], A2[8];
    Node* root;
    Point p;
    Rectangle rec;
    
    //build tree once
    root = build(100, 0, 200, 1000);
    //for each case
    while(scanf("%d", &N), N){
    //reset tree
        reset();
        while(N--){
        //skip rest of previous line
            while(getchar() != '\n') ;
        //first check operation type
            if(getchar() == 'I'){
                scanf("%d %s %s", &H1, A1, A2);
                p.set(H1, parse(A1));
                update(p, parse(A2), root);
            }
            else{
                scanf("%d %d %s %s", &H1, &H2, A1, A2);
                rec.setBounds(H1, parse(A1), H2, parse(A2));
                if(rec.getLeft() > rec.getRight()) swap(rec.leftDown.x, rec.rightUp.x);//这里要交换啊
                if(rec.getBottom() > rec.getTop()) swap(rec.leftDown.y, rec.rightUp.y);//这里要交换啊
                res = query(rec, root);
                if(res == -1) puts("-1");
                else printf("%.1f\n", res / 10.0);
            }
        }
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值