0% found this document useful (0 votes)
61 views1 page

Estrutura de Dados: Segment Tree 2D

This document describes how to implement a 2D segment tree data structure. It includes functions to build, query sums, and update values in the segment tree. The build functions recursively construct the segment tree from a 2D input matrix. The sum functions recursively query subtrees to calculate partial sums over a given rectangular region. The update functions similarly update values in the segment tree by recursively modifying subtrees.

Uploaded by

duhancaraciolo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views1 page

Estrutura de Dados: Segment Tree 2D

This document describes how to implement a 2D segment tree data structure. It includes functions to build, query sums, and update values in the segment tree. The build functions recursively construct the segment tree from a 2D input matrix. The sum functions recursively query subtrees to calculate partial sums over a given rectangular region. The update functions similarly update values in the segment tree by recursively modifying subtrees.

Uploaded by

duhancaraciolo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Estrutura de Dados: Segment Tree 2D

//a eh matriz, t eh segtree


//a[N][M], t[4*N][4*M]
void build_y (int vx, int lx, int rx, int vy, int ly, int ry) {
if (ly == ry) {
if (lx == rx) t[vx][vy] = a[lx][ly];
else t[vx][vy] = t[vx*2][vy] + t[vx*2+1][vy];
} else {
int my = (ly + ry) / 2;
build_y (vx, lx, rx, vy*2, ly, my);
build_y (vx, lx, rx, vy*2+1, my+1, ry);
t[vx][vy] = t[vx][vy*2] + t[vx][vy*2+1];
}
}
void build_x (int vx, int lx, int rx) {
if (lx != rx) {
int mx = (lx + rx) / 2;
build_x (vx*2, lx, mx);
build_x (vx*2+1, mx+1, rx);
}
build_y (vx, lx, rx, 1, 0, m-1);
}
int sum_y (int vx, int vy, int tly, int try_, int ly, int ry) {
if (ly > ry) return 0;
if (ly == tly && try_ == ry) return t[vx][vy];
int tmy = (tly + try_) / 2;
return sum_y (vx, vy*2, tly, tmy, ly, min(ry,tmy))
+ sum_y (vx, vy*2+1, tmy+1, try_, max(ly,tmy+1), ry);
}

int sum_x (int vx, int tlx, int trx, int lx, int rx, int ly, int ry) {
if (lx > rx) return 0;
if (lx == tlx && trx == rx)
return sum_y (vx, 1, 0, m-1, ly, ry);
int tmx = (tlx + trx) / 2;
return sum_x (vx*2, tlx, tmx, lx, min(rx,tmx), ly, ry)
+ sum_x (vx*2+1, tmx+1, trx, max(lx,tmx+1), rx, ly, ry);
}
void update_y (int vx, int lx, int rx, int vy, int ly, int ry, int x, int y, int
new_val) {
if (ly == ry) {
if (lx == rx)
t[vx][vy] = new_val;
else
t[vx][vy] = t[vx*2][vy] + t[vx*2+1][vy];
} else {
int my = (ly + ry) / 2;
if (y <= my)
update_y (vx, lx, rx, vy*2, ly, my, x, y, new_val);
else
update_y (vx, lx, rx, vy*2+1, my+1, ry, x, y, new_val);
t[vx][vy] = t[vx][vy*2] + t[vx][vy*2+1];
}
}
void update_x (int vx, int lx, int rx, int x, int y, int new_val) {
if (lx != rx) {
int mx = (lx + rx) / 2;
if (x <= mx)
update_x (vx*2, lx, mx, x, y, new_val);
else
update_x (vx*2+1, mx+1, rx, x, y, new_val);
}
update_y (vx, lx, rx, 1, 0, m-1, x, y, new_val);
}

You might also like