首页 > 其他分享 >P10268 符卡对决 题解

P10268 符卡对决 题解

时间:2024-03-23 21:24:32浏览次数:24  
标签:include return 符卡 int 题解 sum fa P10268 define

题目链接:符卡对决

视频讲解待上传

经典的题目,对于这个 \([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;
pii op[N];
ll curr;
int a[N], pos[N];
ll n;
int m, q, last;
stack<tuple<int, int, int, int, int>> del;

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

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

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

    ll 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]);
        fa[x] = y;
        siz[y] += siz[x];
        const ll res = sum[x] * sum[y] % MOD;
        sum[y] = (sum[x] + sum[y]) % MOD;
        return res;
    }
} dsu;

struct
{
    int fa[N];
    ll 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]);
    }

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

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];

ll ans[N];
ll tmpAns;

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 curr = (curr + dsu.merge(u, v, isBack)) % MOD;
}

inline void solve()
{
    read(n, m, q);
    const ll inv = qPow(n * (n - 1) / 2 % MOD, MOD - 2, MOD) % MOD;
    const int siz = m / sqrt(q);
    forn(i, 1, n) read(a[i]);
    forn(i, 1, m) read(op[i].first, op[i].second), pos[i] = (i - 1) / siz + 1;
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        read(l, r), id = i;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, n) tmp.fa[i] = i, tmp.sum[i] = a[i];
    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;
            tmp.init(L, R);
            tmpAns = 0;
            continue;
        }
        if (last != pos[L])
        {
            dsu.init();
            curr = 0;
            l = min(m, pos[L] * siz);
            r = l - 1;
            last = pos[L];
        }
        while (r < R) add(++r, false, false);
        ll preAns = curr;
        int tmpL = l;
        while (tmpL > L) add(--tmpL, false, true);
        ans[id] = curr * inv % MOD;
        curr = preAns;
        while (!del.empty())
        {
            const auto [x,faX,y,sizY,sumY] = del.top();
            dsu.fa[x] = x, dsu.siz[y] = sizY, dsu.sum[y] = sumY;
            del.pop();
        }
    }
    forn(i, 1, q) write(endl, ans[i]);
}

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}) \]

标签:include,return,符卡,int,题解,sum,fa,P10268,define
From: https://www.cnblogs.com/Athanasy/p/18091697

相关文章

  • CF1711B Party 题解
    CF1711BParty原题题意给定$n$个点带点权的无向图,点权$a_i$保证无重边自环,点权非负),要求删去一些点和它相连的边,使得剩下这个图的边数为偶数且删去点的点权之和最小。问删去点的点权之和最小是多少?分类讨论我们分类讨论一下。$m$为偶数,则不需要删边或点,直接输出$0$即......
  • 洛谷P3498 [POI2010] KOR-Beads 题解
    P3498[POI2010]KOR-Beads对于数列${A_i}$,求$k$使数列被分为$\lfloor\frac{n}{k}\rfloor$个部分后不同子数列种类最多(子串翻转前后为一类子串)很容易想到:枚举$k\\in\[1,n]$,记录每个$k$下不同种类数,然后取最优即可,那么问题来了:如何计算种类数?暴戾算法:一种纯......
  • 跳马【华为OD机试JAVA&Python&C++&JS题解】
    一.题目马是象棋(包括中国象棋和国际象棋)中的棋子,走法是每步直一格再斜一格,即先横着或直着走一格,然后再斜着走一个对角线,可进可退,可越过河界,俗称“马走‘日’字。给顶m行n列的棋盘(网格图),棋盘上只有有棋子象棋中的棋子“马”,并且每个棋子有等级之分,等级为k的马可以跳1~k......
  • CF710D Two Arithmetic Progressions 题解
    CF710DTwoArithmeticProgressions根号分治薄纱数论看日报学习的根号分治:暴力美学——浅谈根号分治-paulzrm的博客。开始想学ODT的映射思想的推广-金珂拉的博客,结果先学了ODT,又学了根号分治,才搞懂前置知识。什么是根号分治根号分治,是暴力美学的集大成体现。与......
  • [暴力题解系列]2023年蓝桥杯-子串简写
    ​ 大伙都说暴力是最低级的算法,啊那确实。但是好的暴力才是真正牛逼的骗分。咱就是说,暴力如何骗分呢?就是基于最暴力的算法一步步优化到能得更多分的暴力。子串简写这题,首先第一步就能想到一件事情:暴力枚举子串开头和末尾的位置,检查是否是符合题目要求的字符,如果是,并且长度大于......
  • 题解:AT_arc174_a [ARC174A] A Multiply
    题传。先要将\(C\)分类。\(C>0\),为了使答案更大,要乘上一个最大的区间和。\(C\le0\),为了使答案更大,选择乘上一个最小的区间和,因为此时我们可以贪心地想,如果区间和越小,乘上一个负数或\(0\)后,答案减少得越小,甚至乘上负数,还会使答案增大,所以也可以用负负得正来解释。当......
  • P8756 [蓝桥杯 2021 省 AB2] 国际象棋 题解
    设计状态什么的就不讲了,这里是对其它题解的优化。怎么优化呢,我们可以知道的是我们只要明确了当前行的状态,上一行的可选集就是知道的,如果我们明确了当前行以及上一行的状态,那么上上行的可选集就是知道的,于是我们就可以使用二进制子集枚举来写,这样就减去了全部不合法的枝叶,我们可以......
  • CF794F Leha and security system 题解
    题目链接:CF或者洛谷首先观察到题目的修改\(x\rightarrowy\),是每个位置的\(x\)都要变,那就显然的拆位去算每一位的贡献。当然,你又发现\(x\rightarrowy\),这玩意属于值为\(x\)的位变化成\(y\),那么这个和普通的拆位区别就在于这是维护值域维的拆位,我们拆位\(0\sim9\)......
  • [HDU5396] Expression 题解
    每次合并两个数,做过石子合并的人都能看出来是区间dp。设状态\(dp_{i,j}\)表示区间\([i,j]\)中合并为一个数的所有情况之和。那么我们就可以枚举断点\(k\):\(b_k\)为\(+\):\([i,k]\)中的每种情况都要和\([k+1,j]\)中的每种情况产生一个贡献,所以总贡献为\(dp_{i,k}\ti......
  • 2024年3月22号题解
    Fliptile解题思路对于这个题目可以用递推来写因为每次翻转只会影响一个十字架的区域,所以如果我们知道第一行的情况,那么是不是只要把第一行的所有的1在对应的下一个行的相同位置进行翻转就可以把第一行的所有的1变成0呢(重要性质) 那么只要我们不断递推下去就可以得到最后一......