1. 程式人生 > >bzoj2038 [2009國家集訓隊]小Z的襪子(hose)——莫隊

bzoj2038 [2009國家集訓隊]小Z的襪子(hose)——莫隊

col pac || CM mes 模板 rdl targe def

題目:https://www.lydsy.com/JudgeOnline/problem.php?id=2038

莫隊模板。不用特意分塊來跑。連續走就行。

推推式子有什麽什麽的平方的。寫了一個交了。還是放上我最滿意的吧。

(就是加一個就多出原來cnt那麽多對)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int N=5e4+5
; int n,m,a[N],base,cnt[N]; ll sum; struct Ques{ int l,r,bh,id; }q[N]; struct Dt{ ll a,b; }ans[N]; int rdn() { int ret=0;char ch=getchar(); while(ch>9||ch<0)ch=getchar(); while(ch>=0&&ch<=9)(ret*=10)+=ch-0,ch=getchar(); return ret; } ll rdl() { ll ret
=0;char ch=getchar(); while(ch>9||ch<0)ch=getchar(); while(ch>=0&&ch<=9)(ret*=10)+=ch-0,ch=getchar(); return ret; } bool cmp(Ques a,Ques b){return a.bh==b.bh?a.r<b.r:a.bh<b.bh;} ll c(int k){return (ll)k*(k-1)/2;} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} Dt solve(
int l,int r) { ll k=c(r-l+1),p=gcd(k,sum); Dt tmp;tmp.a=sum/p;tmp.b=k/p;return tmp; } void mdfy(int k,int t) { if(t==1)sum+=cnt[a[k]]++; else sum-=--cnt[a[k]]; } int main() { n=rdn();m=rdn();base=sqrt(n); for(int i=1;i<=n;i++)a[i]=rdn(); for(int i=1;i<=m;i++) { q[i].l=rdn();q[i].r=rdn();q[i].bh=(q[i].l-1)/base+1;q[i].id=i; } sort(q+1,q+m+1,cmp); for(int i=1,l=1,r=0;i<=m;i++) { for(;r<q[i].r;r++)mdfy(r+1,1); for(;l>q[i].l;l--)mdfy(l-1,1); for(;r>q[i].r;r--)mdfy(r,-1); for(;l<q[i].l;l++)mdfy(l,-1); ans[q[i].id]=solve(q[i].l,q[i].r); } for(int i=1;i<=m;i++)printf("%lld/%lld\n",ans[i].a,ans[i].b); return 0; }

bzoj2038 [2009國家集訓隊]小Z的襪子(hose)——莫隊