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

    题目:

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038


    题解:

    开LongLong!!!!

    按照莫队的方法把询问拍个序,然后搞cnt数组统计每个颜色出现次数,用cur统计当前方案

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #define N 50010
    typedef unsigned long long ll;
    using namespace std;
    ll n,m,c[N],S,L,R;
    ll ans1[N],ans2[N],cnt[N],cur;
    struct node
    {
        ll l,r,bl,id;
        bool operator < (const node &x)const
        {
            return bl<x.bl || bl==x.bl && r<x.r;
        }
    }q[N];
    ll gcd(ll x,ll y)
    {
        return y?gcd(y,x%y):x;
    }
    int main()
    {
        scanf("%lld%lld",&n,&m);S=sqrt(n),cnt[0]=1;
        for (ll i=1;i<=n;i++)
        scanf("%lld",c+i);
        for (ll i=1,l,r;i<=m;i++)
        {
        scanf("%lld%lld",&q[i].l,&q[i].r);
        q[i].id=i;q[i].bl=(q[i].l-1)/S+1;
        }
        sort(q+1,q+1+m);
        for (ll i=1;i<=m;i++)
        {
        ll tl=q[i].l,tr=q[i].r;
        while (L<tl) cnt[c[L]]--,cur-=cnt[c[L]],L++;
        while (L>tl) L--,cur+=cnt[c[L]],cnt[c[L]]++;
        while (tr>R) R++,cur+=cnt[c[R]],cnt[c[R]]++;
        while (R>tr) cnt[c[R]]--,cur-=cnt[c[R]],R--;
        ans1[q[i].id]=cur;
        ans2[q[i].id]=(tr-tl)*(tr-tl+1LL)/2;
        }
        for (int i=1;i<=m;i++)
        {
        ll G=gcd(ans1[i],ans2[i]);
        printf("%lld/%lld
    ",ans1[i]/G,ans2[i]/G);
        }
        return 0;
    }
  • 相关阅读:
    从txt读取数据到Cvmat
    PCA之后进行归一化
    vc中调用exe文件并传递参数
    C#安装包过程
    电脑技巧
    DLL文件的创建与调用
    SVM调用方法
    舌顶上腭位置与作用
    KNN算法的實現
    How to Read a Paper
  • 原文地址:https://www.cnblogs.com/mrsheep/p/8184230.html
Copyright © 2011-2022 走看看