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;
}

转载于:https://www.cnblogs.com/XSamsara/p/9190114.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值