HYSBZ 2038 small Z Socks (hose) (Mo Team Algorithm primer)

Source: Internet
Author: User
Tags gcd

Test instructions: Take an interval, the probability of taking two numbers in interval;

Idea: The probability p= (A * (A-1)/2+b* (B-1)/2+ ...) /(r-l+1) * (r-l)/2 simplified p= (a*a+b*b+......+z*z-(r-l+1))/(r-l+1) * (R-L);

The left endpoint of the inquiry interval is disposed in the same block, and every query in one block is processed, and for the same piece, the right endpoint is pressed for strict increment processing, and the left endpoint is constantly moving;

#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;intT,n,m,unit;inta[500010],num[500010];structnode{intId,l,r;} m[500010];intCMP (node A,node b) {if(A.l/unit!=b.l/unit)returna.l/unit<b.l/Unit; returna.r<B.R;}Long LonggcdLong LongALong Longb) {    if(b==0)returnA; returnGCD (b,a%b);}structque{Long Longb; voidReduce ()//score Numerator    {        Long LongD=gcd (A, b); A/=d;b/=D; }}ans[500010];voidWork () {intL=1, r=0; Long Longtemp=0; memset (num,0,sizeof(num));//the number of the same number in the interval     for(intI=0; i<m;i++)    {         while(r<M[i]. R) {R++; Temp-=(Long Long) num[a[r]]*Num[a[r]]; NUM[A[R]]++;//A[r] The number plus 1temp+= (Long Long) num[a[r]]*Num[a[r]]; }         while(l>M[i]. L) {L--; Temp-=(Long Long) num[a[l]]*Num[a[l]]; NUM[A[L]]++;//A[l] The number plus 1temp+= (Long Long) num[a[l]]*Num[a[l]]; }         while(r>M[i]. R) {Temp-=(Long Long) num[a[r]]*Num[a[r]]; NUM[A[R]]--;//A[l] The number minus 1temp+= (Long Long) num[a[r]]*Num[a[r]]; R--; }         while(l<M[i]. L) {Temp-=(Long Long) num[a[l]]*Num[a[l]]; NUM[A[L]]--;//A[r] The number minus 1temp+= (Long Long) num[a[l]]*Num[a[l]]; L++; } ans[m[i].id].a=temp-(r-l+1); ANS[M[I].ID].B=(Long Long) (r-l) * (r-l+1);    Ans[m[i].id].reduce (); }}intMain () {inti,j,k;  while(SCANF ("%d%d", &n,&m)! =EOF) {         for(i=1; i<=n;i++) scanf ("%d",&A[i]); Unit=(int) sqrt (n);//The total interval is divided into unit blocks         for(i=0; i<m;i++) {m[i].id=i; scanf ("%d%d", &m[i]. l,&M[i].        R); } sort (M,m+M,CMP);//sort the inquiryWork ();  for(i=0; i<m;i++) printf ("%lld/%lld\n", ans[i].a,ans[i].b); }    return 0;}

HYSBZ 2038 small Z Socks (hose) (Mo Team Algorithm primer)

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.