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", &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", &PA); Updata (PA,B); Updata (-pa,c+1); }Else{printf ("%lld\n", check (b,c)); } }}
Tree array interval Modify and query and