Sequence operation problem

Source: Internet
Author: User

Segment tree or tree-like array

1 Segment Tree2#include <stdio.h>3 intsum[100001];4 structnode5 {6     intV, a, B, left, right;7 };8Node tree[200100];9 inttot=0, D, K, S, T;Ten voidMaketree (intAintb) One { A     intNow ; -tot++; -now=tot; theTree[now].a=A; -tree[now].b=b; -tree[now].v=sum[b]-sum[a-1]; -     if(a<b) +     { -tree[now].left=tot+1; +Maketree (A, (a+b)/2); Atree[now].right=tot+1; atMaketree ((a+b)/2+1, b); -     } - } - voidChangeintR) - { -tree[r].v+=D; in     if(tree[r].left!=0&&tree[tree[r].left].a<=k&&tree[tree[r].left].b>=k) - Change (tree[r].left); to     if(tree[r].right!=0&&tree[tree[r].right].a<=k&&tree[tree[r].right].b>=k) + Change (tree[r].right); - } the intGetsum (intR) * { $     intLA, LB, RA, RB;Panax Notoginseng     inttot=0; -     if(s<=tree[r].a&&tree[r].b<=t)returntree[r].v; the     Else +     { ALa=tree[tree[r].left].a; thelb=tree[tree[r].left].b; +         if(! (la>t| | Lb<s)) tot=tot+getsum (tree[r].left); -Ra=tree[tree[r].right].a; $rb=tree[tree[r].right].b; $         if(! (ra>t| | Rb<s)) tot=tot+getsum (tree[r].right); -         returntot; -     } the } - intMain ()Wuyi { the     intN, I, M; -     Chartemp; Wuscanf"%d", &n); -scanf"%d", &sum[1]); About      for(i=2; i<=n; i++) $     { -scanf"%d", &sum[i]); -sum[i]+=sum[i-1]; -     } AMaketree (1, n); +scanf"%d", &m); the      for(i=1; i<=m; i++) -     { $ GetChar (); thetemp=GetChar (); the GetChar (); the GetChar (); the         if(temp=='S') -         { inscanf"%d%d", &s, &t); theprintf"%d\n", Getsum (1)); the         } About         if(temp=='A') the         { thescanf"%d%d", &k, &d); theChange1); +         } -     } the     return 0;Bayi}
Segment Tree
1 /*2 LRJ ' s code3 used to test it could AC4 5 */6#include <cstdio>7 using namespacestd;8 #defineMAXN 1000059 intC[MAXN];Ten intn,m,data,l,r,k; One CharT; AInlineintLowbit (inti) {returni& (-i);} - intMain () - { thescanf"%d",&n); -      for(intI=1; i<=n;++i) -     { -scanf"%d",&data); +          for(intJ=i;j<=n;j+=lowbit (j)) c[j]+=data; -     } +      for(SCANF ("%d", &m); m;--m) A     { att=GetChar (); -          while(t<'A'|| T>'Z') t=GetChar (); -         if(t=='A')  -         { -              while(t>='A'&&t<='Z') t=GetChar (); -scanf"%d%d",&k,&data); in              for(intJ=k;j<=n;j+=lowbit (j)) c[j]+=data; -         } to         Else +         { -              while(t>='A'&&t<='Z') t=GetChar (); theData=0; scanf ("%d%d",&l,&R); *              for(intJ=r;j;j-=lowbit (j)) data+=C[j]; $              for(intj=l-1; j;j-=Lowbit (j))Panax Notoginsengdata-=C[j]; -printf"%d\n", data);  the         } +     } A     return 0; the}
tree-like array

Sequence operation problem

Related Article

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.