BZOJ2038 [2009 Countries Training team] small Z socks (hose) (Mo team algorithm)

Source: Internet
Author: User
Tags gcd

The magic of the MO team algorithm, to solve the problem can be offline without modification of the interval query:

    • First, the original sequence is divided into blocks, √n block each block √n;
    • Then the interval [l,r] of all queries is sorted, first by the block ordinal in which L is in ascending order, if the same as in ascending order by R;
    • Finally, in order to find out the results of each query: Know [L,r] answer, and on this basis can be relatively fast in O (x) to get the adjacent interval [l+1,r], [L-1,r], [l,r-1], [l,r+1] answer, so that can be from [l,r] On the basis of the LR plus minus minus gets the answer of any one interval [l ', R '].

Seemingly violent, but the time complexity of doing so is O (x*n*√n)! Because:

    • L is arranged according to the number of the block, the same piece within a maximum of √n times ++l or--l to reach the target, a piece of up to about √n times plus minus, a total of √n block, so L change the number of times also √n*√n*√n.
    • R in the same piece is ascending, so the same piece up to n times ++r; the next block when R is assumed to reach the farthest in the last piece, that is up to N times--r back to the target, a total of √n blocks, so the R change number is at most (n+n) *√n.
    • The cost of the new answer is X, so the time complexity is O (x*n*√n)!

This problem, set each interval [l,r] Each color of the socks number is $a,b,c,d,\dots$, each interval [l,r] The answer is $ (c_a^2+c_b^2+c_c^2+c_d^2+\cdots)/c_{r-l+1}^2$, expand to simplify:

$$ (A^2+b^2+c^2+d^2+\cdots-a-b-c-d-\cdots)/((r-l+1) * (r-l+1-1)) $$

$$ (a^2+b^2+c^2+d^2+\cdots-(r-l+1))/((r-l+1) * (r-l)) $$

where $ (a^2+b^2+c^2+d^2+\cdots) $ can be used as the team algorithm to process the interval answer, open the number of sets of records ABCD ... can be transferred to an adjacent interval in O (1).

In addition, the case of l=r interval.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 #defineMAXN 555557 8 intBlock;9 structquery{Ten     intI,l,r; One     BOOL operator< (ConstQuery &q)Const{ A         if(L/block==q.l/block)returnr<Q.R; -         returnl/block<q.l/Block; -     } the }QUERY[MAXN]; -  - Long LonggcdLong LongALong Longb) { -     if(b==0)returnA; +     returnGCD (b,a%b); - } +  A intSEQ[MAXN]; at Long LongCNT[MAXN],ANSX[MAXN],ANSY[MAXN]; - voidInsertLong Long&res,inta) { -res-=cnt[a]*Cnt[a]; -++Cnt[a]; -res+=cnt[a]*Cnt[a]; - } in voidRemoveLong Long&res,inta) { -res-=cnt[a]*Cnt[a]; to--Cnt[a]; +res+=cnt[a]*Cnt[a]; - } the intMain () { *     intn,m; $scanf"%d%d",&n,&m);Panax Notoginsengblock=sqrt (n); -      for(intI=1; i<=n; ++i) scanf ("%d", seq+i); the      for(intI=0; i<m; ++i) { +Query[i].i=i; Ascanf"%d%d",&query[i].l,&QUERY[I].R); the     } +Sort (query,query+m); -     intL=1, r=1; $++cnt[seq[1]]; $     Long Longres=1; -      for(intI=0; i<m; ++i) { -         if(query[i].l==QUERY[I].R) { theansx[query[i].i]=0; ansy[query[i].i]=1; -             Continue;Wuyi         } the          while(l<QUERY[I].L) { - Remove (res,seq[l]); Wu++l; -         } About          while(l>QUERY[I].L) { $--l; - Insert (res,seq[l]); -         } -          while(r<QUERY[I].R) { A++R; + Insert (Res,seq[r]); the         } -          while(r>QUERY[I].R) { $ Remove (res,seq[r]); the--R; the         } the         Long Longa=res-(query[i].r-query[i].l+1), b= (QUERY[I].R-QUERY[I].L+1LL) * (QUERY[I].R-QUERY[I].L), c=gcd (b,a); theANSX[QUERY[I].I]=A/C; ansy[query[i].i]=b/C; -     } in      for(intI=0; i<m; ++i) printf ("%lld/%lld\n", Ansx[i],ansy[i]); the     return 0; the}

BZOJ2038 [2009 Countries Training team] small Z socks (hose) (Mo team algorithm)

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.