1 namespace seg {
2 struct segnode {
3 int S, E, Len, mid;
4 int min, tag;
5 segnode * l, * R;
6 segnode (int l, int R, int * base): S (L), E (R), Len (R-l ), mid (L + (LEN> 1 )){
7 tag = 0;
8 If (LEN = 1) {min = base [L], L = r = NULL; return ;}
9 L = new segnode (L, mid, base );
10 R = new segnode (MID, R, base );
11 min = min (L-> min, R-> min );
12}
13 ~ Segnode () {If (L! = NULL) delete L, delete R ;}
14 int getmin (int l, int R ){
15 if (L = S & R = e) return tag + min;
16 if (L> = mid) return tag + R-> getmin (L, R );
17 if (r <= mid) return tag + L-> getmin (L, R );
18 return tag + min (L-> getmin (L, mid), R-> getmin (MID, R ));
19}
20 void Update (INT Loc, int Val ){
21 if (LEN = 1 ){
22 min = val;
23 return;
24}
25 if (loc <mid) L-> Update (LOC, Val );
26 else R-> Update (LOC, Val );
27 min = min (L-> tag + L-> min, R-> tag + R-> min );
28}
29 void addrng (int l, int R, int K ){
30 if (L = S & R = e) {tag + = K; return ;}
31 if (L> = mid) r-> addrng (L, R, k );
32 else if (r <= mid) L-> addrng (L, R, k );
33 else l-> addrng (L, mid, k), R-> addrng (MID, R, k );
34 min = min (L-> tag + L-> min, R-> tag + R-> min );
35}
36 };
37 struct segtree {
38 segnode * root;
39 segtree (int * l, int * r) {root = new segnode (0, R-l, l );}
40 ~ Segtree () {Delete root ;}
41 int getmin (int l, int R) {return root-> getmin (L, R );}
42 void addrng (int l, int R, int K) {root-> addrng (L, R, k );}
43 };
44}
Line Segment tree (recursive) template