CF1638E Colorful Operations 题解

题目链接:CF 或者 洛谷

关于非随机数据的颜色均摊是 \(O(n)\) 的,在往期文章中已经提过。本题需要考虑一下关于操作二的一个影响怎么进行处理。其实这个东西我们可以类似标记永久化的思想去开一个数组记录 \(tag[color]\) 表示这种颜色最终需要加多少。

解下来我们要来考虑一个问题,如果一个位置 \(i\) 的颜色 \(x \rightarrow y\),那么再查询 \(i\) 处的值,由于它变成了 \(y\),那么最终意外地加上了 \(tag[y]\)。换句话来说,对于一种颜色 \(y\) 来说,当 \(x\) 变成 \(y\) 时并不需要加上原来操作的 \(tag[y]\) 作为答案,它相当于是重新开始计算新的 \(tag[y]\),但我们显然没办法单独为它开空间记录,但我们可以发现最终都是会加上 \(tag[y]\),这个 \(tag[y]\) 中会有多余贡献,这个多余贡献来自于当 \(x \rightarrow y\) 时,我们此时此刻新的 \(y\)\(tag[y]\) 应该从 \(0\) 开始,所以实际上,我们可以直接消这部分贡献 \(-tag[y]\),这样在最终都加上这个 \(tag[y]\) 时,这部分多余的部分影响就能被去掉了。

接下来继续关注 \(x \rightarrow y\) 这个变化,你会发现还是没正确。当 \(tag[x]\) 存在时,那么你这个位置的最终值其实是需要 \(+tag[x]\),但你由于变成了颜色 \(y\),导致你最终的值并不能加上这个时刻的 \(tag[x]\),那么其实当 \(x \rightarrow y\) 时,这个 \(i\) 位置应该还得 \(+tag[x]\),这样保证你最终查找才正确。

当然不可能暴力单点修改,注意到操作一本质是区间覆盖操作,根据非随机数据颜色均摊是 \(O(n)\) 的,我们可以对颜色段进行维护,这个可以 \(ODT\) 也可以线段树,线段树的维护方式就多维护一个标记,表示当前区间是否颜色都是相同的,不相同就往下找。对于一个颜色段 \(x\) 被覆盖为 \(y\),考虑颜色段的范围 \([l,r]+tag[x]\),最终变为了 \(y\) 再最终区间上去掉 \(tag[y]\)。关于查询是单点查询,那么我们只需要维护一个区间加与单点查询。这个我们可以可以用差分树状数组就能做到了。关于颜色段的维护我们这里使用更好写的 \(ODT\),线段树维护颜色段可以参照我的 U404643 帕鲁大陆染色的一天 题解

参照代码
#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;
#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 = 1e6 + 10;
ll bit[N], tag[N];
int n, q;

inline void add(int x, const ll val)
{
    for (; x <= n; x += lowBit(x))
        bit[x] += val;
}

inline void add(const int l, const int r, const ll val)
{
    add(l, val), add(r + 1, -val);
}

inline ll query(int x)
{
    ll ans = 0;
    for (; x; x -= lowBit(x))
        ans += bit[x];
    return ans;
}


struct ODT
{
    int l, r;
    mutable int val;

    bool operator<(const ODT& other) const
    {
        return l < other.l;
    }

    ODT(const int l, const int r, const int val) :
        l(l),
        r(r),
        val(val)
    {
    }
};

set<ODT> node;
typedef set<ODT>::iterator iter;

inline iter split(const int pos)
{
    auto it = node.lower_bound(ODT(pos, 0, 0));
    if (it != node.end() and it->l == pos)return it;
    --it;
    if (it->r < pos)return node.end();
    const auto [l,r,val] = *it;
    node.erase(it), node.insert(ODT(l, pos - 1, val));
    return node.insert(ODT(pos, r, val)).first;
}

inline void assign(const int l, const int r, const int val)
{
    const auto itr = split(r + 1), itl = split(l);
    for (auto it = itl; it != itr; ++it)
    {
        const auto [l,r,col] = *it;
        add(l, r, tag[col]);
    }
    node.erase(itl, itr), node.insert(ODT(l, r, val));
    add(l, r, -tag[val]);
}

string op;

inline void solve()
{
    cin >> n >> q;
    node.insert(ODT(1, n, 1));
    while (q--)
    {
        cin >> op;
        if (op == "Color")
        {
            int l, r, val;
            cin >> l >> r >> val;
            assign(l, r, val);
        }
        else if (op == "Add")
        {
            int col, val;
            cin >> col >> val;
            tag[col] += val;
        }
        else
        {
            int pos;
            cin >> pos;
            const int col = split(pos)->val;
            cout << query(pos) + tag[col] << 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(q\log{n}) \]

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