首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用C++解释二维线段/四叉树

用C++解释二维线段/四叉树
EN

Stack Overflow用户
提问于 2014-08-04 23:11:24
回答 1查看 16.6K关注 0票数 8

附注:这可能不是复制品。我搜索了一下,确定我没有找到我想要的东西。

我是一个ACM问题解决者,最近我学习了线性数组的分段树和延迟传播的分段树。但是我遇到了一些需要2D段树(在某些地方被称为四叉树)的问题。但是我找不到关于它的任何好的教程。我搜索了一下,发现了一个链接http://e-maxx.ru/algo/segment_tree,这是一个俄语教程。

我需要一些关于2D分段树的源代码(最好是在C++中)的很好的解释。值得一提的是,我对典型的分段树非常了解。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-04 23:20:14

嗯,正如你所说的,我希望你对片段树(也就是统计树)有足够的了解。我给出了多维分段树背后的一些直觉。

假设你有一个二维的N * N (对于一个相当大的N,大到不能被暴力破解处理),你被要求执行操作-找到最小/最大值或计算网格特定部分的所有项的总和,更新任何网格索引值等。看起来,这个问题与典型的分段树问题没有什么不同,不同于数据容器的维度。这里可以选择的是构建2D分段树。

2D分段树的思想只不过是Quad Tree -A树数据结构,其中每个外部节点恰好有四个子节点。四元树最常用于通过递归地将二维空间细分为四个象限或区域来划分二维空间。这些区域可以是正方形或矩形,或者可以具有任意形状。1974年,拉斐尔·弗林克尔和J·L·本特利将这种数据结构命名为四叉树。类似的分区也称为Q树。

树的根包含通过[ (0, 0), (N - 1, N - 1) ]的完整数据段。对于每个细分市场[ (a1, b1), (a2, b2) ],我们将它们划分为[ (a1, b1), ( (a1 + a2) / 2, (b1 + b2) / 2 ) ) ][ ( (a1 + a2) / 2 + 1, b1 ), ( a2, (b1 + b2) / 2 ) ][ ( a1, (b1 + b2) / 2 + 1 ), ( (a1 + a2) / 2, b2 ) ][ ( (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1 ), ( a2, b2 ) ],直到a1 = b1a2 = b2。构建分段树的成本是O(nlogn)的,并且在分段树准备就绪后,可以在O(logn)中完成对RMQ (范围最大/最小查询)的应答。

假设你有一个N = 4的网格。那么相应的树将是-

如您所见,4 * 4阵列[ (0, 0), (3, 3) ]分为4个子阵列- [ (0, 0), (1, 1) ][ (2, 0), (3, 1) ][ (2, 0), (1, 3) ][ (2, 2), (3, 3) ]。此外,每四个块被分割成四个较小的单元;例如,段[ (2, 2), (3, 3) ]将是[ (2, 2), (2, 2) ][ (3, 2), (3, 2) ][ (2, 3), (2, 3) ][ (3, 3), (3, 3) ]。这些段是最小的单元,因此不再进一步划分。

Implementation

编码部分与分段树非常相似,不同于分段部分。这里给出的代码对编程竞赛是友好的(没有指针、内存分配/释放之类的东西和OOP结构),我已经在竞赛中多次使用过这段代码。

代码语言:javascript
复制
#include <bits/stdc++.h>
 
using namespace std;
 
#define Max 501
#define INF (1 << 30)
int P[Max][Max]; // container for 2D grid
 
/* 2D Segment Tree node */
struct Point {
    int x, y, mx;
    Point() {}
    Point(int x, int y, int mx) : x(x), y(y), mx(mx) {}
 
    bool operator < (const Point& other) const {
        return mx < other.mx;
    }
};
 
struct Segtree2d {

    // I didn't calculate the exact size needed in terms of 2D container size.
    // If anyone, please edit the answer.
    // It's just a safe size to store nodes for MAX * MAX 2D grids which won't cause stack overflow :)
    Point T[500000]; // TODO: calculate the accurate space needed
    
    int n, m;
 
    // initialize and construct segment tree
    void init(int n, int m) {
        this -> n = n;
        this -> m = m;
        build(1, 1, 1, n, m);
    }
 
    // build a 2D segment tree from data [ (a1, b1), (a2, b2) ]
    // Time: O(n logn)
    Point build(int node, int a1, int b1, int a2, int b2) {
        // out of range
        if (a1 > a2 or b1 > b2)
            return def();
 
        // if it is only a single index, assign value to node
        if (a1 == a2 and b1 == b2)
            return T[node] = Point(a1, b1, P[a1][b1]);
 
        // split the tree into four segments
        T[node] = def();
        T[node] = maxNode(T[node], build(4 * node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2 ) );
        T[node] = maxNode(T[node], build(4 * node - 1, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2 ));
        T[node] = maxNode(T[node], build(4 * node + 0, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2) );
        T[node] = maxNode(T[node], build(4 * node + 1, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2) );
        return T[node];
    }
 
    // helper function for query(int, int, int, int);
    Point query(int node, int a1, int b1, int a2, int b2, int x1, int y1, int x2, int y2) {
        // if we out of range, return dummy
        if (x1 > a2 or y1 > b2 or x2 < a1 or y2 < b1 or a1 > a2 or b1 > b2)
            return def();
 
        // if it is within range, return the node
        if (x1 <= a1 and y1 <= b1 and a2 <= x2 and b2 <= y2)
            return T[node];
 
        // split into four segments
        Point mx = def();
        mx = maxNode(mx, query(4 * node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2, x1, y1, x2, y2) );
        mx = maxNode(mx, query(4 * node - 1, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2, x1, y1, x2, y2) );
        mx = maxNode(mx, query(4 * node + 0, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2, x1, y1, x2, y2) );
        mx = maxNode(mx, query(4 * node + 1, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2, x1, y1, x2, y2));
 
        // return the maximum value
        return mx;
    }
 
    // query from range [ (x1, y1), (x2, y2) ]
    // Time: O(logn)
    Point query(int x1, int y1, int x2, int y2) {
        return query(1, 1, 1, n, m, x1, y1, x2, y2);
    }
 
    // helper function for update(int, int, int);
    Point update(int node, int a1, int b1, int a2, int b2, int x, int y, int value) {
        if (a1 > a2 or b1 > b2)
            return def();
 
        if (x > a2 or y > b2 or x < a1 or y < b1)
            return T[node];
 
        if (x == a1 and y == b1 and x == a2 and y == b2)
            return T[node] = Point(x, y, value);
 
        Point mx = def();
        mx = maxNode(mx, update(4 * node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2, x, y, value) );
        mx = maxNode(mx, update(4 * node - 1, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2, x, y, value));
        mx = maxNode(mx, update(4 * node + 0, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2, x, y, value));
        mx = maxNode(mx, update(4 * node + 1, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2, x, y, value) );
        return T[node] = mx;
    }
 
    // update the value of (x, y) index to 'value'
    // Time: O(logn)
    Point update(int x, int y, int value) {
        return update(1, 1, 1, n, m, x, y, value);
    }
 
    // utility functions; these functions are virtual because they will be overridden in child class
    virtual Point maxNode(Point a, Point b) {
        return max(a, b);
    }
 
    // dummy node
    virtual Point def() {
        return Point(0, 0, -INF);
    }
};
 
/* 2D Segment Tree for range minimum query; a override of Segtree2d class */
struct Segtree2dMin : Segtree2d {
    // overload maxNode() function to return minimum value
    Point maxNode(Point a, Point b) {
        return min(a, b);
    }
 
    Point def() {
        return Point(0, 0, INF);
    }
};
 
// initialize class objects
Segtree2d Tmax;
Segtree2dMin Tmin;
 
 
// Driver program.
int main(void) {
    int n, m;
    // input
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            scanf("%d", &P[i][j]);
 
    // initialize
    Tmax.init(n, m);
    Tmin.init(n, m);
 
    // query
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
 
    Tmax.query(x1, y1, x2, y2).mx;
    Tmin.query(x1, y1, x2, y2).mx;
 
    // update
    int x, y, v;
    scanf("%d %d %d", &x, &y, &v);
    Tmax.update(x, y, v);
    Tmin.update(x, y, v);
 
    return 0;
}

3D分割

给你一个3D网格,并要求你执行类似的操作,如2D线段树,这并不是不可能的。在这种情况下,我们可以构建一个3D分段树,就像我们对2D网格所做的那样。

我们将网格分成8个较小的部分,并递归地进行细分,直到出现最小的单元。下图显示了这种分割思想。

对于1D分段树,我们将数组划分为2 (2^1)个分段,这为特定操作产生了log2(n)复杂度。同样,对于2D分段树,我们将每一步中的2D网格划分为4 (2^2)个分段,以确保每一次操作都具有log2(n)成本。因此,以类似的方式,我们通过将网格细分为8 (2^3)个部分来扩展此3D树。

附言: 3D片段树是我自己的想象;我不知道是否有类似的东西。也许有一种更好的方法可以同时用于2D和3D分段树,但我认为这些代码足以满足编程竞赛的需要,因为我已经使用了很多次。

编辑

3D分割的想法只不过是Octree -一种树数据结构,其中每个内部节点恰好有八个子节点。八叉树最常用于通过递归地将三维空间细分为八个八叉树来划分三维空间。八叉树是四叉树的三维模拟。

八叉树通常用于3D图形和3D游戏引擎。它还有许多其他应用,如空间索引、最近邻搜索、三维高效碰撞检测和so many

希望它能帮上忙!

票数 35
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25121878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档