BZOJ2038[2009 Country Training Team] small Z socks (hose)

Source: Internet
Author: User
Tags gcd

BZOJ2038[2009 Country Training Team] small Z socks (hose)

Test instructions

The n socks are numbered from 1 to N, and each time you draw two from the socks numbered L to R, how much is the probability of pumping the same socks with the same color.

Exercises

Do not know what data structure to use, but can use a global array to save the current number of each color, so that by the interval [l,r] to launch [l,r±1] the answer and [l±1,r] the complexity of O (1), for this problem, can be the complexity of O (NSQRT (n)) of the MO team algorithm to solve.

The MO team algorithm is an off-line algorithm that sorts the queries in some order, making the averaging complexity O (nsqrt (n)), how to sort? If you sort by the left endpoint, then R will be possible to swing a large number of times, so that the complexity is degraded to O (n^2), the positive solution is the end of the block, so that the left end of the block as the first keyword sort, the right end of the second keyword sort. This way, when two queries L are in the same piece, L can only move sqrt (n) times. L in the same block of multiple queries Q can only shift n times, L in different blocks R may shift n times, but because only sqrt (n) block, so the operation to move N is only sqrt (n) times, so the averaging complexity is O (sqrt (n)). All the averaging complexity is metaphysics ...

Because the intermediate results are not forced into a long long,wa several hair, the weak is too weak!

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 #definell Long Long6 #defineInc (I,J,K) for (int i=j;i<=k;i++)7 using namespacestd;8 9 structnd1{Ten     intL,pl,r,id; ll ans; One }; A BOOLCmp1 (nd1 a,nd1 b) { -     if(a.pl!=b.pl)returna.pl<b.pl;if(A.R!=B.R)returna.r<B.R; -     returna.l<B.L; the } - BOOLCMP2 (nd1 a,nd1 b) { -     returna.id<b.id; - } +Nd1 a1[100000];intcol[100000],pos[100000],n,m,l,r;ll ans,s[100000]; -InlinevoidUpdateintXinty) { +ans-= (s[col[x]]* (s[col[x]]-1)); s[col[x]]+= (LL) y;ans+= (s[col[x]]* (s[col[x]]-1)); A } atll GCD (ll A,ll b) {returnb==0? A:GCD (b,a%b);} - intMain () { -scanf"%d%d", &n,&m); Inc (I,1, N) scanf ("%d", &col[i]);intSz= (int) sqrt (n); -Inc (I,1, n) pos[i]= (i-1)/sz+1; -Inc (I,1, M) { -         intA,B;SCANF ("%d%d", &a,&b); a1[i]= (nd1) {a,pos[a],b,i,0}; in     } -Sort (a1+1, a1+1+M,CMP1); L=1; R=0; ans=0; memset (s),0,sizeof(s)); toInc (I,1, M) { +          while(R&LT;A1[I].R) Update (r+1,1), r++; -          while(L&GT;A1[I].L) Update (L-1,1), l--; the          while(R&GT;A1[I].R) Update (r,-1), r--; *          while(L&LT;A1[I].L) Update (l,-1), l++; $a1[i].ans=ans;Panax Notoginseng     } -Sort (a1+1, a1+1+M,CMP2);  theInc (I,1, M) { +         if(a1[i].ans==0) printf ("0/1\n");Else{ All A2=GCD (A1[i].ans, (LL) (a1[i].r-a1[i].l+1) * (a1[i].r-a1[i].l)); theprintf"%lld/%lld\n", A1[I].ANS/A2, (LL) (a1[i].r-a1[i].l+1) * (A1[I].R-A1[I].L)/A2); +         } -     } $     return 0; $}

20160405

BZOJ2038[2009 Country Training Team] small Z socks (hose)

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.