小Z的妹子(HYSBZ 2038)--莫队模板

叔叔传你过去
莫队模板题,直接上代码了

#include<bits/stdc++.h>
using namespace std;
#define maxn 50005
typedef long long ll;
ll c[maxn][3],ans,ans1;
int a[maxn];
int len;//每块的长度
struct query
{
    int L,R,ID,block;//起点,终点,查询编号,块号
    bool operator <(const query cur)const
    {
        if(cur.block==block) return R<cur.R;
        else return block<cur.block;
    }

}que[maxn];

inline void init()
{
    c[1][0]=c[1][1]=1;
    for(int i=2;i<maxn;i++){
            c[i][0]=1;
        for(int j=1;j<=2;j++){
            c[i][j]=c[i-1][j-1]+c[i-1][j];
        }
    }
}

int buf[maxn];
inline void inserts(int x)
{
   buf[x]++;
  ans=ans-c[buf[x]-1][2]+c[buf[x]][2];
}

inline void erases(int x)
{
     buf[x]--;
     ans=ans-c[buf[x]+1][2]+c[buf[x]][2];
}


inline ll gcd(ll a,ll b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}

int main()
{
    init();
    int n,m,l,r;
    scanf("%d%d",&n,&m);
    len=(int)sqrt(n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
     for(int i=1;i<=m;i++){
        scanf("%d%d",&l,&r);
        que[i].ID=i;
        que[i].block=l/len;
        que[i].L=l;
        que[i].R=r;
     }
     sort(que+1,que+1+m);
     memset(buf,0,sizeof buf);
    int L=1,R=0;
    ll up[maxn],down[maxn];
    ans=0;ans1=0;
    for(int i=1;i<=m;i++){
        while(R<que[i].R) inserts(a[++R]);
        while(L>que[i].L) inserts(a[--L]);
        while(L<que[i].L) erases(a[L++]);
        while(R>que[i].R) erases(a[R--]);
        ans1=c[que[i].R-que[i].L+1][2];
        ll gg=gcd(ans,ans1);
        up[que[i].ID]=ans/gg; down[que[i].ID]=ans1/gg;
     }
     for(int i=1;i<=m;i++) printf("%lld/%lld\n",up[i],down[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值