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

题解:

莫队

用组合数学算方案

又把计算顺序搞反了!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=60009;
const int SIZ=200;
typedef long long Lint;

int n,m;
int a[maxn];
int ql[maxn],qr[maxn],p[maxn];
Lint ans[maxn];
bool cmp(const int &rhs1,const int &rhs2){
    if(ql[rhs1]/SIZ==ql[rhs2]/SIZ){
        return qr[rhs1]<qr[rhs2];
    }else{
        return ql[rhs1]<ql[rhs2];
    }
}

Lint Gcd(Lint a,Lint b){
    if(b==0)return a;
    return Gcd(b,a%b);
}

int tong[maxn];

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    for(int i=1;i<=m;++i)scanf("%d%d",&ql[i],&qr[i]);
    for(int i=1;i<=m;++i)p[i]=i;
    sort(p+1,p+1+m,cmp);
    
    int p1=1,p2=0;
    Lint nowans=0;
    for(int i=1;i<=m;++i){
        int t=p[i];
        int l=ql[t],r=qr[t];
        while(p1<l){
            --tong[a[p1]];
            nowans-=tong[a[p1]];
            ++p1;
        }
        while(p1>l){
            --p1;
            nowans+=tong[a[p1]];
            ++tong[a[p1]];
        }
        while(p2<r){
            ++p2;
            nowans+=tong[a[p2]];
            ++tong[a[p2]];
        }
        while(p2>r){
            --tong[a[p2]];
            nowans-=tong[a[p2]];
            --p2;
        }
        ans[t]=nowans;
    }
    
    for(int i=1;i<=m;++i){
        Lint len=qr[i]-ql[i]+1;
        Lint d=Gcd(ans[i],(len-1)*len/2);
        printf("%lld/%lld\n",ans[i]/d,(len-1)*len/2/d);
    }
    return 0;
}

 

posted @ 2018-03-20 16:05  ws_zzy  阅读(145)  评论(0编辑  收藏  举报