"bzoj2038-Little Z socks" mo team algorithm

Source: Internet
Author: User
Tags gcd

Mo Team Example.

MO Team Study: https://www.cnblogs.com/Paul-Guderian/p/6933799.html

The subject molecule is Sigma (C (sum[a[i]],2)) and the denominator is Sigma (l-r+1,2); Maintain molecules and can.

MO Team Scope of application: offline, interval, transfer to the next block O (1).

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <cmath>7 using namespacestd;8 9 Const intn=50010;TentypedefLong LongLL; One intN,m,sq,a[n]; A LL Ans,gcd,sum[n]; - structnode{ -     intL,r,k,id; the LL FZ,FM; - }; - node B[n]; -  + intMaxxintXintY) {returnX>y?x:y;} - BOOLcmp1 (node X,node y) + { A     if(X.K==Y.K)returnx.r<Y.R; at     returnx.l<Y.L; - } - BOOLcmp2 (node X,node y) - { -     returnx.id<y.id; - } in voidRevise (intXintd) - { toans-=sum[a[x]]* (sum[a[x]]-1)/2; +sum[a[x]]+=D; -ans+=sum[a[x]]* (sum[a[x]]-1)/2; the } * ll GETGCD (ll A,ll b) $ {Panax Notoginseng     returnB?GETGCD (b,a%b): A; - } the  + intMain () A { theFreopen ("a.in","R", stdin); +Freopen ("a.out","W", stdout); -scanf"%d%d", &n,&m); sq=sqrt (n); $      for(intI=1; i<=n;i++) scanf ("%d",&a[i]); $      for(intI=1; i<=m;i++) -     { -scanf"%d%d",&b[i].l,&B[I].R); theb[i].k=b[i].l/sq; -B[i].id=i;Wuyi     } theSort (b +1, b+m+1, CMP1); -memset (SUM,0,sizeof(sum)); Wu     intL=1, r=1; sum[a[1]]=1; ans=0; -      for(intI=1; i<=m;i++) About     { $          while(L&LT;B[I].L) {Revise (l,-1); l++;} -          while(L&GT;B[I].L) {Revise (L-1,1); l--;} -          while(R&LT;B[I].R) {Revise (r+1,1); r++;} -          while(R&GT;B[I].R) {Revise (r,-1); r--;} AB[i].fz=ans;b[i].fm= (LL) (b[i].r-b[i].l+1)) * ((LL) (B[I].R-B[I].L))/2; +Gcd=GETGCD (b[i].fz,b[i].fm); theB[i].fz/=gcd;b[i].fm/=gcd; -         if(b[i].fz==0) b[i].fm=1; $     } theSort (b +1, b+m+1, CMP2); the      for(intI=1; i<=m;i++) the     { theprintf"%lld/%lld\n", b[i].fz,b[i].fm); -     } in     return 0; the}

"bzoj2038-Little Z socks" 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.