Tree-like array interval plus interval summation

Source: Internet
Author: User

Generally, a tree array is much better than a segment tree, but only for single-point modification.

Then recently learned a method of interval modification, interval plus interval summation.

Here we do not directly maintain the original array, but introduce another array b[i], which indicates the difference from the previous number.

In this case A[i] can be expressed as b[1]+b[2]+b[3]......b[i], the corresponding sum (i) is b[1]+b[1]+b[2]+b[1]+b[2]+b[3]......b[1]+b[2]+b[3]...b[i]= (n+1) *sum (b [i]) -sum (B[i]*i)

This translates into the prefix of the maintenance b[i] and the prefix and the b[i]*i. The modified interval [l,r], only b[l] and B[r+1] Two points will change, so a single point of modification two times can get positive solution.

The following is the processing interval update, sum code, a bit redundant, too lazy to change.

1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 6 Long Longn,m;7 Long Longc1[200011],c2[200011];8 Long Longa[200011],b[200011]; 9 Ten Long LongRead () One { A     Long Longx=0, f=1;CharC=GetChar (); -      while(!isdigit (c)) {if(c=='-') f=-1; c=GetChar ();} -      while(IsDigit (c)) {x=x*Ten+c-'0'; c=GetChar ();} the     returnx*F; - } -  - Long LongLowbit (Long Longx) + { -     returnx&-x; + } A  at voidUpdate1 (Long LongPosLong Longv) - { -      while(pos<=N) -     { -c1[pos]+=v;pos+=Lowbit (POS); -     } in } -  to voidUpdate2 (Long LongPosLong Longv) + { -      while(pos<=N) the     { *c2[pos]+=v;pos+=Lowbit (POS); $     }Panax Notoginseng } -  the Long LongQuery1 (Long LongPOS) + { A     Long Longsum=0; the      while(POS) +     { -Sum+=c1[pos];p os-=Lowbit (POS); $     } $     returnsum; - } -  the Long LongQuery2 (Long LongPOS) - {Wuyi     Long Longsum=0; the      while(POS) -     { WuSum+=c2[pos];p os-=Lowbit (POS); -     } About     returnsum; $ } -  - intMain () - { An=read (); +      for(Long LongI=1; i<=n;i++) thea[i]=read (); -b[1]=a[1]; $      for(Long LongI=2; i<=n;i++) b[i]=a[i]-a[i-1]; the      for(Long LongI=1; i<=n;i++) update1 (I,b[i]), Update2 (i,b[i]*i); them=read (); the      for(Long LongI=1; i<=m;i++) the     { -         Long LongD=read (), X=read (), y=read (), V; in         if(d==1) the         { theV=read (); update1 (x,v); Update1 (y+1,-V); Update2 (x,x*v); Update2 (y+1,-(y+1)*v); About         } the         Else the         { the             Long LongTmp1,tmp2;tmp1= (y+1) *query1 (y)-query2 (y); Tmp2=x*query1 (X-1)-query2 (x1); +cout << TMP1-TMP2 <<"\ n"; -         } the     }Bayi     return 0; the}

Tree-like array interval plus interval summation

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.