MO Team Algorithm Summary and template problem

Source: Internet
Author: User

was splay tortured for one weeks or copy template are WA, debugging when the data are found too large can not check the Konjac Konjac Yang Lan decided to learn MO team algorithm,

butnever thoughtthe team is full of pits, too .----------------------------------------------Preface 

Mo Team this thing is a very simple ♂-like rules of the thing, but also that will not always be, a look at the problem of the second understand, [although I see the puzzle is also half a day to understand but only the first problem, everything at the beginning difficult (perhaps)]


MO Team algorithm is a block-based off-line algorithm, mainly used to solve some of the interval of inquiry [is violence], we need to do is to save the query and then appropriate sorting, chunking, the content of similar queries together to focus on [using the data previously obtained to process the query] to save time ... Said it like not to say the same. But the principle of this thing is sauce.

We first sort by the left endpoint and then block.

Then sort by the right endpoint within each block.

then you can process it sequentially.  

Intra-block complexity:

Because the right endpoint in the block is incremented, the right endpoint changes O (n) times, and the left endpoint is not necessarily, so it is O (l*l) =o (n).

Inter-block jump: Brace dead O (n).

Total Complexity: O (n^1.5). ↑ A copy of something in the big god ppt ... It's 10,000 times times clearer than I wrote.
so the next question is the first one .Template title ...the procedure and the http://www.cnblogs.com/chadinblog/p/5898011.html←lzx of the Great God. The second question is also a meow here.title: Given a sequence of n and a positive integer, there are M queries asking for a given two number l,r each time, L≤i≤j≤r, and A (i) Xor A (i+1) xor A (i+2) XOR (=k) of the number of XOR A (j) i,j.  Input: Input data The first behavior n,m,k, meaning title is shown.
The second row n positive integers, and the number of I represents the term I of this series A.
The next m line, two integers per line, represents the L and r of the query operation.  output: The output data contains m lines, which represent the answers to each query. Sample Input6 2 31 2 1 1 0 31 63 5Sample Output70 first we need to recall the little bits and pieces of knowledge that have been forgotten. The important thing we needed was, XOR is its own inverse operation, A^b^a=b, A^a=0, 0^a=a;then we can use the array a[i] to store the prefix (XOR) and to handle this thing ....The following is an XOR of x to Y, meaning that a (x) x+1 xor A (x+2) xor ... xor a (y)↓ part of the code
1P= (int) sqrt (m*1.0);2      for(intI=1; i<=m/p;i++){3memset (CNT,0,sizeof(CNT));4Sort (e+ (i-1) *p+1, e+i*p+1, mycmp);5         Long LongL=0, r=0, ans=0;6cnt[0]++;7          for(intj= (I-1) *p+1; j<=i*p;j++){8              while(R&LT;E[J].Y) {r++,ans+=cnt[a[r]^k],cnt[a[r]]++;}9 /*from the nature of the XOR can be known if a[r]^k=z, then z^a[r]=k;Ten Cnt[z] refers to O∈ (L,r] (left open right closed because L declaration is 0 ...) Satisfies the number of XOR from 1th to o how many of Z's o ... Good trouble ... One so why is +cnt[z]? A because A[r] is XOR from 1 to R, if a[r]^ a 1 to O xor and =k, then the XOR from O to R is K*/ -              while(l<e[j].x-1) {Cnt[a[l]]--, ans-=cnt[a[l]^k],l++;} -              while(l>e[j].x-1) {L--, ans+=cnt[a[l]^k],cnt[a[l]]++;} thee[j].ans=ans; -         } -}
View Code

What a torture goblin! [Stick read]TBC The next 1p should be the socks that problem ...

MO Team Algorithm Summary and template problem

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.