-----chunking of the Law of deception | | Fan Line Tree Example a

Source: Internet
Author: User
Tags square root

What is chunking?

is a kind of magic algorithm that can help you cheat a lot of points.

There are several types of chunking, and I know that there are ① pretreatment ② not pretreatment

Non-preprocessed code I see a face.

So I'll talk about it here. Pre-processing version www

First look at a question:

Given a sequence containing n, the initial value is all 0, there are two operations for this sequence:
Operation 1: Change the given number of K1 to K2;
Action 2: Query from the number of K1 to the maximum number of K2. (k1<=k2<=n)

All the numbers are <=100000.

OK, if we make a traversal sure timeout to explode.

Then we're going to need a chunk of DAFA.

Divide the n number into blocks, and then each block calculates the maximum value and deposits the array.

This will only need to traverse the number of blocks in the query, efficiency + +

Here are some of the wonders of Zi Xuan's sister 's explanation:

Imagine that you are now the head of a project and you want to manage all your employees efficiently, then one of the easiest ways to think about it is to group them, and then set up a team leader for each group.

Now you have to ask the maximum of some people, so if all the employees in a group are within the scope of the inquiry, you just need to ask the team leader once to know who the most powerful employees are, not one, and if some of the groups are only a few of them are asked, then you need to ask these people one by one.

Now that we've moved this idea to a sequence, it's easy to think of splitting the adjacent elements into the same group, which is what we call chunking. So to ask for an interval, you just divide the interval into a number of complete blocks and record the maximum value of each block, as well as several separate elements at both ends. Give a chestnut: you have 20 elements, you divide them every four elements, which is divided into [1,4][5,8][9,12][13,16][17,20] five blocks, when you ask [6,18] This interval only need to ask 6th, 7, 8, 17, The maximum value of the 18 and fourth blocks, so that we eliminate one element that accesses the middle.

As for the modification operation is relatively simple, when modifying an element, if its value increases, then we determine whether it is larger than the previous maximum value, and when the modified element is the maximum value in the block and its value is also reduced is more troublesome, we can only pass through the whole block again to determine the maximum value.

At this point we will consider how to do the work more efficiently, if a block element too much then you want a one-way access to the element may be a lot (such as a large block only one element is not asked to that you have to access the entire block except for its elements), if a block element is too small then you may want to access many groups.

We assume that s is the size of the block, that is, each of the adjacent s elements is divided into pieces, then we can divide up to n/s+1 block, assuming that the number of blocks is C (obviously it is almost equivalent to N/S), and then see how our inquiry operation is carried out: access to all of the queried interval of the entire contained block, And both ends of the two elements that are not being questioned by the entire contained block (possibly not). In the first half we will access O (C) blocks, and the second half we will access O (S) elements. Then we'll look at the modification: in the worst case we need to access the entire block, which is O (S).

Because O (a+b) =o (A, b), and C is decremented with S, the best complexity is obtained when c=s, which is s=n^0.5. So one operation is the cost of O (n^0.5), the complexity of the algorithm is O (m*n^0.5), because M and n is the same order, so O (n^1.5) (read as Eugen) can also. We can see that only the square root can make the elements of the block and the number of blocks to achieve a balance, which is why the symbol is a square root element.

It is very simple to review the whole process: block the sequence and then break up an interval query into several blocks and several individual elements.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Then, the code for the example is ↓:

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cmath>5#include <cstring>6 using namespacestd;7 Const intmaxx=1000100, inf=1000000000;8 intn=0, s=0, c=0, k=0, q=0, w=0;9 intA[maxx],f[maxx];Ten intmx[ -],zuo[ -],you[ -]; One voidChu () A { -S= (int) sqrt (Double) n); -      for(intI=1; i<n;i+=s) the     { -f[i]=++C; -          for(intj=i+1; j<=i+s-1; j + +) -         { +f[j]=F[i]; -         } +zuo[c]=i; Ayou[c]=i+s-1; at     } -     if(s>0) -     if(n%s>0) -     { -zuo[c+1]=you[c]+1; -you[++c]=N; in          for(inti=zuo[c];i<=you[c];i++) f[i]=C; -     } to  + } - voidGaiintXintv) the { *     inty=F[x]; $a[x]=v;Panax Notoginseng         inttemp=-1*inf; -          for(inti=zuo[y];i<=you[y];i++) the             if(a[i]>temp) +temp=A[i]; A              themx[y]=temp; + } - intZuiintLintR) $ { $     intx=f[l],y=f[r],ans=-inf; -     if(x==y) -     { the          for(inti=l;i<=r;i++) -             if(a[i]>ans)Wuyians=A[i]; the             returnans; -     } Wu          for(inti=l;i<=you[x];i++) -             if(a[i]>ans) Aboutans=A[i]; $              for(inti=x+1; i<y;i++) -                 if(mx[i]>ans) -ans=Mx[i]; -                  for(inti=zuo[y];i<=r;i++) A                     if(a[i]>ans) +ans=A[i]; the     returnans; - } $ intMain () the { theMemset (A,0,sizeof(a)); theMemset (F,0,sizeof(f)); thememset (MX,0,sizeof(MX)); -Cin>>N; in Chu (); the      for(intI=1; i<=n;i++) the     { AboutCin>>k>>q>>W; the         if(k==1) gai (q,w); the         if(k==2) Cout<<zui (q,w) <<Endl; the     } +     return 0; -}

-----chunking of the Law of deception | | Fan Line Tree Example a

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.