洛谷 P1494 [国家集训队]小Z的袜子(经典莫队)

传送门


听说本题是莫队(提莫队长!?)创始人曾经出过的原版莫队题,在简单了解莫队后这题确实经典

题目大意

给出一个序列,然后有若干次查询,每次查询输出从一个区间 [ l , r ] [l,r] [l,r]内选出相同的两个数的概率是多少?

分析

假设区间内不同的数的个数分别为 a l , a l + 1 , a l + 2 , . . . , a r a_l,a_{l+1},a_{l+2},...,a_r al,al+1,al+2,...,ar,根据排列组合的思想,设区间长度为 l e n len len(实际上就是袜子的总数),答案很显然: ∑ i = l r C a i 2 C l e n 2 = ∑ i = l r ( a i 2 − a i ) l e n ∗ ( l e n − 1 ) , a i ≥ 2 \frac{\sum_{i=l}^rC_{a_i}^2}{C_{len}^2}=\frac{\sum_{i=l}^r(a_i^2-a_i)}{len*(len-1)},a_i \geq 2 Clen2i=lrCai2=len(len1)i=lr(ai2ai),ai2

但是我被 a i ≥ 2 a_i\geq2 ai2这个东西给难住了,调了好久很多 b u g bug bug也是没辙了。看了正解,才发现,即使一个数只有一个,那么它的贡献 a i ( a i − 1 ) = 0 a_i(a_i-1)=0 ai(ai1)=0,实际上只需要将这部分计入答案,故只是这个公式: ∑ i = l r a i 2 − l e n l e n ∗ ( l e n − 1 ) , l ≤ i ≤ r \frac{\sum_{i=l}^ra_i^2-len}{len*(len-1)},l \leq i \leq r len(len1)i=lrai2len,lir

那么对于上面的这个 ∑ i = l r a i 2 \sum_{i=l}^ra_i^2 i=lrai2,显然和这个小B的询问是一样的,可以每次减去原来的平方数再加上更新后的平方数;也可以根据 x 2 + 2 x + 1 = ( x + 1 ) 2 x^2+2x+1=(x+1)^2 x2+2x+1=(x+1)2每次加上或者减去 ( 2 x + 1 ) (2x+1) (2x+1)

//
// Created by Happig on 2020/8/24
//
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define lowbit(x) (x&(-x))
#define mkp(x, y) make_pair(x,y)
#define mem(a, x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double dinf = 1e300;
const ll INF = 1e18;
const int Mod = 1e9 + 7;
const int maxn = 2e5 + 10;

struct node {
    int l, r, id, blk;

    bool operator<(const node &p) const {
        return blk == p.blk ? r < p.r : blk < p.blk;
    }
} q[maxn];

int a[maxn];
ll cnt[maxn], fz[maxn], fm[maxn];
int n, m;
ll tot;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

inline void add(int pos) {
    tot += 2LL * (cnt[a[pos]]++) + 1;
}

inline void del(int pos) {
    tot -= 2LL * (cnt[a[pos]]--) - 1;
}

void solve() {
    int l = 1, r = 0;
    for (int i = 1; i <= m; i++) {
        int ql = q[i].l, qr = q[i].r, cur = q[i].id;
        while (l < ql) del(l++);
        while (l > ql) add(--l);
        while (r < qr) add(++r);
        while (r > qr) del(r--);
        int len = qr - ql + 1;
        if (len - 1 <= 0) fz[cur] = 0, fm[cur] = 1;
        else {
            ll up = tot - len, down = 1LL * len * (len - 1);
            if (!up) fz[cur] = 0, fm[cur] = 1;
            else fz[cur] = up, fm[cur] = down;
        }
    }
    for (int i = 1; i <= m; i++) {
        ll g = gcd(fz[i], fm[i]);
        cout << fz[i] / g << "/" << fm[i] / g << "\n";
    }
}

int main() {
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int sz = sqrt(n);
    for (int i = 1; i <= m; i++) {
        cin >> q[i].l >> q[i].r;
        q[i].id = i;
        q[i].blk = (q[i].l - 1) / sz + 1;
    }
    sort(q + 1, q + 1 + m);
    solve();
    return 0;
}

你可能感兴趣的:(算法进阶)