St Table and Tree array

Source: Internet
Author: User

St Table

 The St table can solve the problem of the interval maximum value. Can do O (nlogn) preprocessing, O (1) query, but does not support the modification.

the approximate idea of St table is to use st[i][j] to represent the maximum value of the J-Tree of 2 starting from I, when queried start with the left endpoint, find out how many times the interval length is 2, and then query. However, it is clear that the interval length we are querying is not necessarily the number of powers of 2. Then how to do O (1) query, this will use the most value of the characteristics.

, if we want to query the maximum value between 2 and 7, but 7-2+1 between 22 and 23, we choose 22, that is st[2][2], what about the rest of 6,7, we consider backwards from 7, that is, in st[7-22][2] and st[2][2. Take Max as the maximum value from 2 to 7.

First, preprocessing, St[i][j] represents the J of 2 from the beginning of I, then st[i][j] should be from the beginning of I 2 of the j-1 and i+2j-1 from the beginning of the j-1 of 2 of the maximum value, then the recursion is good.

Code:

1#include <cstdio>2#include <iostream>3 using namespacestd;4 Const intn=100100;5 intn,m,a[n],st[n][ -],log[n],cf[ -];6 voidPre ()7 {8log[2]=1;9log[1]=0;Ten    for(intI=3; i<=n;++i) One   { Alog[i]=log[i/2]+1; -   } -cf[0]=1; thecf[1]=2; -    for(intI=2; i<=log[n]+1;++i) -   { -cf[i]=cf[i-1]*2; +   } - } + intRead () A { at   intx=0, f=1;CharC=GetChar (); -    while(c<'0'|| C>'9') -   { -     if(c=='-') f=-1; -C=GetChar (); -   } in    while(c>='0'&&c<='9') -   { tox=x*Ten+c-'0'; +C=GetChar (); -   } the   returnx; * } $ intffintXinty)Panax Notoginseng { -   intl=y-x+1, k=Log[l]; the   intF=max (st[x][k],st[y-cf[k]+1][k]); +   returnF; A } the intMain () + { -N=read (), m=read (); $ pre (); $    for(intI=1; i<=n;++i) -   { -st[i][0]=a[i]=read (); the  -   }Wuyi  the    for(intj=1; j<=log[n];++j) -   { Wu      for(intI=1; i+cf[j]-1<=n;++i) -     { AboutSt[i][j]=max (st[i][j-1],st[i+cf[j-1]][j-1]); $     } -   } -    for(intI=1, x,y;i<=m;++i) -   { AX=read (), y=read (); +Cout<<ff (x, y) <<"\ n"; the   } -   return 0; $}

tree-like array

In fact, the principle of tree-like array I do not know very well, but because of its short refining code, so I like very much ....

So just by the way published in the blog, until a period of time, really understand its principle, and then back to modify.

The tree array does not require preprocessing, only the two operations of modifying and querying. The modification can be a value added or minus, and the query is an interval and.

First we need two arrays, one is the original array A, and the other is the tree-like array.

Then the modification, just write a few lines of the sub-function, and then modify the element's subscript and the element to be added into the function, and then the miracle happened.

Query incoming to query the subscript can be queried from 1 to change the interval between elements and, if the query from L to R, only need to find their range from to their interval and then subtract, similar to the prefix and. The specifics are in the code.

Oh, yes, the tree-like array also seems to support interval modifications and single-point queries, as well as when I do, and then come back to modify ...

1#include <cstdio>2#include <iostream>3 using namespacestd;4 Const intn=500005;5 inttree[n],n,m;6 voidAddintAintPOS)7 {8      while(pos<=N) {9tree[pos]+=A;Tenpos+=pos&-Pos; One     } A } - intccintPOS) - { the     intans=0; -      while(pos>=1){ -ans+=Tree[pos]; -pos-=pos&-Pos; +     } -     returnans; + } A intMain () at { -scanf"%d%d",&n,&m); -      for(intI=1, x;i<=n;++i) -     { -scanf"%d",&x); - Add (x,i); in     } -      for(intI=1, bz,x,y;i<=m;++i) { toscanf"%d%d%d",&bz,&x,&y); +         if(bz==1) - Add (y,x); the         Else{ *             intKK=CC (y), ZZ=CC (x1); $printf"%d\n", kk-ZZ);Panax Notoginseng         } -     } the     return 0; +}

St Table and Tree array

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.