bzoj2038 Little Z's socks

Source: Internet
Author: User
Tags gcd

Using the plane Manhattan distance minimum spanning tree or MO team algorithm can be acridine qwq~

But apparently the latter better write (escape ~)

Mo team how to write, look at the picture bar qwq~

So I didn't open a long and then took 3000 groups without making a mistake and handed it up to WA Qaq

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#defineint long Longusing namespacestd;Const intmx=50010;structNode {intL,r,num;} QUE[MX];BOOLCMP1 (Node A,node b) {returna.l<B.L;}BOOLCMP2 (Node A,node b) {returna.r<B.R;}intn,m,c[mx],num[mx],ans[mx],ans1[mx][2];inlineintGCD (intAintb) {inttmp while(a>0) tmp=b%a,b=a,a=tmp;returnb;} Signed Main () {scanf ("%lld%lld",&n,&m);  for(intI=1; i<=n;i++) scanf ("%lld",&C[i]);  for(intI=1; i<=m;i++) {scanf ("%lld%lld", &AMP;QUE[I].L,&AMP;QUE[I].R);if(que[i].l>que[i].r) Swap (QUE[I].L,QUE[I].R); }     for(intI=1; i<=m;i++) que[i].num=i; Sort (que+1, que+1+M,CMP1);  for(intI=1; I<=m;i+=sqrt (m)) sort (Que+i,que+min (m,i+ (int) sqrt (m)), CMP2);  for(intI=1; i<=m;i++)    {        if(i% (int) sqrt (m) = =1|| i==1) {memset (num,0,sizeof(num));  for(intj=que[i].l;j<=que[i].r;j++) num[c[j]]++;  for(intj=0; j<=n;j++) ans[i]+=num[j]* (num[j]-1)/2; }        Else        {             for(intj=que[i-1].l,to=que[i].l;j!=to ;) {                if(j<to) ans[i]-=num[c[j]]-1, Num[c[j]]--, j + +; Elseans[i]+=num[c[j-1]],num[c[j-1]]++,j--; }             for(intj=que[i-1].r,to=que[i].r;j!=to ;) {                if(j<to) ans[i]+=num[c[j+1]],num[c[j+1]]++,j++; Elseans[i]-=num[c[j]]-1, Num[c[j]]--, j--; } Ans[i]+=ans[i-1]; }            intDIV=GCD (Ans[i], (que[i].r-que[i].l+1) * (QUE[I].R-QUE[I].L)/2); if(que[i].r==que[i].l| | ans[i]==0) ans1[que[i].num][0]=0, ans1[que[i].num][1]=1; Elseans1[que[i].num][0]=ans[i]/div,ans1[que[i].num][1]= (que[i].r-que[i].l+1) * (QUE[I].R-QUE[I].L)/(div*2); }     for(intI=1; i<=m;i++) printf ("%lld/%lld\n", ans1[i][0],ans1[i][1]); return 0;}

bzoj2038 Little Z's 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.