• 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
    ",ans[i]/d,(len-1)*len/2/d);
        }
        return 0;
    }
    自己还是太辣鸡了
  • 相关阅读:
    Qt5.3.2(VS2010)_调试_进入Qt源码
    Qt5.3.2(VS2010)_调试_遇到的问题
    Qt_QString::split测试
    Qt_QString.indesOf和mid测试
    激活_目标窗口
    DrawDibDraw__ZC测试
    【转】DrawDibDraw
    数学运算_基本_01
    get和post请求及进程和线程及cookie和session的区别
    Redis性能优化之redis.cnf配置文件
  • 原文地址:https://www.cnblogs.com/zzyer/p/8609870.html
Copyright © 2020-2023  润新知