U404643 帕鲁大陆染色的一天 题解

题目链接:帕鲁大陆染色的一天

注意到每种颜色是独立的,所以我们能够比较容易地得知每种颜色在一系列操作中的出现和消失时间。我们注意到每个操作加入以后会有两个影响,对它后面的操作显然颜色总数都会 \(+1\),对前面操作的影响来说,显然会覆盖掉某些颜色,导致某些颜色消失,换句话来讲消除了前面操作对后面操作的影响 \(1\)

基于上述的过程,其实这是在时间轴的操作影响,并且询问也是基于时间轴,所以我们可以在时间轴上维护一个树状数组用于维护每个操作的影响即:

  1. 前面的操作加入以后会使得当前时间点以后的所有操作对应的时间点颜色数 \(+1\)

  2. 可能在当前时间点的时候应该去除前面的某个操作对后面操作的影响。

没看懂意思的可以看这个:P8512 [Ynoi Easy Round 2021] TEST_152 题解

那么基于扫描线离线操作下来,从左往右不断加入某个操作,由于需要考虑颜色何时删除,所以我们需要为每个颜色或者说是时间点维护一个桶表示颜色总数,当清空时它对应的时间点贡献也就没了。而清空一个区间的所有连续颜色段的贡献我们可以考虑 线段树 或者 珂朵莉树 来实现。最后,判断的时候注意判断掉颜色为 \(0\) 的点,不考虑这部分贡献。

线段树做法,具体的维护这个区间是否是连续段,是连续段的话颜色或者说时间点是啥,本题颜色和时间点其实是一致的。然后支持区间覆盖,所以需要一个覆盖标记。当左右子树皆为连续段且颜色相同就能归并为一个新的连续段区间。删除贡献时不为连续段就往下递归就行了,复杂度分析参照珂朵莉树的均摊分析。

线段树做法
#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 = 1e6 + 10;

int n, m, q;

int cnt[N];
int bit[N];

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

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

struct Node
{
    bool isSame = true;
    int tim;
    int cov;
} node[N << 2];

#define isSame(x) node[x].isSame
#define tim(x) node[x].tim
#define cov(x) node[x].cov

inline void Cover(const int curr, const int val)
{
    tim(curr) = val;
    cov(curr) = val;
    isSame(curr) = true;
}

inline void push_down(const int curr)
{
    if (cov(curr))
    {
        Cover(ls(curr),cov(curr));
        Cover(rs(curr),cov(curr));
        cov(curr) = 0;
    }
}

inline void push_up(const int curr)
{
    if (isSame(ls(curr)) and isSame(rs(curr)) and tim(ls(curr)) == tim(rs(curr)))
    {
        isSame(curr) = true, tim(curr) = tim(ls(curr));
    }
    else isSame(curr) = false;
}

inline void Del(const int curr, const int l, const int r, const int s = 1, const int e = n)
{
    if (isSame(curr))
    {
        if (tim(curr))
        {
            cnt[tim(curr)] -= min(e, r) - max(s, l) + 1;
            if (!cnt[tim(curr)])add(tim(curr), -1);
        }
        return;
    }
    const int mid = s + e >> 1;
    push_down(curr);
    if (l <= mid)Del(ls(curr), l, r, s, mid);
    if (r > mid)Del(rs(curr), l, r, mid + 1, e);
}

inline void Cov(const int curr, const int l, const int r, const int val, const int s = 1, const int e = n)
{
    if (l <= s and e <= r)
    {
        Cover(curr, val);
        return;
    }
    const int mid = s + e >> 1;
    push_down(curr);
    if (l <= mid)Cov(ls(curr), l, r, val, s, mid);
    if (r > mid)Cov(rs(curr), l, r, val, mid + 1, e);
    push_up(curr);
}

inline void assign(const int Time, const int l, const int r)
{
    Del(1, l, r);
    Cov(1, l, r, Time);
    add(Time, 1);
    cnt[Time] += r - l + 1;
}

int L[N], R[N];
vector<pii> seg[N];
int ans[N];

inline void solve()
{
    cin >> n >> m;
    forn(i, 1, m)cin >> L[i] >> R[i];
    cin >> q;
    forn(i, 1, q)
    {
        int l, r;
        cin >> l >> r;
        seg[r].emplace_back(l, i);
    }
    forn(r, 1, m)
    {
        assign(r, L[r], R[r]);
        for (const auto [l,id] : seg[r])ans[id] = query(r) - query(l - 1);
    }
    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;
}

珂朵莉树做法
#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 = 1e6 + 10;

int n, m, q;

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

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

set<ODT> node;
int cnt[N];
typedef set<ODT>::iterator iter;
int bit[N];

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

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

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();
    auto [l,r,tim] = *it;
    node.erase(it), node.insert(ODT(l, pos - 1, tim));
    return node.insert(ODT(pos, r, tim)).first;
}

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

int L[N], R[N];
vector<pii> seg[N];
int ans[N];

inline void solve()
{
    cin >> n >> m;
    forn(i, 1, m)cin >> L[i] >> R[i];
    cin >> q;
    forn(i, 1, q)
    {
        int l, r;
        cin >> l >> r;
        seg[r].emplace_back(l, i);
    }
    node.insert(ODT(1, n, 0));
    forn(r, 1, m)
    {
        assign(r, L[r], R[r]);
        for (const auto [l,id] : seg[r])ans[id] = query(r) - query(l - 1);
    }
    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\log{n}) \]

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