bzoj2038: [2009 countries Training team] small Z socks (hose)

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 #defineMAXN 500058typedefLong Longll;9ll ans=0, ANS[MAXN],BNS[MAXN];Ten intn,m,sum[maxn],a[maxn],block[maxn],temp; One structtsegment{ A     intId,l,r; - }QS[MAXN]; - voidprepare () { thetemp=sqrt (n); -      for(intI=1; i<=n;i++) block[i]= (i-1)/temp+1; - } - BOOLComp (tsegment x,tsegment y) { +     if(block[x.l]==BLOCK[Y.L]) { -         if(block[x.l]%2==1)returnx.r<Y.R; +         Else returnX.r>Y.R; A     } at     returnblock[x.l]<BLOCK[Y.L]; - } - voidChangeintZinty) { -     intx=A[z]; -ans-=1ll*sum[x]* (sum[x]-1); -sum[x]+=y; inans+=1ll*sum[x]* (sum[x]-1); - } to ll gcd (ll X,ll y) { +     if(y==0)returnx; -     returnGCD (y,x%y); the } * voidWork () { $ans=0;Panax Notoginseng      for(intL=1, r=0, i=1; i<=m;i++){ -          for(; qs[i].l<l;l--) Change (L-1,1); the          for(; qs[i].l>l;l++) Change (l,-1); +          for(; qs[i].r>r;r++) Change (r+1,1); A          for(; qs[i].r<r;r--) Change (r,-1); theans[qs[i].id]=ans; +     } -      for(intI=1; i<=m;i++){ $         if(ans[i]!=0){ $ll x=gcd (Ans[i],bns[i]); -printf"%lld/%lld\n", ans[i]/x,bns[i]/x); -}Else{ theprintf"0/1\n"); -         }Wuyi     } the } - intMain () { Wuscanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++) scanf ("%d",&a[i]); About      for(intI=1; i<=m;i++) scanf ("%d%d", &AMP;QS[I].L,&AMP;QS[I].R), qs[i].id=i; $      for(intI=1; i<=m;i++) bns[i]=1ll* (qs[i].r-qs[i].l+1) * (qs[i].r-qs[i].l); - prepare (); -Sort (qs+1, qs+m+1, comp); - Work (); A     return 0; +}
View Code

Title Link: http://www.lydsy.com/JudgeOnline/problemset.php

The main topic: see the problem surface.

Procedure: Mo team algorithm.

bzoj2038: [2009 countries 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.