1. Trang chủ
  2. » Công Nghệ Thông Tin

Cây IT 2D ebook

4 324 1

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Suppose you’re given a two dimensional N * N (for a pretty large N, large enough to be not handled by brute-force) grid of integer value and you’re asked to perform operations – find the minimum/maximum value or calculate the sum of all items of a particular portion of the grid, update any of the grid index value etc It seems, the problem is no different than typical segment tree problem unlike the dimension of data container What can be a choice here is building a 2D Segment Tree The idea of 2D segment tree is nothing but the Quad Tree - A tree data structure in which each external node has exactly four children Quad trees are most often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions The regions may be square or rectangular or may have arbitrary shapes The data structure was named a quadtree by Raphael Frinkel and J L Bentley in 1974 A similar partitioning is also known as Qtree The root of the tree contains the full segments through [ (0, 0), (N - 1, N - 1) ] And for each segment [ (a1, b1), (a2, b2) ], we split them into [ (a1, b1), ( (a1 + a2) / 2, (b1 + b2) / ) ) ] , [ ( (a1 + a2) / + 1, b1 ), ( a2, (b1 + b2) / ) ], [ ( a1, (b1 + b2) / + ), ( (a1 + a2) / 2, b2 ) ] and [ ( (a1 + a2) / + 1, (b1 + b2) / + ), ( a2, b2 ) ] until a1 = b1 and a2 = b2 The cost of building a segment tree is O(nlogn) and with segment tree ready answering an RMQ (Range maximum/minimum query) can be done in O(logn) Suppose, you are given a grid with N = Then the corresponding tree will be - As you see, the * array [ (0, 0), (3, 3) ] is segmented into sub-arrays – [ (0, 0), (1, 1) ], [ (2, 0), (3, 1) ], [ (2, 0), (1, 3) ] and [ (2, 2), (3, 3) ] And further, each four chunks are segmented into four smaller units; For example segment [ (2, 2), (3, 3) ] will be [ (2, 2), (2, 2) ], [ (3, 2), (3, 2) ], [ (2, 3), (2, 3) ] and [ (3, 3), (3, 3) ] These segments are the smallest unit so no more further division Implementation The coding part is very similar to segment tree unlike the segmentation part The code given here is programming contest friendly (no pointer, memory allocation/deallocation stuff and OOP structure) and I've used this snippet many times in contests #include using namespace std; #define Max 506 #define INF (1 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 T[node] = maxNode(T[node], build(4 T[node] = maxNode(T[node], build(4 T[node] = maxNode(T[node], build(4 return T[node]; * * * * node node node node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / ) ); - 1, (a1 + a2) / + 1, b1, a2, (b1 + b2) / )); + 0, a1, (b1 + b2) / + 1, (a1 + a2) / 2, b2) ); + 1, (a1 + a2) / + 1, (b1 + b2) / + 1, a2, b2) ); } // 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 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 mx = maxNode(mx, update(4 mx = maxNode(mx, update(4 mx = maxNode(mx, update(4 return T[node] = mx; * * * * node node node node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2, x, y, value) ); - 1, (a1 + a2) / + 1, b1, a2, (b1 + b2) / 2, x, y, value)); + 0, a1, (b1 + b2) / + 1, (a1 + a2) / 2, b2, x, y, value)); + 1, (a1 + a2) / + 1, (b1 + b2) / + 1, a2, b2, x, y, value) ); } // 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; /* Drier program */ int main(void) { int n, m; // input scanf("%d %d", &n, &m); for(int i = 1; i m = m; build(1, 1, 1, n, m); } // build a 2D segment... 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... a1 > a2 or b1 > b2) return def(); // if it is within range, return the node if (x1

Ngày đăng: 04/02/2018, 22:43

Xem thêm:

TỪ KHÓA LIÊN QUAN

w