Line Segment tree (recursive) template

Source: Internet
Author: User
Tags return tag

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.