MO Team Algorithm

Source: Internet
Author: User

Principle

1, offline operation.

2, divided into a number of blocks, the interval is sorted by block first, the block is sorted by the right boundary of the interval. The block size is generally sqrt (n).

3, according to the sequence of the operation of the interval, continuous interval transfer, update the answer.

Topics

1, small z socks (hose) HYSBZ-2038

Test instructions: There are n socks, the probability of finding two socks with the same color in the interval.

Idea: for interval [l,r], the probability is sum (cnt[i]* (cnt[i-1]) |i∈val[l,.., R])/[(r-l+1) * (R-L)]. Update the molecule according to Mo team's thought.

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cmath>5 using namespacestd;6 Const intMAXN = 5e5 +Ten;7 Const intMAXM = 5e5 +Ten;8 intN, M;9 intUnit_len;Ten intBE[MAXN];//record the subscript L belongs to which sub-block One intVAL[MAXN]; A intCNT[MAXN]; - Long Longans; - Long LongANSA[MAXN],ANSB[MAXN];//answer A/b the structnode - { -     intL, R,id;//interval [l,r], input order -FriendBOOL operator< (ConstNODE&AMP;N1,Constnode&n2) +     { -         if(BE[N1.L] = = Be[n2.l])returnN1.R <N2.R; +         Else returnN1.L <N2.L; A     } at }QS[MAXM]; - Long LongGCD (Long LongALong Longb) - { -     if(A <b) Swap (A, b); -      while(b) -     { in         Long LongTMP = a%b; -A =b; tob =tmp; +     } -     returnA; the } * Long LongCalintx) $ {Panax Notoginseng     return1LL * x* (X-1); - } the voidUpdateintPosintv) + { AAns-=Cal (Cnt[val[pos]]); theCnt[val[pos]] + =v; +Ans + =Cal (Cnt[val[pos]]); - } $ voidSolve () $ { -     intL =1, r =0; -Ans =0; the      for(inti =1; I <= m; i++) -     {Wuyi         intID =qs[i].id; the          while(R < QS[I].R) Update (R +1,1), r++; -          while(R > QS[I].R) Update (R,-1), r--; Wu          while(L < QS[I].L) Update (L,-1), l++; -          while(L > QS[I].L) update (L-1,1), l--; About         if(ans = =0) Ansa[id] =0, Ansb[id] =1; $         Else -         { -Ansb[id]= Cal (QS[I].R-QS[I].L +1); -ansa[id]=ans; A             Long LongGCD =GCD (Ansa[id],ansb[id]); +Ansa[id]/=gcd; theAnsb[id]/=gcd; -         } $     } the } the intMain () the { the      while(~SCANF ("%d%d", &n, &m)) -     { inUnit_len =sqrt (n); the          for(inti =1; I <= N; i++) scanf ("%d", &val[i]); the          for(inti =1; I <= N; i++) Be[i] = I/unit_len +1; About          for(inti =1; I <= m; i++) scanf ("%d%d", &AMP;QS[I].L, &AMP;QS[I].R), qs[i].id=i; theSort (qs +1, QS +1+m); the solve (); the          for(inti =1; I <= m; i++) +         { -printf"%lld/%lld\n", Ansa[i], ansb[i]); the         }Bayi     } the  the  -     return 0; -}
View Code

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.