题意:
中文题面
分析:
对于[L,R]的询问,设其中颜色为x,y,z,...的袜子的个数为a,b,c,...
那么ans(L,R)=C2a+C2b+C2c+...C2R−L+1=(a∗(a−1)/2+b∗(b−1)/2+c∗(c−1)/2+...)(R−L+1)∗(R−L)/2)
=a2+b2+c2+...−(a+b+c+...)(R−L+1)∗(R−L)=a2+b2+c2+...−(R−L+1)(R−L+1)∗(R−L)
所以这道题目的关键是求一个区间内每种颜色数目的平方和
关于莫队具体的可以看这里, 戳我查看
莫队均摊复杂度为O(n1.5),证明如下:
对于每一块内的所有询问,r是单调递增的,总变化范围为0∼n,这部分总复杂度为O(n);
l的变化范围为0∼n−−√,每块内平均有n−−√个询问,这部分复杂度为O(n−−√⋅n−−√)=O(n)
总共有n−−√块询问,总体均摊复杂度为O(n−−√⋅(n+n))=O(2nn−−√)=O(n1.5) ■
代码(区间表示为 [l,r) ):
//
// Created by TaoSama on 2016-01-25
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 5e4 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int B = 250;
typedef long long LL;
int n, q;
int c[N], cnt[N];
LL sum, up[N], dw[N];
struct Query {
int l, r, id;
bool operator< (const Query& q) const {
return r < q.r;
}
};
void update(int i, int delta) {
sum -= cnt[c[i]] * cnt[c[i]];
cnt[c[i]] += delta;
sum += cnt[c[i]] * cnt[c[i]];
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &q) == 2) {
for(int i = 1; i <= n; ++i) scanf("%d", c + i);
memset(cnt, 0, sizeof cnt);
vector<vector<Query> > Q(n / B + 2, vector<Query>());
for(int i = 1; i <= q; ++i) {
int l, r; scanf("%d%d", &l, &r);
Q[l / B].push_back(Query {l, ++r, i});
}
for(int i = 0; i <= n / B; ++i) sort(Q[i].begin(), Q[i].end());
sum = 0;
for(int i = 0; i < Q.size(); ++i) {
int l, r; l = r = i * B;
for(int j = 0; j < Q[i].size(); ++j) {
Query &q = Q[i][j];
while(r < q.r) update(r++, 1);
while(l < q.l) update(l++, -1);
while(l > q.l) update(--l, 1);
up[q.id] = sum - r + l;
dw[q.id] = 1LL * (r - l) * (r - l - 1);
LL g = __gcd(up[q.id], dw[q.id]);
up[q.id] /= g;
dw[q.id] /= g;
}
for(int j = l; j < r; ++j) update(j, -1);
}
for(int i = 1; i <= q; ++i) printf("%lld/%lld\n", up[i], dw[i]);
}
return 0;
}
本文介绍了如何使用莫队算法优化区间内颜色袜子数目平方和的计算过程,并详细解释了算法的具体实现和复杂度分析。
577

被折叠的 条评论
为什么被折叠?



