Super Tree array of amputated segment tree

Source: Internet
Author: User

Do you hate the lengthy code of the line tree? Are you still full of ♂dark sweat because of the difficult debugging of line-segment trees? Well, please don't miss it! Super Tree Array Specials! As long as 998, as long as 998!

##¥......# ... ¥%......&%¥ ... er#%$#$#^t%$^$%

The Super tree array, in fact, is a kind of tree array which can support interval modification and interval query, and its constant is very small, does not need too much space and the code quantity is much less than the segment tree (amputated line segment tree)

1. Tree-like array

Since it is a super-tree array, you need a tree-like array as the basis. But in the real implementation, only the Lowbit () function is used (so lowbit is the core of the tree array)

2. Preparatory work

First, we need a difference score group.

Set a[] Array as the original array, then tree[] (Difference fractional group) is defined as tree[i]=a[i]-a[i-1]

A monkey can see it. Nature: A[i]=tree[1]+tree[2]+tree[3]+...+tree[i]

3. Interval query

(Why did you say the query first?) )

(1) query interval 1.....L and

SUM[L]=A[1]+A[2]+...+A[L]

where A[i]=tree[1]+...+tree[i]

Well, then we can get this equation very well.

T1+T1+T2+T1+T2+T3+....+T1+T2+T3+....+TL (What the hell?)

If you look at it mathematically, it looks like this.

t1*l+t2* (L-1) +t3* (L-2) +....+tl*1

If you're sitting next to a number of big-brother, Ta will look like this immediately.

l* (T1+T2+....+TL)-(t1*0+t2*1+...+tl* (L-1))

And we were surprised to find that these two parts are maintainable.

So we can process a difference score group and a tree1[i]=tree[i]* (i-1) at the input.

And then you can check it out.

(2) Query the L.....R and

Analogy prefixes and processing

(3) Code

Long LongGetsum (Long Long*arr,Long LongPOS) {    Long Longsum=0;  while(POS) sum+=arr[pos],pos-=Lowbit (POS); returnsum;}Long LongQueryLong LongXLong Longy) {    returnY*getsum (D1,y)-(x1) *getsum (d1,x-1)-(Getsum (d2,y)-getsum (d2,x-1));}

4. Interval modification

Interval modification of the analogy tree-like array

void Add (longlong *arr, Long Long pos, Long Long x) {    while (pos<=n) arr[pos]+=x,pos+=lowbit (POS);} 

However, due to the existence of tree and tree1, modifications need to be changed

void Change (long long L, Long Long R,longlong  x) {    Add (d1,l,x);    Add (d1,r+1,-x);    Add (d2,l,x* (l1));    Add (d2,r+1,-x*R);}

If the interval l-r plus x, it can be tree[l]+x,tree[r]-x, so that in the calculation of a[i] can make the number of L-r +x and others do not +x

Put code

#include <cstdio>#include<algorithm>//long Long tree[100001];Long Longn,m;Long Longc1|100001];Long Longc2|100001];inlineLong LongLowbit (Long Longx) {    returnx&-x;}/*void Add (Long long X,long long k)//μ¥μ? Dt??        {while (x<=n) {tree[x]+=k;    X+=lowbit (x); }}long long sum (Long long POS) {//????    2é?ˉlong long sum=0;        while (POS) {Sum+=tree[pos];        Pos-=lowbit (POS);    return sum; }} void Add_ex (Long long Pos,long long x) {//????     Dt??        while (pos<=n) {detla[pos]+=x;    Pos+=lowbit (POS);    }}void sum_ex (Long long L,long long R,long long x) {add_ex (l,x); Add (r+1,-x);}    Long Long sum_ex (Long long POS)//μ¥μ?2é?ˉ{long long sum=0;        while (POS) {Sum+=detla[pos];    Pos-=lowbit (POS); } return sum;}*///Ò??? Ê????? Dt?? +???? 2é?ˉvoidAddLong Long*arr,Long LongPosLong Longx) {     while(pos<=n) arr[pos]+=x,pos+=Lowbit (POS);}voidChangeLong LongLLong LongRLong Longx)    {Add (d1,l,x); Add (D1,r+1,-x); Add (d2,l,x* (L-1)); Add (D2,r+1,-x*r);}Long LongGetsum (Long Long*arr,Long LongPOS) {    Long Longsum=0;  while(POS) sum+=arr[pos],pos-=Lowbit (POS); returnsum;}Long LongQueryLong LongXLong Longy) {    returnY*getsum (D1,y)-(x1) *getsum (d1,x-1)-(Getsum (d2,y)-getsum (d2,x-1));}//Ò??? Ê?x?? μ/*void Build (Long long N) {for (long long i=1;i<=n;i++) {tree[i]=a[i];        Long Long t=lowbit (i);    For (long long j=1;j<t;j*=2) Tree[i]=std::max (Tree[i],tree[i-j]);    }}void Add (Long long Pos,long long x) {a[pos]=x;        while (pos<=n) {Tree[pos]=a[pos];        Long Long t=lowbit (i);        For (long long j=1;j<t;j++) {Tree[i]=std::max (tree[i],tree[i-j]);    } pos+=lowbit (POS);    }}long Long (long L,long long R) {long Long ans=a[r];        while (1) {Ans=std::max (ans,tree[r]);        if (r==l) break;r--;    while (R-l>=lowbit (R)) Ans=std::max (Ans,tree[r]), R-=lowbit (R); } return ans;*/ intMain ()//ê÷x′êyxé′ó?£°?{scanf ("%lld%lld",&n,&m); Long Longa,b=0;  for(Long LongI=1; i<=n;i++) {scanf ("%lld",&a); b=a-b;        Add (d1,i,b); Add (D2,i, (i-1)*b); b=A; }     while(m--){        Long Longop; scanf ("%lld",&op); if(op==1){//??? μ            Long Longx, Y, Z scanf ("%lld%lld%lld",&x,&y,&z);        Change (x, y, z); }Else{            Long Longx, y;//2é?ˉscanf"%lld%lld",&x,&y); printf ("%lld\n", query (x, y)); }    }}

5. Amputated Segment Tree

Now let's count the core code length of the Super Tree array

17 lines .... ~ ~ Line tree You can go to die ~ ~

Let's take a look at the time and space of the Super Tree array and the segment tree when running the template

OK segment tree You can really die on the spot ~

Amputated-tree array of segment tree

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.