码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj2038 [2009国家集训队]小Z的袜子(hose)——莫队

时间:2018-06-13 23:28:22      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:space   ace   cstring   color   algorithm   efi   com   ++   原来   

题目: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\n",ans[i].a,ans[i].b);
    return 0;
}

 

bzoj2038 [2009国家集训队]小Z的袜子(hose)——莫队

标签:space   ace   cstring   color   algorithm   efi   com   ++   原来   

原文地址:https://www.cnblogs.com/Narh/p/9180395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!