BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队】

2038: [2009国家集训队]小Z的袜子(hose)

【题目描述】
传送门

【题解】

裸的莫队,套一下组合数就可以了。

代码如下

#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
#define LL long long
using namespace std;
int n,m,K,a[50005],hsh[50005];
LL FZ,FM,AnsFZ[50005],AnsFM[50005];
struct xcw{
    int L,R,id;
    bool operator <(const xcw b)const{return (L/K<b.L/K)||(L/K==b.L/K)&&(R<b.R);}
}Q[50005];
int read(){
    int ret=0;char ch=getchar();bool f=1;
    for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');
    for(; isdigit(ch);ch=getchar()) ret=(ret<<3)+(ret<<1)+ch-48;
    return f?ret:-ret;
}
LL gcd(LL x,LL y){if(y==0) return x;return gcd(y,x%y);}
void Add(int x){
    FM++;FZ-=(hsh[a[x]]-1)*hsh[a[x]]/2;
    hsh[a[x]]++;FZ+=(hsh[a[x]]-1)*hsh[a[x]]/2;
}
void Del(int x){
    FM--;FZ-=(hsh[a[x]]-1)*hsh[a[x]]/2;
    hsh[a[x]]--;FZ+=(hsh[a[x]]-1)*hsh[a[x]]/2;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    n=read(),m=read();K=sqrt(n);
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=m;i++) Q[i]=(xcw){read(),read(),i};
    sort(Q+1,Q+1+m);int L=1,R=0;
    for(int i=1;i<=m;i++){
        while(L<Q[i].L) Del(L++);
        while(L>Q[i].L) Add(--L);
        while(R<Q[i].R) Add(++R);
        while(R>Q[i].R) Del(R--);
        if(L==R){
            AnsFZ[Q[i].id]=0;AnsFM[Q[i].id]=1;
        }else{
            int t=gcd(FZ,(FM-1)*FM/2);
            AnsFZ[Q[i].id]=FZ/t;AnsFM[Q[i].id]=(FM-1)*FM/2/t;
        }
    }
    for(int i=1;i<=m;i++) printf("%lld/%lld\n",AnsFZ[i],AnsFM[i]);
    return 0;
}
posted @ 2018-06-16 09:40  XSamsara  阅读(131)  评论(0编辑  收藏  举报