Skip to content

Commit ac07d11

Browse files
committed
Implementation of simple and 2D segment tree
1 parent 2b76aed commit ac07d11

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<snippet>
2+
<content><![CDATA[
3+
// 2-D Segment tree
4+
#define N 1000
5+
#define M 1000
6+
7+
int t[4*N][4*M] = {0} ; // tree
8+
int a[N][M] = {0}; // n*m matrix
9+
int n,m;
10+
11+
12+
void buildy(int idx, int lx, int rx, int idy=1, int ly=0, int ry=m-1) // id starts from 1
13+
{ // [ly,ry]
14+
15+
if ( ly == ry )
16+
{
17+
if ( lx == rx)
18+
{
19+
t[idx][idy] = a[lx][ly] ;
20+
}
21+
else
22+
{
23+
t[idx][idy] = t[2*idx][idy] + t[2*idx+1][idy] ;
24+
}
25+
}
26+
else
27+
{
28+
int my = ly + (ry - ly)/2 ;
29+
buildy( idx, lx, rx, 2*idy, ly, my ) ;
30+
buildy( idx, lx, rx, 2*idy+1, my+1, ry ) ;
31+
t[idx][idy] = t[idx][2*idy] + t[idx][2*idy+1] ;
32+
}
33+
34+
}
35+
36+
void buildx(int idx=1, int lx=0, int rx=n-1) // id starts from 1
37+
{ // call buildx(), [l,r]
38+
if ( lx != rx )
39+
{
40+
int mx = lx + ( rx - lx )/2 ;
41+
buildx( 2*idx, lx, mx ) ; // [lx,mx] , left child
42+
buildx( 2*idx+1, mx+1, rx ) ; // [mx+1,rx] , right child
43+
}
44+
buildy( idx, lx, rx, 1, 0, m-1 ) ;
45+
}
46+
47+
48+
ll queryy(int idx, int idy , int tly , int tryy , int ly, int ry)
49+
{
50+
if ( ly > ry || ly > tryy || ry < tly ) // no overlap
51+
{
52+
return 0;
53+
}
54+
if ( ly <= tly && tryy <= ry ) // complete overlap
55+
{
56+
return t[idx][idy] ;
57+
}
58+
59+
int tmy = tly + (tryy-tly)/2 ;
60+
return queryy(idx, 2*idy, tly, tmy, ly, min(tmy,ry) ) + queryy(idx, 2*idy+1, tmy+1, tryy, max(ly,tmy+1), ry ) ;
61+
}
62+
63+
ll queryx( int lx, int rx, int ly, int ry , int idx=1 , int tlx=0 , int trx=n-1 ) // query in rectangle (lx,ly) to (rx,ry)
64+
{ // call queryx(x1,x2,y1,y2) where (x1,y1)=upper-left,(x2,y2)=lower-right cood.
65+
if ( lx > rx || lx > trx || rx < tlx ) // no overlap
66+
{
67+
return 0;
68+
}
69+
70+
if ( lx <= tlx && trx <= rx ) // complete overlap
71+
{
72+
return queryy(idx, 1, 0, m-1, ly, ry) ;
73+
}
74+
75+
int tmx = tlx + (trx-tlx)/2 ;
76+
return queryx( lx, min(rx,tmx), ly, ry, 2*idx, tlx, tmx ) + queryx( max(lx,tmx+1), rx, ly, ry, 2*idx+1, tmx+1, trx ) ;
77+
78+
}
79+
80+
void updatey( int x, int y, int value, int idx, int lx, int rx, int idy=1, int ly=0, int ry=m-1 )
81+
{
82+
if ( ly == ry )
83+
{
84+
if ( lx == rx )
85+
{
86+
t[idx][idy] = value ;
87+
}
88+
else
89+
{
90+
t[idx][idy] = t[2*idx][idy] + t[2*idx+1][idy] ;
91+
}
92+
}
93+
else
94+
{
95+
int my = ly + (ry - ly)/2 ;
96+
if ( y <= my )
97+
{
98+
updatey( x, y, value, idx, lx, rx, 2*idy, ly, my ) ;
99+
}
100+
else
101+
{
102+
updatey( x, y, value, idx, lx, rx, 2*idy+1, my+1, ry ) ;
103+
}
104+
105+
t[idx][idy] = t[idx][2*idy] + t[idx][2*idy+1] ;
106+
}
107+
108+
}
109+
110+
void updatex(int x, int y, int value, int idx=1, int lx=0, int rx=n-1) // call updatex(x,y,value) if a[x][y] changes to value.
111+
{
112+
if ( lx != rx )
113+
{
114+
int mx = lx + (rx-lx)/2 ;
115+
if ( x <= mx )
116+
{
117+
updatex( x, y, value, 2*idx, lx, mx ) ;
118+
}
119+
else
120+
{
121+
updatex( x, y, value, 2*idx+1, mx+1, rx ) ;
122+
}
123+
}
124+
125+
updatey( x, y, value, idx, lx, rx, 1, 0, m-1 ) ;
126+
}
127+
// assign values to n( no. of rows ), m( no. of columns ), array a! And then call build!
128+
]]></content>
129+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
130+
<tabTrigger>2d-segment-tree</tabTrigger>
131+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
132+
<!-- <scope>source.python</scope> -->
133+
</snippet>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<snippet>
2+
<content><![CDATA[
3+
// Classic Segment Tree
4+
void build(int id = 1,int l = 0,int r = n) //root index 1,children 2x,2x+1
5+
{ // root = [0,n)
6+
if( l+1 == r )
7+
{
8+
s[id] = a[l]; // put values in leaf nodes
9+
return; // comes from array
10+
}
11+
12+
ll mid = l + (r-l)/2;
13+
build( id*2, l, mid); // interval = [left,right)
14+
build( id*2+1, mid, r);
15+
16+
s[id]= [id * 2] + s[id * 2 + 1]; // put values in non-leaf nodes
17+
// comes from child nodes
18+
}
19+
20+
void modify(int p, int x, int id=1, int l=0, int r=n) // call modify(p,x)
21+
{
22+
//x is the value to which a[p] is changed
23+
s[id] += x-a[p] ; // change the value of the segtree node
24+
25+
if( l+1 == r){ // l=r-1=p
26+
a[p]=x; // change the value in the array
27+
return;
28+
}
29+
30+
ll mid = l + (r-l)/2;
31+
32+
if( p < mid )
33+
{
34+
modify(p, x, id*2, l, mid);
35+
}
36+
else
37+
{
38+
modify(p, x, id*2+1, mid, r);
39+
}
40+
41+
}
42+
43+
ll query(int x,int y,int id=1,int l=0,int r=n){ //verify return type
44+
// call query(l,r)
45+
if( x >= r or l >= y ) return 0; // no overlap
46+
47+
if( x <= l and r <= y ) return s[id]; // complete overlap
48+
49+
ll mid = l + (r-l)/2; // partial overlap
50+
return query(x, y, id*2, l, mid) + query(x, y, id*2+1, mid, r);
51+
52+
}
53+
54+
]]></content>
55+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
56+
<tabTrigger>segment-tree</tabTrigger>
57+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
58+
<!-- <scope>source.python</scope> -->
59+
</snippet>

0 commit comments

Comments
 (0)