Segment tree interval update, interval summation, maximum value, minimum value template

Source: Internet
Author: User

Transfer from http://blog.csdn.net/acmer_ak/article/details/52002537

#include <string.h>#include<algorithm>#include<stdio.h>#include<math.h>#include<queue>#defineMAXN 100010#defineINF 0x3f3f3f3fusing namespacestd; structnode{intL,r;//interval [l,r]    intAdd//delay mark of interval    intSum//intervals and    intmx//Interval Maximum value    intMn//Interval Minimum value}tree[maxn<<2];//Be sure to open up to 4 times times more space.  voidPushup (intindex) {Tree[index].sum= tree[index<<1].sum+tree[index<<1|1].sum; Tree[index].mx= Max (tree[index<<1].mx,tree[index<<1|1].mx); Tree[index].mn= Min (tree[index<<1].mn,tree[index<<1|1].mn); }  voidPushdown (intindex) {      //indicates that the interval has been updated before//To update the sub-range below the interval, update the value of the interval last updated    if(Tree[index].add >0){          //Replace the original value        /*tree[index<<1].sum = (tree[index<<1].r-tree[index<<1].l+1) *tree[index].add;         Tree[index<<1|1].sum = (tree[index<<1|1].r-tree[index<<1|1].l+1) *tree[index].add;         tree[index<<1].mx = Tree[index].add;         tree[index<<1|1].mx = Tree[index].add;         tree[index<<1].mn = Tree[index].add;         tree[index<<1|1].mn = Tree[index].add;         Tree[index<<1].add = Tree[index].add;         Tree[index<<1|1].add = Tree[index].add; Tree[index].add = 0;*/          //based on the original value, add ValTree[index<<1].sum + = (tree[index<<1].r-tree[index<<1].l+1)*Tree[index].add; Tree[index<<1|1].sum + = (tree[index<<1|1].r-tree[index<<1|1].l+1)*Tree[index].add; Tree[index<<1].mx + =Tree[index].add; Tree[index<<1|1].mx + =Tree[index].add; Tree[index<<1].mn + =Tree[index].add; Tree[index<<1|1].mn + =Tree[index].add; Tree[index<<1].add + =Tree[index].add; Tree[index<<1|1].add + =Tree[index].add; Tree[index].add=0; }  }  voidBuildintLintRintindex) {TREE[INDEX].L=l; TREE[INDEX].R=R; Tree[index].add=0;//At first, we must clear 0 .    if(L = =R) {scanf ("%d",&tree[index].sum); Tree[index].mn= Tree[index].mx =tree[index].sum; return ; }      intMid = (l+r) >>1; Build (L,mid,index<<1); Build (Mid+1,r,index<<1|1);  Pushup (index); }  voidUpdata (intLintRintIndexintval) {      if(l <= tree[index].l && R >=TREE[INDEX].R) {          /*replace the original value with Val, because the interval has a number of tree[index].r-tree[index].l+1, so the interval and the maximum value are:*/          /*tree[index].sum = (tree[index].r-tree[index].l+1) *val;         Tree[index].mn = val;         tree[index].mx = val; Tree[index].add = val;//delay Mark*/          //add Val on the basis of the original value, because the interval has tree[index].r-tree[index].l+1//number, so the interval and the maximum value are:Tree[index].sum + = (tree[index].r-tree[index].l+1)*Val; Tree[index].mn+=Val; Tree[index].mx+=Val; Tree[index].add+ = Val;//delay Mark          return ;      } pushdown (index); intMid = (TREE[INDEX].L+TREE[INDEX].R) >>1; if(L <=mid) {Updata (L,r,index<<1, Val); }      if(R >mid) {Updata (L,r,index<<1|1, Val);  } pushup (index); }  intQueryintLintRintindex) {      if(l <= tree[index].l && R >=TREE[INDEX].R) {          //return tree[index].sum;         returntree[index].mx; //return tree[index].mn; } pushdown (index); intMid = (TREE[INDEX].L+TREE[INDEX].R) >>1; intAns =0; intMax =0; intMin =inf; if(L <=mid) {ans+ = Query (l,r,index<<1); Max= max (Query (l,r,index<<1), Max); Min= min (Query (l,r,index<<1), Min); }      if(R >mid) {ans+ = Query (l,r,index<<1|1); Max= max (Query (l,r,index<<1|1), Max); Min= min (Query (l,r,index<<1|1), Min); }      //return ans;     returnMax; //return Min; }  intMain () {intn,m,q,x,y,z;  while(~SCANF ("%d%d",&n,&m)) {Build (1N1);  while(m--) {scanf ("%d",&q); if(q = =1) {cout<<"query: (x, y)"<<Endl; scanf ("%d%d",&x,&y); cout<<query (x, Y,1) <<Endl; }              Else{cout<<"Update (x, y) is Z:"<<Endl; scanf (" %d%d%d",&x,&y,&z); Updata (x, Y,1, z);  for(inti =1; I <= N; ++i) {printf ("a[%d] =%d\n", I,query (I,i,1)); }              }          }      }      return 0; }

Segment tree interval update, interval summation, maximum value, minimum value 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.