P6466 分散层叠算法(Fractional Cascading) 题解

题目链接:分散层叠算法

比较妙的东西,在很多涉及到若干个有序块的 \(kth\) 查询的 ynoi 题中都有妙用。这里简单提提。

两种暴力解法在其他文章已有涉及,在此不再赘述。讲讲具有该怎么写这个算法,首先我们需要预处理出新的 \(k\) 个序列,不妨记每个为 \(M_i\)\(M_{n}=L_n\),其中 \(L\) 表示原序列,每个序列 \(M_{i}\)\(L_{i}\)\(M_{i+1}\) 组成,具体序列每个点一共维护三个信息,一个为 \(val\) 当前值,\(currIdx\) 在当前 \(L_i\) 中的后继,\(nxtIdx\)\(M_{i+1}\) 中的后继。我们每次处理一个序列的时候先拿到 \(M_{i+1}\) 的偶数下标的值,然后再与当前 \(L_i\) 进行拼凑。我们可以用两个指针分别做双指针算法,求出 \(currIdx\)\(nxtIdx\),这样就能处理完这 \(k\)\(M_i\)。对于每个点的后继有这么一个特点,\(currIdx\) 是准确的,\(nxtIdx\) 至多相差 \(1\),因为我们只记录了偶数下标,并未记录奇数下标,所以当它跳到 \(M_{i+1}\) 时,需要移动指针到正确的后继位置,但移动步数并不会太多。每个 \(M_i\)多比 \(M_{i+1}\) 多个 \(\frac{M_{i+1}}{2}\)\(n+\frac{n}{2}\)\(n+\frac{n}{2}+\frac{n}{4}\)\(n+\frac{n}{2}+\frac{n}{4}+\frac{n}{8}\),容易知道:\(1+\frac{1}{2}+\frac{1}{4}+...\frac{1}{n}<2\),所以空间方面:\(\sim 2nk\),预处理时间复杂度方面,显然每次至多 \(O(2n)\),处理 \(k\) 个序列即为 \(O(nk)\)

查找就简单了,第一个序列就直接二分,其余的跳指针以后移动到正确位置,落点离正确位置至多差 \(1\)。这个单次查找的复杂度为 \(O(\log{n}+k)\)

点击查看代码
#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 LEN = 110;
constexpr int N = 1e4 + 10;
int a[LEN][N];
int len[LEN];

struct Info
{
    int val, currNxt, nextNxt;
} node[LEN][N << 1];

int n, k, q, d;
Info tmp[N];
int binary[N];

inline void build()
{
    len[k] = n;
    forn(i, 1, len[k])node[k][i] = Info(a[k][i], i, 0);
    forv(i, k-1, 1)
    {
        int cnt = 0;
        forn(j, 1, len[i+1])if (j % 2 == 0)tmp[++cnt] = node[i + 1][j], tmp[cnt].nextNxt = j;
        int currIdx = 1, nxtIdx = 1;
        forn(j, 1, n)
        {
            while (currIdx <= cnt and tmp[currIdx].val <= a[i][j])
            {
                tmp[currIdx].currNxt = j;
                node[i][++len[i]] = tmp[currIdx++];
            }
            while (nxtIdx <= len[i + 1] and node[i + 1][nxtIdx].val <= a[i][j])nxtIdx++;
            node[i][++len[i]] = Info(a[i][j], j, nxtIdx);
        }
        while (currIdx <= cnt)
        {
            tmp[currIdx].currNxt = n + 1;
            node[i][++len[i]] = tmp[currIdx++];
        }
    }
    forn(i, 1, len[1])binary[i] = node[1][i].val;
}

int last;

inline void solve()
{
    cin >> n >> k >> q >> d;
    forn(i, 1, k)
    {
        forn(j, 1, n)cin >> a[i][j];
    }
    build();
    forn(idx, 1, q)
    {
        int x;
        cin >> x;
        x ^= last;
        int ans = 0;
        int ansIdx = lower_bound(binary + 1, binary + len[1] + 1, x) - binary;
        forn(i, 1, k)
        {
            while (ansIdx <= len[i] and node[i][ansIdx].val < x)ansIdx++;
            while (ansIdx >= 2 and node[i][ansIdx - 1].val >= x)ansIdx--;
            if (ansIdx <= len[i])
            {
                ans ^= a[i][node[i][ansIdx].currNxt];
                ansIdx = node[i][ansIdx].nextNxt;
            }
            else ansIdx = len[i + 1] + 1;
        }
        if (idx % d == 0)cout << ans << endl;
        last = ans;
    }
}

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;
}
posted @ 2024-02-22 16:47  Athanasy  阅读(49)  评论(0编辑  收藏  举报