Tree array interval Modify and query and

Source: Internet
Author: User

A little change in the difference score Group, you can achieve this Sao operation

First, let's take a look at the normal tree array (based on difference) how violent the solution interval is to ask interval length and

\ (\sum^{i=1}_{len}\sum^{j=1}_{i}base[j]\)Base is the original sequence

The above is the brute force solution, then we can find \ (base[i]\) was added \ (p-i+1\) times

So we can rewrite the formula to be the next

\ ((len+1) \sum^{i=1}_{len}-\sum^{i=1}_{len} (base[i]*i) \)

We use a tree-like array to maintain \ ((len+1) \sum^{i=1}_{len}\), and another maintenance \ (\sum^{i=1}_{len} (base[i]*i) \) .

#include <cstdio>#include <iostream>#include <algorithm>using namespaceStdLong Longbase_1[101000];Long Longbase_2[101000];intLenvoidInsertLong LongValintPOS) {intNow=pos; while(Now<=len)        {base_1[now]+=val;        Base_2[now]+=val*pos;    now+=now& (-now); }return;}Long LongSumintPOS) {Long Longres1=0, res2=0, Now=pos; while(now)        {Res1+=base_1[now];        Res2+=base_2[now];    now-=now& (-now); }returnRes1* (POS+1)-res2;}voidUpdata (Long LongValintPOS) {intNow=pos; while(Now<=len)        {base_1[now]+=val;        Base_2[now]+=val*pos;    now+=now& (-now); }return;}Long LongCheckintLintR) {returnSUM (r)-sum (l-1);}intMain () {intN,m; scanf"%d%d", &n,&m); Len=n;Long Longpa,pb=0; for(intI=1; i<=n;i++) {scanf ("%lld", &AMP;PA);        Updata (Pa-pb,i);    PB=PA; }intA,b,c; for(intI=1; i<=m;i++) {scanf ("%d%d%d", &a,&b,&c);if(a==1) {scanf ("%lld", &AMP;PA);            Updata (PA,B); Updata (-pa,c+1); }Else{printf ("%lld\n", check (b,c)); }    }}

Tree array interval Modify and query and

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.