zoukankan      html  css  js  c++  java
  • bzoj2038 [2009国家集训队]小Z的袜子(hose)——莫队

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2038

    莫队模板。不用特意分块来跑。连续走就行。

    推推式子有什么什么的平方的。写了一个交了。还是放上我最满意的吧。

    (就是加一个就多出原来cnt那么多对)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define ll long long
    using namespace std;
    const int N=5e4+5;
    int n,m,a[N],base,cnt[N];
    ll sum;
    struct Ques{
        int l,r,bh,id;
    }q[N];
    struct Dt{
        ll a,b;
    }ans[N];
    int rdn()
    {
        int ret=0;char ch=getchar();
        while(ch>'9'||ch<'0')ch=getchar();
        while(ch>='0'&&ch<='9')(ret*=10)+=ch-'0',ch=getchar();
        return ret;
    }
    ll rdl()
    {
        ll ret=0;char ch=getchar();
        while(ch>'9'||ch<'0')ch=getchar();
        while(ch>='0'&&ch<='9')(ret*=10)+=ch-'0',ch=getchar();
        return ret;
    }
    bool cmp(Ques a,Ques b){return a.bh==b.bh?a.r<b.r:a.bh<b.bh;}
    ll c(int k){return (ll)k*(k-1)/2;}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    Dt solve(int l,int r)
    {
        ll k=c(r-l+1),p=gcd(k,sum);
        Dt tmp;tmp.a=sum/p;tmp.b=k/p;return tmp;
    }
    void mdfy(int k,int t)
    {
        if(t==1)sum+=cnt[a[k]]++;
        else sum-=--cnt[a[k]];
    }
    int main()
    {
        n=rdn();m=rdn();base=sqrt(n);
        for(int i=1;i<=n;i++)a[i]=rdn();
        for(int i=1;i<=m;i++)
        {
            q[i].l=rdn();q[i].r=rdn();q[i].bh=(q[i].l-1)/base+1;q[i].id=i;
        }
        sort(q+1,q+m+1,cmp);
        for(int i=1,l=1,r=0;i<=m;i++)
        {
            for(;r<q[i].r;r++)mdfy(r+1,1);
            for(;l>q[i].l;l--)mdfy(l-1,1);
            for(;r>q[i].r;r--)mdfy(r,-1);
            for(;l<q[i].l;l++)mdfy(l,-1);
            ans[q[i].id]=solve(q[i].l,q[i].r);
        }
        for(int i=1;i<=m;i++)printf("%lld/%lld
    ",ans[i].a,ans[i].b);
        return 0;
    }
  • 相关阅读:
    23种设计模式
    Java实现动态代理的两种方式
    jQuery easyui combobox级联及内容联想
    使用Spring MVC统一异常处理
    springmvc下使用kaptcha做验证码
    Hadoop及spark介绍
    jQuery easyui 之 expend row
    Http 1.1协议
    公钥、私钥、CA认证、数字签名、U盾
    SOA架构介绍
  • 原文地址:https://www.cnblogs.com/Narh/p/9180395.html
Copyright © 2011-2022 走看看