(pits) tree-like array 1

Source: Internet
Author: User

Remember that year tangled in the structure of the first half of the tree-like array ...

A perpetual old pit

To be honest, I haven't done this before. Tree-like array code

--------------------------------------Split Line--------------------------------------

Go to the Chase (P.S. All definitions are reproduced from the learning blogger, at the end of the link)

The most common tree-like array, using the Lowbit binary correspondence method, support single-point modification, 1-i query, but this application is too small 2333

#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#defineN 3010using namespacestd;intQ,n,a[n];inlineintLowbit (intx) {    return(x&-x);} InlinevoidAddintXintdel) {     for(inti=x;i<=n;i+=lowbit (i)) A[i]+=del;} InlineintSum_ (intx) {    intsum=0;  for(inti=x;i;i-=lowbit (i)) sum+=A[i]; returnsum;} InlinevoidJimmy () {scanf ("%d%d",&n,&q);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]);  for(intI=1, type;i<=q;i++) {scanf ("%d",&type); if(type==1){            intX,del; scanf ("%d%d",&x,&del);        Add (X,del); }        if(type==2){            intx; scanf ("%d",&x); printf ("%d\n", Sum_ (x))); }    }}intMain () {Jimmy (); return 0;} 
single-point modification 1-i query

So we're going to show you how to do interval changes and interval queries.

Interval modification:

We assume that Sigma (R,i) represents the first I term of the r array and that the complexity of the call is log2 (i)

Set the original array is a[n], Difference fractional group C[n],c[i]=a[i]-a[i-1], then obviously A[i]=sigma (c,i), if you want to modify A[i] to A[j] (such as +v), just make C[i]+=v,c[j+1]-=v

Interval query:

In a tree-based array of basic operations: single-point modification, interval query, we can do this

First observe

A[1]+a[2]+...+a[n]

= (C[1]) + (c[1]+c[2]) + ... + (C[1]+c[2]+...+c[n])

= N*c[1] + (n-1) *c[2] + ... +c[n]

= n * (C[1]+c[2]+...+c[n])-(0*c[1]+1*c[2]+...+ (n-1) *c[n]) (Formula ①)

Then we'll maintain an array c2[n], where c2[i] = (i-1) *c[i]

Whenever you modify C, you modify the C2, so that the complexity does not change

So

Formula ①

=n*sigma (C,n)-Sigma (C2,n)

So we did it. Complete the interval and query in O (LOGN) time

Wouldn't it be wonderful?

#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#defineN 3010using namespacestd;intN,q,num[n],c1[n],c2[n];inlineintLowbit (intx) {    return(x&-x);} InlineintAddint*r,intUintdel) {     for(inti=u;i<=n;i+=lowbit (i)) R[i]+=del;} InlineintSum_ (int*r,intv) {    intsum=0;  for(intI=v;i;i-=lowbit (i)) sum+=R[i]; returnsum; }inlinevoidJimmy () {scanf ("%d%d",&n,&q);  for(intI=1; i<=n;i++) {scanf ("%d",&Num[i]); Add (C1,i,num[i]-num[i-1]); Add (C2,i, (i-1) * (num[i]-num[i-1])); }     for(intI=1, type;i<=q;i++) {scanf ("%d",&type); if(type==1){            intu,v; scanf ("%d%d%d",&u,&v,&W); Add (c1,u,w); Add (C1,v+1,-W); Add (C2,u, (U-1) *w); Add (c2,v+1, v* (-W)); }        if(type==2){            intu,v; scanf ("%d%d",&u,&v); intsum1= (U1) *sum_ (c1,u-1)-sum_ (c2,u-1);//Sigma (U-1)            intSum2=v*sum_ (c1,v)-sum_ (C2,V);//Sigma (v)printf"%d\n", sum2-sum1); }    }}intMain () {Jimmy (); return 0;}
interval modification interval query

Learning Blog && information Source: http://blog.csdn.net/fsahfgsadhsakndas/article/details/52650026

(pits) tree-like array 1

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.