P2824 [HEOI2016/TJOI2016] 排序 与 ABC297_g Range Sort Query 题解

洛谷题目链接:排序

abc题目链接:Atcoder 或者 洛谷

两道差不多的题拿出来说说。本题有双 \(\log\) 做法,也有单 \(\log\) 做法,都讲讲。

\(\log\) 做法

对于第一个题而言,询问最终 \(pos\) 位置上的数为多少,那么这个问题是否具有单调性?这个是很有意思的点,我们考虑只关注某个数 \(x\) 而言,由于本题是排列,那么除开 \(x\) 剩下的数被分为了两类:

  1. \(<x\) 的数。

  2. \(>x\) 的数。

对于一个区间上进行排序,我们知道排序的结果一定是 \(<x\) 的数全部被放在了前面,而 \(>x\) 的数全部被放在了后面,只是它们之间的相对顺序我们并无法确定。但我们发现最终排序的结果可以知道 \(pos\) 位置上的数对 \(x\) 的关系。

具体的,我们把 \(\ge x\) 的数全部当做 \(1\)\(<x\) 的数全部当做 \(0\),一次排序无非就是全部的 \(0\) 放前面或者后面,当然这些 \(0\) 或者 \(1\) 的相对次序我们是不知道的。但最终一定可以知道 \(pos\) 位置是 \(0\) 还是 \(1\),而显然,如果我们假设答案为 \(x\),那么 \(pos\) 位置最终的值应该是 \(1\)

现在考虑 \(x\) 是否具有单调性?考虑如果 \(x\) 成立可以让 \(pos\)\(1\),让 \(x\) 变得更大或者更小,是否一定能满足答案。

考虑更小的数 \(y<x\),当 \(x\) 满足 \(check\) 时,是否 \(y\) 也一定满足:

考虑 \([l,r]\) 为一次操作且包括 \(pos\),假设这是升序操作,那么由于 \(y<x\),那么 \([l,r]\) 上的 \(1\) 的数量一定是不降的,因为 \(1\)\(\ge x\ 或者\ y\) 的数,那么 \(cnt1\) 变大,由于这是是升序排序,那么 \(cnt0\) 变少:

降序是同理的,可以发现在原来包含 \(pos\)\([l,r]\) 的基础上有更多的地方被排序为了 \(1\),更容易构造出 \(1\) 了,极端情况为 \(x=1\),这个时候显然全为 \(1\),一定 \(check\) 成功。变得越大显然有 \(check\) 越难成功的情况,可以二分。

所以这玩意 \(check\) 我们做两个操作:

  1. 区间覆盖

  2. 单点查询

这个线段树或者珂朵莉树去做,复杂度为:\(O(q\log^2{n})\)

P2824 线段树做法
#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast,unroll-loops")

#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 = 1e5 + 10;

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

int a[N], b[N];
#define cov(x) node[x].cov
#define len(x) node[x].len
#define cnt(x) node[x].cnt

inline void push_down(const int curr)
{
    if (cov(curr) != -1)
    {
        cnt(ls(curr)) = len(ls(curr)) * (cov(ls(curr)) = cov(curr));
        cnt(rs(curr)) = len(rs(curr)) * (cov(rs(curr)) = cov(curr));
        cov(curr) = -1;
    }
}

inline void push_up(const int curr)
{
    cnt(curr) = cnt(ls(curr)) + cnt(rs(curr));
}

inline void build(const int curr, const int l, const int r)
{
    cov(curr) = -1;
    len(curr) = r - l + 1;
    if (l == r)
    {
        cnt(curr) = b[l];
        return;
    }
    const int mid = l + r >> 1;
    build(ls(curr), l, mid);
    build(rs(curr), mid + 1, r);
    push_up(curr);
}

inline void cover(const int curr, const int l, const int r, const int s, const int e, const int val)
{
    if (l > r)return;
    if (l <= s and e <= r)
    {
        cnt(curr) = len(curr) * val;
        cov(curr) = val;
        return;
    }
    push_down(curr);
    const int mid = (s + e) >> 1;
    if (l <= mid)cover(ls(curr), l, r, s, mid, val);
    if (r > mid)cover(rs(curr), l, r, mid + 1, e, val);
    push_up(curr);
}

inline int query(const int curr, const int l, const int r, const int s, const int e)
{
    if (l <= s and e <= r)return cnt(curr);
    push_down(curr);
    const int mid = (s + e) >> 1;
    int ans = 0;
    if (l <= mid)ans += query(ls(curr), l, r, s, mid);
    if (r > mid)ans += query(rs(curr), l, r, mid + 1, e);
    return ans;
}

int op[N], l[N], r[N];
int n, q;
int pos;

inline bool check(const int x)
{
    forn(i, 1, n)b[i] = a[i] >= x;
    build(1, 1, n);
    forn(i, 1, q)
    {
        int L = l[i], R = r[i];
        int cnt1 = query(1, L, R, 1, n);
        int cnt0 = R - L + 1 - cnt1;
        // cerr << "L:" << L << " R:" << R << " cnt0:" << cnt0 << " cnt1:" << cnt1 << endl;
        if (!op[i])
        {
            cover(1, L, L + cnt0 - 1, 1, n, 0);
            cover(1, L + cnt0, R, 1, n, 1);
        }
        else
        {
            cover(1, L, L + cnt1 - 1, 1, n, 1);
            cover(1, L + cnt1, R, 1, n, 0);
        }
    }
    return query(1, pos, pos, 1, n);
}

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)cin >> a[i];
    forn(i, 1, q)cin >> op[i] >> l[i] >> r[i];
    cin >> pos;
    int L = 1;
    int R = n;
    while (L < R)
    {
        const int mid = (L + R + 1) >> 1;
        if (check(mid))L = mid;
        else R = mid - 1;
    }
    cout << L;
}

signed int main()
{
    Spider
    //------------------------------------------------------
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
}
P2824 珂朵莉树做法
#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast,unroll-loops")

#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;
}

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

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

inline int query(const int l, const int r)
{
    auto itr = split(r + 1), itl = split(l);
    int ans = 0;
    for (; itl != itr; ++itl)if (itl->val)ans += itl->r - itl->l + 1;
    return ans;
}

constexpr int N = 1e5 + 10;
int n, q, pos, a[N], op[N], l[N], r[N];

inline bool check(const int x)
{
    node.clear();
    forn(i, 1, n)node.insert(ODT(i, i, a[i] >= x));
    forn(i, 1, q)
    {
        int L = l[i], R = r[i];
        int cnt1 = query(L, R);
        int cnt0 = R - L + 1 - cnt1;
        // cerr << "L:" << L << " R:" << R << " cnt0:" << cnt0 << " cnt1:" << cnt1 << endl;
        if (!op[i])
        {
            assign(L, L + cnt0 - 1, 0);
            assign(L + cnt0, R, 1);
        }
        else
        {
            assign(L, L + cnt1 - 1, 1);
            assign(L + cnt1, R, 0);
        }
    }
    return split(pos)->val;
}

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)cin >> a[i];
    forn(i, 1, q)cin >> op[i] >> l[i] >> r[i];
    cin >> pos;
    int L = 1;
    int R = n;
    while (L < R)
    {
        const int mid = (L + R + 1) >> 1;
        if (check(mid))L = mid;
        else R = mid - 1;
    }
    cout << L;
}

signed int main()
{
    Spider
    //------------------------------------------------------
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
}

关于 abc 这题询问的是等于 \(pos\) 的位置是多少,很显然,这个玩意没法直接做二分,但是我们发现,如果将 \(\le x\) 设做 \(1\)\(>x\) 设做 \(0\),那么显然两种二分的同一个数 \(pos\) 都应该为 \(1\)。当然如果你是 \(\ge x\) 设做 \(1/0\)\(<x\) 的设做 \(0/1\) 那么对于 \(=x\) 的位置算出来的我位置的答案应该是不同的。总之,无论你怎么选,我们都需要单独地让只有 \(=x\) 的位置与众不同,这样最后遍历就只需要找到这个唯一一个不同位置即可。这个问题可以不需要二分,直接这样两次排序做到 \(O(q\log{n})\) 解决,这种单 \(\log\) 并不通用,所以本题我给出另一种单 \(\log{n}\) 做法。

当然上述第二个问题还有一种写法即为 \(<x\)\(0\)\(=x\)\(1\)\(>x\)\(2\),每次排序的时候分别查出这三个数的数量,然后分别做三次区间覆盖即可,只需要排序一次就 \(ok\),不再赘述。

通用的单 \(\log\) 做法

在数据结构里有个东西比较有意思,那就是权值线段树自带有序,当然这种单 \(\log\) 换成平衡树也是完全可以的,不过很久没写线段树分裂和合并了,这里讲这种。我们注意到将若干个权值线段树合并为一棵权值线段树,这棵权值线段树就能表示 \([l,r]\) 的有序情况了,可以二分,可以查找第 \(kth\)。我们一个很直接的想法,为每个点处开一颗权值线段树,那么排序即为若干个棵权值线段树合并,我们规定都和 \(l\) 处的权值线段树合并,那么 \(root[l]\) 的线段树即为 \([l,r]\) 上的有序情况。我们注意到了 \([l,r]\) 可能有部分线段树已经合并,或者 \([l,x]\) 部分已经被包括在了 \(root[pre]\) 处的一棵权值线段树上了,即我们需要找到 \(l\) 前面的那个权值线段树的根 \(root\) 然后把 \([l,x]\) 这一部分通过线段树分裂出来,这个很容易做到,我们可以用 \(set\) 类似珂朵莉树去维护若干个权值线段树的根,其实和镜中的昆虫那题差不多的思想,那么我们就可以二分出这个 \(pre\),然后知道 \(pre\rightarrow l\) 上有多少个数,自然可以二分出它的补集了,线段树上二分分裂。当然要区别正逆序二分的方向是不同的。

如图以顺序为例,蓝色部分 (不能包括 \(l\)) 的补集即为红色部分,红色部分就是我们要的,它的补集即为蓝色部分,红色部分就是我们需要丢给 \(root[l]\) 这棵线段树的新线段树,需要分裂出来,我们只需要看补集那部分元素个数是否有 \(k\) 个即可二分,甚至不需要记录二分的范围 \(L\)\(R\)

同理也对 \(r\) 分裂 ,然后类似珂朵莉树枚举合并,根据颜色均摊,每次 \([l,r]\) 都被合并为了一棵树,相当于覆盖为了一个颜色值,类似 P4690 [Ynoi2016] 镜中的昆虫 题解 提到的,每次合并和分裂是 \(\log{n}\),操作总复杂度均摊最坏为 \(O(q\log{n})\)

最后查询我们可以考虑找到 \(pre\) 变为查找第 \(k\) 大,也可以直接全部 \(dfs\) 做到 \(O(n)\) 还原原数组直接可以完成这两个题。

P2824 最终找第 $kth$ 做法
#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 = 1e5 + 10;

struct Node
{
    int child[2];
    int siz;
} node[N << 5];

#define left(x) node[x].child[0]
#define right(x) node[x].child[1]
#define son(x,y) node[x].child[y]
#define siz(x) node[x].siz
int cnt, n, q;
int root[N], op[N]; //0升序,1降序
stack<int> del; //回收节点

inline int newNode()
{
    if (del.empty())return ++cnt;
    const int curr = del.top();
    del.pop();
    return curr;
}

inline void Del(int& curr)
{
    left(curr) = right(curr) = siz(curr) = 0;
    del.push(curr), curr = 0;
}

inline void pushUp(const int curr)
{
    siz(curr) = siz(left(curr)) + siz(right(curr));
}

inline void add(int& curr, const int val, const int l = 1, const int r = n)
{
    if (!curr)curr = newNode();
    if (l == r)
    {
        siz(curr) = 1;
        return;
    }
    const int mid = l + r >> 1;
    if (val <= mid)add(left(curr), val, l, mid);
    else add(right(curr), val, mid + 1, r);
    pushUp(curr);
}

//权值线段树合并
inline void merge(int& curr, int& other, const int l = 1, const int r = n)
{
    if (!curr or !other)
    {
        curr += other;
        return;
    }
    if (l == r)
    {
        siz(curr) += siz(other), Del(other);
        return;
    }
    const int mid = l + r >> 1;
    merge(left(curr),left(other), l, mid);
    merge(right(curr),right(other), mid + 1, r);
    Del(other);
    pushUp(curr);
}

//线段树分离,二分找蓝色部分(红色部分补集)是否满足k限制
inline void split(const int curr, int& other, const int k, const int Op)
{
    if (!curr or siz(curr) == k)return;
    const int binarySize = siz(son(curr,Op));
    if (!other)other = newNode();
    if (k <= binarySize)
    {
        son(other, Op^1) = son(curr, Op^1), son(curr, Op^1) = 0;
        split(son(curr, Op),son(other, Op), k, Op);
    }
    else split(son(curr, Op^1),son(other, Op^1), k - binarySize, Op);
    pushUp(curr), pushUp(other);
}

set<int> point; //权值线段树的根位置
typedef set<int>::iterator iter;

inline iter split(const int pos)
{
    auto it = point.lower_bound(pos);
    if (*it == pos)return it; //本身就是根
    --it;
    auto pre = *it;
    //分裂出[pos,last] 这部分,变为了[x,pos-1]+[pos,last]两棵线段树
    split(root[pre], root[pos], pos - pre, op[pos] = op[pre]);
    return point.insert(pos).first;
}

inline void assign(const int l, const int r, const int Op)
{
    auto itr = split(r + 1), itl = split(l);
    for (auto it = ++itl; it != itr; ++it)merge(root[l], root[*it]); //合并到最左边的那棵线段树上
    op[l] = Op, point.erase(itl, itr);
}

//查找第k大
inline int query(const int curr, const int k, const int l = 1, const int r = n)
{
    if (l == r)return l;
    const int mid = l + r >> 1;
    if (siz(left(curr)) >= k)return query(left(curr), k, l, mid);
    return query(right(curr), k - siz(left(curr)), mid + 1, r);
}

inline void dfs(const int curr, const int Op, const int l = 1, const int r = n)
{
    if (!curr)return;
    if (l == r)
    {
        cout << l << ' ';
        return;
    }
    const int mid = l + r >> 1;
    if (!Op)
    {
        if (siz(left(curr)))dfs(left(curr), Op, l, mid);
        if (siz(right(curr)))dfs(right(curr), Op, mid + 1, r);
    }
    else
    {
        if (siz(right(curr)))dfs(right(curr), Op, mid + 1, r);
        if (siz(left(curr)))dfs(left(curr), Op, l, mid);
    }
}

int x;

inline void debug()
{
    forn(i, 1, n)if (root[i])dfs(root[i], op[i]);
    cout << endl;
}

inline void solve()
{
    cin >> n >> q;
    point.insert(n + 1);
    forn(i, 1, n)cin >> x, add(root[i], x), point.insert(i);
    // debug();
    while (q--)
    {
        int Op, l, r;
        cin >> Op >> l >> r;
        assign(l, r, Op);
        // debug();
    }
    int pos;
    cin >> pos;
    //第k大查找
    const int pre = *--point.upper_bound(pos);
    const int idx = pos - pre + 1;
    const int curr = !op[pre] ? idx : siz(root[pre]) - idx + 1;
    cout << query(root[pre], curr);
}

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

P2824 最终还原数组做法
#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 = 1e5 + 10;

struct Node
{
    int child[2];
    int siz;
} node[N << 5];

#define left(x) node[x].child[0]
#define right(x) node[x].child[1]
#define son(x,y) node[x].child[y]
#define siz(x) node[x].siz
int cnt, n, q;
int root[N], op[N]; //0升序,1降序
stack<int> del;

inline int newNode()
{
    if (del.empty())return ++cnt;
    const int curr = del.top();
    del.pop();
    return curr;
}

inline void Del(int& curr)
{
    left(curr) = right(curr) = siz(curr) = 0;
    del.push(curr), curr = 0;
}

inline void pushUp(const int curr)
{
    siz(curr) = siz(left(curr)) + siz(right(curr));
}

inline void add(int& curr, const int val, const int l = 1, const int r = n)
{
    if (!curr)curr = newNode();
    if (l == r)
    {
        siz(curr) = 1;
        return;
    }
    const int mid = l + r >> 1;
    if (val <= mid)add(left(curr), val, l, mid);
    else add(right(curr), val, mid + 1, r);
    pushUp(curr);
}

inline void merge(int& curr, int& other, const int l = 1, const int r = n)
{
    if (!curr or !other)
    {
        curr += other;
        return;
    }
    if (l == r)
    {
        siz(curr) += siz(other), Del(other);
        return;
    }
    const int mid = l + r >> 1;
    merge(left(curr),left(other), l, mid);
    merge(right(curr),right(other), mid + 1, r);
    Del(other);
    pushUp(curr);
}

inline void split(const int curr, int& other, const int k, const int Op)
{
    if (!curr or siz(curr) == k)return;
    const int binarySize = siz(son(curr,Op));
    if (!other)other = newNode();
    if (k <= binarySize)
    {
        son(other, Op^1) = son(curr, Op^1), son(curr, Op^1) = 0;
        split(son(curr, Op),son(other, Op), k, Op);
    }
    else split(son(curr, Op^1),son(other, Op^1), k - binarySize, Op);
    pushUp(curr), pushUp(other);
}

set<int> point;
typedef set<int>::iterator iter;

inline iter split(const int pos)
{
    auto it = point.lower_bound(pos);
    if (*it == pos)return it;
    --it;
    auto pre = *it;
    split(root[pre], root[pos], pos - pre, op[pos] = op[pre]);
    return point.insert(pos).first;
}

inline void assign(const int l, const int r, const int Op)
{
    auto itr = split(r + 1), itl = split(l);
    for (auto it = ++itl; it != itr; ++it)merge(root[l], root[*it]);
    op[l] = Op, point.erase(itl, itr);
}

inline int query(const int curr, const int k, const int l = 1, const int r = n)
{
    if (l == r)return l;
    const int mid = l + r >> 1;
    if (siz(left(curr)) >= k)return query(left(curr), k, l, mid);
    return query(right(curr), k - siz(left(curr)), mid + 1, r);
}

vector<int> ans;

inline void dfs(const int curr, const int Op, const int l = 1, const int r = n)
{
    if (!curr)return;
    if (l == r)
    {
        ans.push_back(l);
        return;
    }
    const int mid = l + r >> 1;
    if (!Op)
    {
        if (siz(left(curr)))dfs(left(curr), Op, l, mid);
        if (siz(right(curr)))dfs(right(curr), Op, mid + 1, r);
    }
    else
    {
        if (siz(right(curr)))dfs(right(curr), Op, mid + 1, r);
        if (siz(left(curr)))dfs(left(curr), Op, l, mid);
    }
}

int x;

inline void getAns()
{
    forn(i, 1, n)if (root[i])dfs(root[i], op[i]);
}

inline void solve()
{
    int pos;
    cin >> n >> q;
    point.insert(n + 1);
    forn(i, 1, n)cin >> x, add(root[i], x), point.insert(i);
    while (q--)
    {
        int Op, l, r;
        cin >> Op >> l >> r;
        assign(l, r, Op);
    }
    cin >> pos;
    getAns();
    cout << ans[pos - 1];
}

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

ABC-G 最终还原数组做法
#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;

struct Node
{
    int child[2];
    int siz;
} node[N << 5];

#define left(x) node[x].child[0]
#define right(x) node[x].child[1]
#define son(x,y) node[x].child[y]
#define siz(x) node[x].siz
int cnt, n, q;
int root[N], op[N]; //0升序,1降序
stack<int> del;

inline int newNode()
{
    if (del.empty())return ++cnt;
    const int curr = del.top();
    del.pop();
    return curr;
}

inline void Del(int& curr)
{
    left(curr) = right(curr) = siz(curr) = 0;
    del.push(curr), curr = 0;
}

inline void pushUp(const int curr)
{
    siz(curr) = siz(left(curr)) + siz(right(curr));
}

inline void add(int& curr, const int val, const int l = 1, const int r = n)
{
    if (!curr)curr = newNode();
    if (l == r)
    {
        siz(curr) = 1;
        return;
    }
    const int mid = l + r >> 1;
    if (val <= mid)add(left(curr), val, l, mid);
    else add(right(curr), val, mid + 1, r);
    pushUp(curr);
}

inline void merge(int& curr, int& other, const int l = 1, const int r = n)
{
    if (!curr or !other)
    {
        curr += other;
        return;
    }
    if (l == r)
    {
        siz(curr) += siz(other), Del(other);
        return;
    }
    const int mid = l + r >> 1;
    merge(left(curr),left(other), l, mid);
    merge(right(curr),right(other), mid + 1, r);
    Del(other);
    pushUp(curr);
}

inline void split(const int curr, int& other, const int k, const int Op)
{
    if (!curr or siz(curr) == k)return;
    const int binarySize = siz(son(curr,Op));
    if (!other)other = newNode();
    if (k <= binarySize)
    {
        son(other, Op^1) = son(curr, Op^1), son(curr, Op^1) = 0;
        split(son(curr, Op),son(other, Op), k, Op);
    }
    else split(son(curr, Op^1),son(other, Op^1), k - binarySize, Op);
    pushUp(curr), pushUp(other);
}

set<int> point;
typedef set<int>::iterator iter;

inline iter split(const int pos)
{
    auto it = point.lower_bound(pos);
    if (*it == pos)return it;
    --it;
    auto pre = *it;
    split(root[pre], root[pos], pos - pre, op[pos] = op[pre]);
    return point.insert(pos).first;
}

inline void assign(const int l, const int r, const int Op)
{
    auto itr = split(r + 1), itl = split(l);
    for (auto it = ++itl; it != itr; ++it)merge(root[l], root[*it]);
    op[l] = Op, point.erase(itl, itr);
}

inline int query(const int curr, const int k, const int l = 1, const int r = n)
{
    if (l == r)return l;
    const int mid = l + r >> 1;
    if (siz(left(curr)) >= k)return query(left(curr), k, l, mid);
    return query(right(curr), k - siz(left(curr)), mid + 1, r);
}

vector<int> ans;

inline void dfs(const int curr, const int Op, const int l = 1, const int r = n)
{
    if (!curr)return;
    if (l == r)
    {
        ans.push_back(l);
        return;
    }
    const int mid = l + r >> 1;
    if (!Op)
    {
        if (siz(left(curr)))dfs(left(curr), Op, l, mid);
        if (siz(right(curr)))dfs(right(curr), Op, mid + 1, r);
    }
    else
    {
        if (siz(right(curr)))dfs(right(curr), Op, mid + 1, r);
        if (siz(left(curr)))dfs(left(curr), Op, l, mid);
    }
}

int x;

inline void getAns()
{
    forn(i, 1, n)if (root[i])dfs(root[i], op[i]);
}

inline void solve()
{
    int pos;
    cin >> n >> q >> pos;
    point.insert(n + 1);
    forn(i, 1, n)cin >> x, add(root[i], x), point.insert(i);
    while (q--)
    {
        int Op, l, r;
        cin >> Op >> l >> r;
        if (Op == 1)Op = 0;
        else Op = 1;
        assign(l, r, Op);
    }
    getAns();
    forn(i, 0, n-1)
    {
        if (ans[i] == pos)
        {
            cout << i + 1 << endl;
            return;
        }
    }
}

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-15 22:34  Athanasy  阅读(20)  评论(0编辑  收藏  举报