P10268 符卡对决 题解

题目链接:符卡对决

视频讲解

经典的题目,对于这个 \([l,r]\) 询问,我们先关注期望怎么算。

考虑方案总数和有效的和,方案总数显然有 \(\dfrac{n\times (n-1)}{2}\),现在还需要关注有效和,我们关注对于若干个有效的关系用一个比较形象的数据结构表示-----并查集,那么两个卡牌之间有效就可以看做并查集的 \(merge\),现在我们来考虑合并两个并查集会有多少贡献:

对于并查集 \(x\)\(y\) 合并,枚举 \(x_i\) 与所有的 \(y_i\) 的贡献即为:\(x_i\times \sum y_i\),然后我们再将所有的 \(x_i\) 贡献加起来,那么它们合并的总贡献就为:\(\sum x_i \times \sum y_i\),所以并查集带权维护和即可算单个 \(merge\) 操作激活的贡献。

考虑是否好删除操作,不好删除,那么显然就是最好只加不减,那么我们如果对 \(m\) 个操作做回滚莫队维护查询即可。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c) if (b & 1) (ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-') sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char)) return;
    if (x < 0) x = -x, putchar('-');
    if (x > 9) write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow) return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3()
    {
        one = tow = three = 0;
    }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y) x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y) x = y;
}

constexpr int N = 2e5 + 10;
constexpr int MOD = 1e9 + 7;
ll n;
int m, q;
pii op[N];
int a[N];

//暴力用的并查集
struct
{
    int fa[N], sum[N];

    void init(const int l, const int r)
    {
        forn(i, l, r)
        {
            const auto [u,v] = op[i];
            fa[u] = u, fa[v] = v;
            sum[u] = a[u], sum[v] = a[v];
        }
    }

    int find(const int x)
    {
        return x == fa[x] ? x : fa[x] = find(fa[x]);
    }

    int merge(int x, int y)
    {
        x = find(x), y = find(y);
        if (x == y) return 0;
        int ans = 1LL * sum[x] * sum[y] % MOD;
        fa[x] = y, sum[y] = (1LL * sum[y] + sum[x]) % MOD;
        return ans;
    }
} tmp;

stack<tuple<int, int, int, int, int>> del;

struct
{
    int fa[N], sum[N], siz[N];

    void init()
    {
        forn(i, 1, n) fa[i] = i, siz[i] = 1, sum[i] = a[i];
    }

    int find(const int x)
    {
        return x == fa[x] ? x : find(fa[x]);
    }

    int merge(int x, int y, const bool isBack = false)
    {
        x = find(x), y = find(y);
        if (x == y) return 0;
        if (siz[x] > siz[y]) swap(x, y);
        if (isBack) del.emplace(x, fa[x], y, siz[y], sum[y]);
        int ans = 1LL * sum[x] * sum[y] % MOD;
        fa[x] = y, sum[y] = (1LL * sum[y] + sum[x]) % MOD;
        siz[y] += siz[x];
        return ans;
    }
} moDsu;

inline void back()
{
    while (!del.empty())
    {
        const auto [x,faX,y,sizY,sumY] = del.top();
        moDsu.fa[x] = faX, moDsu.siz[y] = sizY, moDsu.sum[y] = sumY;
        del.pop();
    }
}

int pos[N];

struct Mo
{
    int l, r, id;

    bool operator<(const Mo& other) const
    {
        return pos[l] ^ pos[other.l] ? pos[l] < pos[other.l] : r < other.r;
    }
} node[N];

int ans[N];
ll currAns, tmpAns;
int last; //上一次编号查询
inline void add(const int id, const bool isTmp, const bool isBack)
{
    const auto [u,v] = op[id];
    if (isTmp) tmpAns = (tmpAns + tmp.merge(u, v)) % MOD;
    else currAns = (currAns + moDsu.merge(u, v, isBack)) % MOD;
}

inline void solve()
{
    cin >> n >> m >> q;
    const ll inv = qPow(n * (n - 1) / 2, MOD - 2, MOD);
    forn(i, 1, n) cin >> a[i];
    const int siz = sqrt(m);
    forn(i, 1, m) cin >> op[i].first >> op[i].second, pos[i] = (i - 1) / siz + 1;
    moDsu.init();
    forn(i, 1, n) tmp.fa[i] = i, tmp.sum[i] = a[i];
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        cin >> l >> r, id = i;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        const auto [L,R,id] = node[i];
        if (pos[L] == pos[R])
        {
            forn(i, L, R) add(i, true, false);
            ans[id] = tmpAns * inv % MOD;
            tmpAns = 0;
            tmp.init(L, R);
            continue;
        }
        if (pos[L] != last)
        {
            moDsu.init();
            r = min(pos[L] * siz, m);
            l = r + 1;
            currAns = 0;
            last = pos[L];
        }
        while (r < R) add(++r, false, false);
        const ll preAns = currAns;
        int tmpL = l;
        while (tmpL > L) add(--tmpL, false, true);
        ans[id] = currAns * inv % MOD;
        back();
        currAns = preAns;
    }
    forn(i, 1, q) cout << ans[i] << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test) solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[时间复杂度为:\ O(m\sqrt{q}\times \log{n}) \]

posted @ 2024-03-23 21:10  Athanasy  阅读(12)  评论(0编辑  收藏  举报