2024 jscpc E题 Divide题解

题目链接:Divide

分析题意,区间要取最大值,然后除以 \(2\),向下取整,不断执行 \(k\) 次这样的操作,最后问你区间最大值。看一眼 \(k \le 1e9\),看眼 \(n,val \le 1e5\),再看眼时限:\(6s\),当时赛场上刚看到时想到的是 \(根号/大常数双\log?\),这题出发点其实还是很显然的:均摊结构

常见的均摊结构,对元素大小为 \(n\) 的数进行一系列操作:

  1. 对一个数不断除以 \(x\),最终为 \(0\) 以后不变,次数为 \(\log\) 级别。

  2. 对一个数不断开方,最终为 \(1\) 以后不变,根据 \(master\) 主定理,类似 \(vb\) 树的分析,至多为 \(\log\log{n}\) 次。

  3. 对一个数不断做 \(a_i=\gcd{(a_i,x)}\) 操作,最终为 \(1\),如果每次操作是有效的,那么至多为 \(\log\) 级别。证明:对于一个数来说,如果这个操作 \(x\) 恰好满足 \(x\) 恰好为 \(a_i\) 的倍数,显然他们的 \(\gcd\) 恰好为 \(a_i\),并不会发生改变,是无效操作。那么有效操作,显然一定会减小。容易知道除了 \(1\) 以外,\(2\) 可能是 \(a_i\) 的最小因数,那么反之最大因数 \(mx \le \dfrac{a_i}{2}\),所以每次操作变为最大公因数不会小于各自的最大因数,即至少减小为原来的一半,即情况 \(1\)

那么一个很好想的东西,就是这 \(n\) 个数全部变为最终的 \(0\),至多有 \(n\log{n}\) 个数,当然去重后显然也不会超过 \(n\),但到此你还是并不清楚查询问题怎么解决。

考虑暴力

赛场时队友给了一个很好的对拍的暴力思路,就是我们把 \([l,r]\) 上的数全部丢入到堆当中,然后我们执行 \(k\) 次这样的操作,每次弹出堆顶,然后除以 \(2\) 以后加入到堆当中,执行 \(k\) 次后的堆顶即为答案,其实也就是模拟题目过程。考虑这个模型简化下,我们如果让所有数及其它们的操作数全部放入堆中,那么我们发现,操作就变为了:每次弹出堆顶,最后堆顶即为答案,这个很好思考,手玩一下就懂了。那么这个堆肯定不能要了,我们考虑更快的一些数据结构去维护一个区间 \([l,r]\) 上的所有真实操作数。考虑询问的问题是不断弹出最大的数,即从最大的数开始数,弹出 \(1\) 个数以后的最大数是第 \(2\) 大的数,弹出第 \(k\) 个最大数剩下的堆顶的数是第 \(k+1\) 大的数。

最终问题解决方案:求一个区间 \([l,r]\) 从大到小的第 \(k\) 大操作数。

解法

赛场上没注意不带修改。。直接码了个树套树被卡空间,赛后又被卡时间了,挂了几发换主席树过了。

本题实际可以看成 \(t=n\log{n}\) 个数的规模问题,实际上,常见的树套树解决第 \(k\) 大有三 \(\log\) 的解法,即 二分答案 \(+ (区间+值域限制的\ check)\),这样的话,对 \(t\le 2e6\) 来说显然不可接受。对于线段树套权值树来说,我们一样可以做树上二分,具体的我们需要把所有区间树对应的权值树的节点全部取出,这样可加性问题在算 \(siz\) 的时候,就可以直接累计各个权值树的贡献了,区间限制下的节点数量显然是 \(\log{n}\) 的,树上二分计算右子树的贡献时,显然要算 \(\log{n}\) 个节点 \((注意\ n是区间限制,t是元素个数限制)\),这种算法的瓶颈点在预处理原数组上,这种做法很难过。

线段树套权值线段树参照代码
#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 = 1e5 + 10;
constexpr int MX = 1e5;
int n, q, x;

struct
{
    struct Out
    {
        int cnt;
        int left, right;
    } node[N << 9];

    int cnt;
#define left(x) node[x].left
#define right(x) node[x].right
#define cnt(x) node[x].cnt

    void add(int& curr, const int pos, const int l = 0, const int r = MX)
    {
        if (!curr) curr = ++cnt;
        cnt(curr)++;
        const int mid = l + r >> 1;
        if (l == r) return;
        if (pos <= mid) add(left(curr), pos, l, mid);
        else add(right(curr), pos, mid + 1, r);
    }

    int root[N << 2];

    void Add(const int curr, const int pos, const int val, const int l = 1, const int r = n)
    {
        add(root[curr], val);
        if (l == r) return;
        const int mid = l + r >> 1;
        if (pos <= mid) Add(ls(curr), pos, val, l, mid);
        else Add(rs(curr), pos, val, mid + 1, r);
    }

    void getNode(vector<int>& ans, const int curr, const int l, const int r, const int s = 1, const int e = n)
    {
        if (l <= s and e <= r)
        {
            ans.push_back(root[curr]);
            return;
        }
        const int mid = s + e >> 1;
        if (l <= mid) getNode(ans,ls(curr), l, r, s, mid);
        if (r > mid) getNode(ans,rs(curr), l, r, mid + 1, e);
    }

    int query(vector<int>& rt, const int k, const int l = 0, const int r = MX)
    {
        if (l == r) return l;
        int siz = 0;
        for (const int x : rt) siz += cnt(right(x));
        const int mid = l + r >> 1;
        if (siz >= k)
        {
            for (int& x : rt) x = right(x);
            return query(rt, k, mid + 1, r);
        }
        for (int& x : rt) x = left(x);
        return query(rt, k - siz, l, mid);
    }

    int query(const int l, const int r, const int k)
    {
        vector<int> ans;
        getNode(ans, 1, l, r);
        return query(ans, k);
    }
} seg;

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)
    {
        cin >> x;
        while (x) seg.Add(1, i, x), x >>= 1;
    }
    while (q--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        cout << seg.query(l, r, k + 1) << 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;
#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;
int n, q, x;
constexpr int MX = 1e5;

struct
{
    struct Out
    {
        int cnt;
        int left, right;
    } node[N << 9];

    int cnt;
#define left(x) node[x].left
#define right(x) node[x].right
#define cnt(x) node[x].cnt

    void add(int& curr, const int pos, const int l = 0, const int r = MX)
    {
        if (!curr) curr = ++cnt;
        cnt(curr)++;
        const int mid = l + r >> 1;
        if (l == r) return;
        if (pos <= mid) add(left(curr), pos, l, mid);
        else add(right(curr), pos, mid + 1, r);
    }

    int root[N];

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

    void getRoot(vector<int>& ans, int x) const
    {
        while (x) ans.push_back(root[x]), x -= lowBit(x);
    }

    int query(vector<int>& L, vector<int>& R, const int k, const int l = 0, const int r = MX)
    {
        if (l == r) return l;
        const int mid = l + r >> 1;
        int siz = 0;
        for (const int x : R) siz += cnt(right(x));
        for (const int x : L) siz -= cnt(right(x));
        if (siz >= k)
        {
            for (int& x : R) x = right(x);
            for (int& x : L) x = right(x);
            return query(L, R, k, mid + 1, r);
        }
        for (int& x : R) x = left(x);
        for (int& x : L) x = left(x);
        return query(L, R, k - siz, l, mid);
    }

    int query(const int l, const int r, const int k)
    {
        vector<int> L, R;
        getRoot(L, l - 1);
        getRoot(R, r);
        return query(L, R, k);
    }
} seg;

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)
    {
        cin >> x;
        while (x) seg.Add(i, x), x >>= 1;
    }
    while (q--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        cout << seg.query(l, r, k + 1) << 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(t\log{n}\log{n}+q\log{v}\log{n})\)


考虑不带修,直接主席树:

主席树参照代码
#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 MX = 1e5;
constexpr int N = MX * log2(MX) + 10;

struct Node
{
    int left, right, cnt;
} node[N << 5];

int n, q, x, cnt;
#define left(x) node[x].left
#define right(x) node[x].right
#define cnt(x) node[x].cnt

inline void add(const int pre, int& curr, const int pos, const int l = 0, const int r = MX)
{
    (node[curr = ++cnt] = node[pre]).cnt++;
    if (l == r) return;
    const int mid = l + r >> 1;
    if (pos <= mid) add(left(pre),left(curr), pos, l, mid);
    else add(right(pre),right(curr), pos, mid + 1, r);
}

inline int query(const int L, const int R, const int k, const int l = 0, const int r = MX)
{
    if (l == r) return l;
    const int mid = l + r >> 1;
    const int rightSize = cnt(right(R)) - cnt(right(L));
    if (rightSize >= k) return query(right(L),right(R), k, mid + 1, r);
    return query(left(L),left(R), k - rightSize, l, mid);
}

int root[N];

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)
    {
        cin >> x;
        add(root[i - 1], root[i], x);
        while (x >>= 1) add(root[i], root[i], x);
    }
    while (q--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        cout << query(root[l - 1], root[r], k + 1) << 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(t\log{V}+q\log{V})\)


不带修,那么我们可以用归并树去做,即为每个值处理一个有序下标桶,可以利用二分判断一个值被包含的区间贡献,那么搬到树上,即为权值树套有序数组,因为不带修,所以可以使用归并排序合并两个有序数组进行预处理。然后我们可以处理出一个前缀和数组,用于算出一个区间上的操作数有多少,这样当 \(k\) 大于等于它,显然可以直接输出 \(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;
#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;
constexpr int MX = 1e5;
vector<int> init[N];
int n, x, q;
vector<int> idx[N << 2];
ll pre[N];

inline void pushUp(const int curr)
{
    auto &L = idx[ls(curr)], &R = idx[rs(curr)], &val = idx[curr];
    int idx1 = 0, idx2 = 0;
    while (idx1 < L.size() and idx2 < R.size()) val.push_back(L[idx1] <= R[idx2] ? L[idx1++] : R[idx2++]);
    while (idx1 < L.size()) val.push_back(L[idx1++]);
    while (idx2 < R.size()) val.push_back(R[idx2++]);
}

inline void build(const int curr = 1, const int l = 0, const int r = MX)
{
    if (l == r)
    {
        idx[curr] = init[l];
        return;
    }
    const int mid = l + r >> 1;
    build(ls(curr), l, mid), build(rs(curr), mid + 1, r);
    pushUp(curr);
}

inline int query(const int curr, const int l, const int r, const int k, const int s = 0, const int e = MX)
{
    if (s == e) return s;
    const int rightSize = ranges::upper_bound(idx[rs(curr)], r) - ranges::upper_bound(idx[rs(curr)], l - 1);
    const int mid = s + e >> 1;
    if (rightSize >= k) return query(rs(curr), l, r, k, mid + 1, e);
    return query(ls(curr), l, r, k - rightSize, s, mid);
}

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)
    {
        cin >> x;
        while (x) pre[i]++, init[x].push_back(i), x >>= 1;
    }
    forn(i, 1, n) pre[i] += pre[i - 1];
    build();
    while (q--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        cout << (pre[r] - pre[l - 1] <= k ? 0 : query(1, l, r, k + 1)) << 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;
#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 Query
{
    int l, r, k, id;
    bool isQuery;
} qu[N << 5], qL[N << 5], qR[N << 5];

int ans[N], bit[N];
int n, q, idx, x;
vector<int> child[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 void binary(const int L, const int R, const int idxL, const int idxR)
{
    if (L == R)
    {
        forn(i, idxL, idxR) ans[qu[i].id] = L;
        return;
    }
    const int mid = L + R >> 1;
    int cntL = 0, cntR = 0;
    forn(i, idxL, idxR)
    {
        auto& [l,r,v,id,isQuery] = qu[i];
        if (isQuery)
        {
            const int siz = query(r) - query(l - 1);
            if (v <= siz) qR[++cntR] = qu[i];
            else v -= siz, qL[++cntL] = qu[i];
            continue;
        }
        if (v > mid) add(l, 1), qR[++cntR] = qu[i];
        else qL[++cntL] = qu[i];
    }
    forn(i, 1, cntR) if (!qR[i].isQuery) add(qR[i].l, -1);
    forn(i, 1, cntL) qu[i + idxL - 1] = qL[i];
    forn(i, 1, cntR) qu[i + idxL + cntL - 1] = qR[i];
    binary(L, mid, idxL, idxL + cntL - 1);
    binary(mid + 1, R, idxL + cntL, idxR);
}

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)
    {
        cin >> x;
        while (x) child[i].push_back(x), x /= 2;
    }
    forn(i, 1, n) for (const int x : child[i]) qu[++idx] = Query(i, i, x, 0, false);
    forn(i, 1, q)
    {
        int l, r, k;
        cin >> l >> r >> k;
        qu[++idx] = Query(l, r, k + 1, i, true);
    }
    binary(0, 1e5, 1, idx);
    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((t+q)\log^2{n})\)


根号算法显然不太行,\(2e6\) 的数据量如果用值域分块也看上去很不可行,如果平衡根号复杂度大概有序块能做到类似 \(q\sqrt{t\log{t}}\) 这种东西。

有序块的参照做法
#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 = 1e5 + 10;
constexpr int MX = 1e5;
vector<int> pos[N];
int n, x, q;
vector<int> bol[N];
int idx[N], s[N], e[N];

inline int querySize(const vector<int>& curr, const int x)
{
    return curr.size() - (ranges::lower_bound(all(curr), x) - curr.begin());
}

inline int queryBolck(const int l, const int r, const int x)
{
    const int L = idx[l], R = idx[r];
    int ans = 0;
    if (L == R)
    {
        forn(i, l, r) ans += querySize(pos[i], x);
        return ans;
    }
    forn(i, l, e[L]) ans += querySize(pos[i], x);
    forn(i, s[R], r) ans += querySize(pos[i], x);
    forn(i, L+1, R-1) ans += querySize(bol[i], x);
    return ans;
}

inline int query(const int l, const int r, const int k)
{
    int L = 0, R = MX;
    while (L < R)
    {
        const int mid = L + R + 1 >> 1;
        if (queryBolck(l, r, mid) >= k) L = mid;
        else R = mid - 1;
    }
    return L;
}

inline void solve()
{
    cin >> n >> q;
    const int siz = sqrt(n);
    const int cnt = (n + siz - 1) / siz;
    forn(i, 1, n)
    {
        cin >> x;
        while (x) pos[i].push_back(x), x >>= 1;
        reverse(all(pos[i]));
        idx[i] = (i - 1) / siz + 1;
    }
    forn(i, 1, cnt)
    {
        s[i] = (i - 1) * siz + 1;
        e[i] = min(n, i * siz);
        forn(j, s[i], e[i]) bol[i].insert(bol[i].end(),all(pos[j]));
    }
    while (q--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        cout << query(l, r, k + 1) << 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;
}

最后

这题官解给的最优做法,如果没理解错大概是 线段树上的分散层叠算法,然后可以用 \(bitset\) 优化之类的,等后续理解了再进行补充。

posted @ 2024-05-14 17:27  Athanasy  阅读(169)  评论(0编辑  收藏  举报