P5524 [Ynoi2012] NOIP2015 充满了希望 题解

题目链接:充满了希望

一开始以为是传统老题,结果看到有个交换单修操作,ODT 这题试了下,应该 \(fake\) 了,毕竟有单修,很难保证之前的 \(log\) 级复杂度。有些较为智慧的解法确实不好思考,说个很简单的做法,这里没有问颜色数,而是问的颜色具体情况,那就比之前的很多题简单太多了。颜色的具体情况取决于最后一次颜色覆盖。所以在没有操作一的情况下,我们对于每个三操作很容易找到它是由哪个操作决定的。直接线段树维护每个点处的最近覆盖操作即可。考虑交换操作,很显然,如果两个点交换了,那么这两个点处的最近交换操作也跟着交换就行了,这样最终跑下来的这些点处对应的最近交换处都是正确的。

下一个问题,如何统计一系列查询的和,这个很经典,扫描线一下的二维偏序,具体的我们如果遇到了一个 \(3\) 操作,它由最近覆盖 \(x\) 时决定颜色,且颜色为 \(val\),那么显然从 \(x\) 覆盖以后的所有时机,都应该加上这个 \(val\) 值。比如我们查询当前的 \([l,r]\)\(l\) 的限制,即为从 \([1,r]\) 更新以后的 \(3\) 操作的最近覆盖里处于覆盖时间在 \(l\) 之后的 \(3\) 操作,如果在 \(l\) 之前,显然根本还没覆盖,根本产生不了影响。这玩意用一个树状数组求扫描线轴上的区间和即可。

第一个问题看到最优解好像有人直接用的 \(dfs+记忆化搜索\) 确认,感觉不算通用,还是基本线段树思路维护即可。ODT 过不了,因为有单修的缘故,给个 \(fake\) 的参照。

ODT 思路参照
#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 id;

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

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();
    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 Id, const int l, const int r)
{
    auto itr = split(r + 1), itl = split(l);
    node.erase(itl, itr), node.insert(ODT(l, r, Id));
}

inline int odtQuery(const int id)
{
    split(id + 1);
    return split(id)->id;
}

struct Query
{
    int op;
    int l, r;
    int val;
} Q[N];

vector<pii> seg[N];
int ansId[N];
ll ans[N], bit[N];

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

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

inline void solve()
{
    read(n, m, q);
    node.insert(ODT(1, m, 0));
    forn(i, 1, m)
    {
        auto& [op,l,r,val] = Q[i];
        read(op);
        if (op == 1)
        {
            read(l, r);
            int idL = odtQuery(l), idR = odtQuery(r);
            swap(idL, idR);
            assign(idL, l, l), assign(idR, r, r);;
        }
        else if (op == 2)
        {
            read(l, r, val);
            assign(i, l, r);
        }
        else
        {
            read(val);
            ansId[i] = odtQuery(val);
        }
    }
    forn(i, 1, q)
    {
        int l, r;
        read(l, r);
        seg[r].emplace_back(l, i);
    }
    forn(r, 1, m)
    {
        if (ansId[r])add(ansId[r], Q[ansId[r]].val);
        for (const auto [l,id] : seg[r])ans[id] = query(r) - query(l - 1);
    }
    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;
}#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 id;

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

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();
    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 Id, const int l, const int r)
{
    auto itr = split(r + 1), itl = split(l);
    node.erase(itl, itr), node.insert(ODT(l, r, Id));
}

inline int odtQuery(const int id)
{
    split(id + 1);
    return split(id)->id;
}

struct Query
{
    int op;
    int l, r;
    int val;
} Q[N];

vector<pii> seg[N];
int ansId[N];
ll ans[N], bit[N];

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

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

inline void solve()
{
    read(n, m, q);
    node.insert(ODT(1, m, 0));
    forn(i, 1, m)
    {
        auto& [op,l,r,val] = Q[i];
        read(op);
        if (op == 1)
        {
            read(l, r);
            int idL = odtQuery(l), idR = odtQuery(r);
            swap(idL, idR);
            assign(idL, l, l), assign(idR, r, r);;
        }
        else if (op == 2)
        {
            read(l, r, val);
            assign(i, l, r);
        }
        else
        {
            read(val);
            ansId[i] = odtQuery(val);
        }
    }
    forn(i, 1, q)
    {
        int l, r;
        read(l, r);
        seg[r].emplace_back(l, i);
    }
    forn(r, 1, m)
    {
        if (ansId[r])add(ansId[r], Q[ansId[r]].val);
        for (const auto [l,id] : seg[r])ans[id] = query(r) - query(l - 1);
    }
    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;
}

正解改用线段树维护实实在在保证复杂度就行了,读入比较大,可以考虑快读读入,实测 \(cin\) 也没啥问题,这里用快读写:

线段树参照代码
#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;

struct Node
{
    int id;
    int cov;
} node[N << 2];

#define cov(x) node[x].cov
#define id(x) node[x].id

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

int n, m, q;

inline void cover(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)
    {
        id(curr) = cov(curr) = val;
        return;
    }
    const int mid = s + e >> 1;
    push_down(curr);
    if (l <= mid)cover(ls(curr), l, r, val, s, mid);
    if (r > mid)cover(rs(curr), l, r, val, mid + 1, e);
}

inline int query(const int curr, const int pos, const int l = 1, const int r = n)
{
    if (l == r)return id(curr);
    const int mid = l + r >> 1;
    push_down(curr);
    if (pos <= mid)return query(ls(curr), pos, l, mid);
    return query(rs(curr), pos, mid + 1, r);
}

struct Query
{
    int op;
    int l, r;
    int val;
} Q[N];

vector<pii> seg[N]; //扫描线查询
int ansId[N]; //处理出最近覆盖操作编号
ll ans[N], bit[N]; //区间和

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

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

inline void solve()
{
    read(n, m, q);
    forn(i, 1, m)
    {
        auto& [op,l,r,val] = Q[i];
        read(op);
        if (op == 1)
        {
            read(l, r);
            int idL = query(1, l), idR = query(1, r);
            swap(idL, idR);
            cover(1, l, l, idL), cover(1, r, r, idR);;
        }
        else if (op == 2)
        {
            read(l, r, val);
            cover(1, l, r, i);
        }
        else
        {
            read(val);
            ansId[i] = query(1, val);
        }
    }
    forn(i, 1, q)
    {
        int l, r;
        read(l, r);
        seg[r].emplace_back(l, i);
    }
    forn(r, 1, m)
    {
        if (ansId[r])add(ansId[r], Q[ansId[r]].val);
        for (const auto [l,id] : seg[r])ans[id] = query(r) - query(l - 1);
    }
    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;
}

当然,对于区间覆盖查询的线段树,我们有一种常数优化方式,在查询时并不需要 pushDown,而是有标记直接查标记,pushDown 放在修改即可,这样常数会小很多。

参照代码
#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;

struct Node
{
    int id;
    int cov;
} node[N << 2];

#define cov(x) node[x].cov
#define id(x) node[x].id

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

int n, m, q;

inline void cover(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)
    {
        id(curr) = cov(curr) = val;
        return;
    }
    const int mid = s + e >> 1;
    push_down(curr);
    if (l <= mid)cover(ls(curr), l, r, val, s, mid);
    if (r > mid)cover(rs(curr), l, r, val, mid + 1, e);
}

inline int query(const int curr, const int pos, const int l = 1, const int r = n)
{
    if(cov(curr))return cov(curr);
    if (l == r)return id(curr);
    const int mid = l + r >> 1;
    if (pos <= mid)return query(ls(curr), pos, l, mid);
    return query(rs(curr), pos, mid + 1, r);
}

struct Query
{
    int op;
    int l, r;
    int val;
} Q[N];

vector<pii> seg[N];
int ansId[N];
ll ans[N], bit[N];

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

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

inline void solve()
{
    read(n, m, q);
    forn(i, 1, m)
    {
        auto& [op,l,r,val] = Q[i];
        read(op);
        if (op == 1)
        {
            read(l, r);
            int idL = query(1, l), idR = query(1, r);
            swap(idL, idR);
            cover(1, l, l, idL), cover(1, r, r, idR);;
        }
        else if (op == 2)
        {
            read(l, r, val);
            cover(1, l, r, i);
        }
        else
        {
            read(val);
            ansId[i] = query(1, val);
        }
    }
    forn(i, 1, q)
    {
        int l, r;
        read(l, r);
        seg[r].emplace_back(l, i);
    }
    forn(r, 1, m)
    {
        if (ansId[r])add(ansId[r], Q[ansId[r]].val);
        for (const auto [l,id] : seg[r])ans[id] = query(r) - query(l - 1);
    }
    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\times (\log{n}+\log{m})+q\log{m}) \]

posted @ 2024-02-11 00:33  Athanasy  阅读(18)  评论(0编辑  收藏  举报