BZOJ2038 2009国家集训队 小Z的袜子(hose) 题解&代码

26 篇文章 0 订阅
2 篇文章 0 订阅

莫队原例题【有气无力状】…手推那个O(1)的转移计算就行辣…
一眼看过去和上一道题差别不大…懒得写其它解释了
莫队详解参见上一题http://blog.csdn.net/rainbow6174/article/details/50858386
——————————分割线————————————
发出来之后代码格式错了orz心塞

/**************************************************************
    Problem: 2038
    User: Rainbow6174
    Language: C++
    Result: Accepted
    Time:1076 ms
    Memory:3048 kb
****************************************************************/

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
const int maxn = 50005;
int n, m, k, l, r, c[maxn], p[maxn], le[maxn], ri[maxn], len[maxn];
LL temp, ans[maxn], cnt[maxn], temp2;
bool cmp(int a,int b)
{
    if(len[a] == len[b]) return ri[a] < ri[b];
    return len[a] < len[b];
}
LL gcd(LL a, LL b)
{
    if(!b)return a;
    return gcd(b, a%b);
}
int main(void)
{
    scanf("%d%d",&n, &m);
    k = sqrt(n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &c[i]);
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d", &le[i], &ri[i]);
        p[i] = i;
        len[i] = (le[i]-1)/k+1;
    }
    sort(p, p+m, cmp);
    l = 1;
    for(int i = 0; i < m; i++)
    {
        while(l < le[p[i]]) cnt[c[l]]--, temp -= cnt[c[l]], l++;
        while(l > le[p[i]]) temp += cnt[c[--l]], cnt[c[l]]++;
        while(r < ri[p[i]]) temp += cnt[c[++r]], cnt[c[r]]++;
        while(r > ri[p[i]]) cnt[c[r]]--, temp -= cnt[c[r]], r--;
        ans[p[i]] = temp;
    }
    for(int i = 0; i < m; i++)
    {
        if(!ans[i]) printf("0/1\n");
        else temp2 = ((LL)(ri[i]-le[i]+1)*(LL)(ri[i]-le[i]))/2,
            temp = gcd(ans[i], temp2),
            printf("%lld/%lld\n",ans[i]/temp, temp2/temp);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值