MO Team algorithm [country training team] small Z Socks

Source: Internet
Author: User

Topic Link Windam https://www.luogu.org/problemnew/show/P1494

Probably say what you understand.

The formula for calculating the probability first∑c ( 2,f (i ) )  /  c (2< Span id= "mathjax-span-89" class= "Mo" >,r l+ 1)    f (i) is the number of socks for each color in the interval

The last formula to be introduced is ∑f (i) *f (i)-(R? L+1)/C(2,r? ) L+1) (I don't know how to push it.) given by others);

The mere violence does not say O (n*n*n);

The Next and O (n*n);

Mo team is O (n*sqrt (n));

O (n*n) algorithm has two kinds of one is [l,r] interval with a small skill from the beginning to the end of the ans=10;

See the first 1 of us + 0; See the second 1 of US +1;-0; 31st; +3;-1;

every time ans1=ans1-sum[a[j]]* (sum[a[j]]-1);   sum[a[j]]++;   ans1=ans1+sum[a[j]]* (sum[a[j]]-1); The team also has this technique .

The second is the interval transfer from [A, b] to [P, Q] a-〉p b-q; Slightly smaller than O (n*n) According to the pre-order;

MO Team Algorithm

Each interval transfer can transfer n number

Mo team divides (1-n) into sqrt blocks each shift in each block up to sqrt (n) +n so finally sqrt (n) * (N+SQRT (n));

#include <bits/stdc++.h>#defineint long Longusing namespacestd;Const intmaxn=5e4+Ten;intN,m,unit,ans;intCOL[MAXN],BE[MAXN],SUM[MAXN];structMo {intL,r,id,a,b;} Q[MAXN];intSintx) {returnx*x;}BOOLCMP (Mo A,mo b) {returnbe[a.l]==be[b.l]?a.r<b.r:a.l<B.L;}BOOLCMP (Mo A,mo b) {returna.id<b.id;}voidRevise (intXintadd) {ans-=S (Sum[col[x]]); SUM[COL[X]]+=add; Ans+=S (Sum[col[x]]);} int32_t Main () {CIN>>n>>m; unit=sqrt (n);  for(intI=1; i<=n;i++) cin>>col[i],be[i]=i/unit+1;  for(intI=1; i<=m;i++) cin>>q[i].l>>q[i].r,q[i].id=i; Sort (q+1, q+m+1, CMP); intL=1; intR=0;  for(intI=1; i<=m;i++)    {       //cout<<q[i].l<< "" <<q[i].r<<endl;         while(L&LT;Q[I].L) Revise (l,-1), l++;  while(L&GT;Q[I].L) Revise (l1,1), l--;  while(R&LT;Q[I].R) Revise (r+1,1), r++;  while(R&GT;Q[I].R) Revise (r,-1), r--; if(Q[I].L==Q[I].R) {Q[i]. A=0; Q[i]. b=1;Continue;} Q[i]. A=ans-(q[i].r-q[i].l+1); Q[i]. B= (q[i].r-q[i].l+1) * (q[i].r-Q[I].L); intC=__GCD (Q[i]. A,q[i]. B); Q[i]. A=q[i]. A/C; Q[i]. B=q[i]. b/C; } sort (Q+1, q+m+1, CMP);  for(intI=1; i<=m;i++) printf ("%lld/%lld\n", Q[i]. A,q[i].    B); return 0;}

MO Team algorithm [country training team] small Z Socks

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.