BZOJ 2308 小Z的袜子(hose) ( 莫队

模板题qwq
不过不会数学,,, 看似是概率题 其实就是统计区间 [L,R] [ L , R ] ,内部每个数字出现次数的平方

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 2e5+10;

struct node {
     int l, r, id;
}q[MAXN];
int sz, n, m;
int  cnt[MAXN*20], arr[MAXN];
ll res = 0, ans[MAXN];
ll a[MAXN], b[MAXN];
ll gcd(ll a, ll b) {
    if(b == 0)
        return a;
    return gcd(b, a%b);
}
ll cmp(const node &a, const node &b) {
     if(a.l/sz == b.l/sz) {
         return a.r < b.r;
     }
     return a.l < b.l;
 }
void add(int x) {
    res -= (ll)cnt[arr[x]]*cnt[arr[x]];
    cnt[arr[x]]++;
    res += (ll)cnt[arr[x]]*cnt[arr[x]];
}
void del(int x) {
    res -= cnt[arr[x]]*cnt[arr[x]];
    cnt[arr[x]]--;
    res += cnt[arr[x]]*cnt[arr[x]];
}
int main() {
    scanf("%d%d",&n,&m);
    sz = (ll)sqrt(n);
    for(int i = 1; i <= n; ++i)
        scanf("%d",&arr[i]);
    for(int i = 1; i <= m; ++i) {
        scanf("%d%d",&q[i].l, &q[i].r);
        q[i].id = i;
    }
    sort(q+1, q+1+m, cmp);
    ll L = 1, R = 0;
    for(int i = 1; i <= m; ++i) {
        while(L < q[i].l) {
            del(L++);
        }
        while(L > q[i].l) {
            add(--L);
        }
        while(R < q[i].r) {
            add(++R);
        }
        while(R > q[i].r) {
            del(R--);
        }
        int id = q[i].id;
        a[id] = res-(R-L+1);
        b[id] = ll(R-L+1)*(R-L);
        int k = gcd(a[id], b[id]);
        a[id] /= k;
        b[id] /= k;
    }
    for(int i = 1; i <= m; ++i) {
        cout << a[i] << "/" << b[i] << endl;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值