Bzoj 2038 small Z socks (Mo team algorithm)

Source: Internet
Author: User
Tags gcd

MO Team Algorithm
If we know the answer to [l,r], we can get [L+1,r] 's answer in O (1) and the answer of [l,r-1], we can use the MO team algorithm. The time complexity is O (n^1.5). If the interval can only be moved at logn time, the time complexity is O (n^1.5*log N).
is to find a data structure to support the insertion, deletion, maintenance of the current answer.

It is easy to use an array to implement this problem, and to do O (1) transfer from [l,r] to [l,r+1] and [L+1,r].

So how does the MO team algorithm do? The following are the time complexities discussed on the basis of transfer to O (1). Also, because N and M are in the same order, write n uniformly.
If the answer to [L,r] is known, and the answer to [l ', R '] is required, we can easily get through the |l–l ' |+|r–r ' | sub-transfer.
Divides the number of n into sqrt (n) blocks.
Sort by interval, with the first keyword within the block of the left endpoint, and the right end as the second keyword, sorted by (POS [l],r)
And then by this sort of direct violence, the complexity analysis is like this:
1, I and i+1 in the same piece, r monotonically increment, so R is O (n). Because of the n^0.5 block, this part of the time complexity is n^1.5.
2, I and i+1 across a piece, r up to change N, because of the n^0.5 block, so this part of the time complexity is n^1.5
3, I and i+1 in the same piece of L change not more than n^0.5, across a piece will not exceed n^0.5, ignore * *. Because there are M queries (and N peers), the time complexity is n^1.5
So it's O (n^1.5).

# include <cstdio># include<cstring># include<cstdlib># include<iostream># include<vector># include<queue># include<stack># include<map># include<Set># include<cmath># include<algorithm>using namespacestd;# define Lowbit (x) ( x)& (-x)) # define PI3.1415926535# define EPS 1e-9# define MOD1000000009# define INF1000000000# define MEM (A, B) memset (A, B,sizeof(a) # define for (I,a,n) for(intI=a; i<=n; ++i) # define FO (I,a,n) for(intI=a; i<n; ++i) # define bug puts ("H"); # define LCH P<<1, l,mid# define RCH p<<1|1, mid+1, r# define MP make_pair# define PB push_backtypedef pair<int,int>pii;typedef Vector<int>vi;# pragma comment (linker,"/stack:1024000000,1024000000") typedefLong LongLL;intScan () {intres=0, flag=0; Charch; if((Ch=getchar ()) = ='-') flag=1; Else if(ch>='0'&&ch<='9') res=ch-'0';  while((Ch=getchar ()) >='0'&&ch<='9') res=res*Ten+ (ch-'0'); returnflag?-res:res;}voidOut (inta) {if(a<0) {Putchar ('-'); a=-A;} if(a>=Ten) Out (A/Ten); Putchar (A%Ten+'0');}Const intn=50005;//Code begin ...structquery{intL, R, id;} Node[n]; ll GCD (ll A, ll b) {returnb==0? A:GCD (b,a%b);}structans{LL A, B; voidReduce () {LL d=gcd (A, b); a/=d; b/=D;}} Ans[n];intA[n], num[n], N, M, unit;BOOLComp (query A, query B) {if(A.l/unit!=b.l/unit)returna.l/unit<b.l/Unit; returna.r<B.R;}voidWork () {LL temp=0; MEM (num,0); intL=1, r=0; FO (i,0, M) {         while(r<Node[i]. R) {++r; temp-= (LL) num[a[r]]*Num[a[r]]; ++num[a[r]]; temp+= (LL) num[a[r]]*Num[a[r]]; }         while(r>Node[i]. R) {Temp-= (LL) num[a[r]]*num[a[r]]; --Num[a[r]]; Temp+ = (LL) num[a[r]]*num[a[r]]; --s; }         while(l<Node[i]. L) {Temp-= (LL) num[a[l]]*num[a[l]]; --Num[a[l]]; Temp+ = (LL) num[a[l]]*num[a[l]]; ++m; }         while(l>Node[i]. L) {--l; temp-= (LL) num[a[l]]*Num[a[l]]; ++num[a[l]]; temp+= (LL) num[a[l]]*Num[a[l]]; } ans[node[i].id].a=temp-(r-l+1); ANS[NODE[I].ID].B= (LL) (r-l+1) * (rm);    Ans[node[i].id].reduce (); }}intMain () {scanf ("%d%d",&n,&m); For (I,1, N) scanf ("%d", A +i); FO (i,0, m) scanf ("%d%d", &node[i]. L,&node[i]. R), node[i].id=i; Unit=(int) sqrt (n); Sort (Node,node+M,comp);    Work (); FO (i,0, m) printf ("%lld/%lld\n", ans[i].a,ans[i].b); return 0;}
View Code

Bzoj 2038 small Z socks (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.