首页 > 编程语言 >P6466 分散层叠算法(Fractional Cascading) 题解

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

时间:2024-02-22 16:58:03浏览次数:22  
标签:typedef frac int 题解 Cascading template Fractional include define

题目链接:分散层叠算法

比较妙的东西,在很多涉及到若干个有序块的 \(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;
}

标签:typedef,frac,int,题解,Cascading,template,Fractional,include,define
From: https://www.cnblogs.com/Athanasy/p/18027682

相关文章

  • [ABC259Ex] Yet Another Path Counting 题解
    Description有\(N\)行\(N\)列的网格图,只能向下或向右走,合法路径的开端和结尾的格子上数字一样找到合法路径条数,对\(998244353\)取模\(1\leqN\leq400,1\leqa_{i,j}\leqN^2\)。Solution有一个\(O(n^4)\)的做法是每次枚举起点和终点然后用组合数计算答案,但是由于同......
  • 牛吃草 题解
    牛吃草居然真的是牛吃草Description由于现代化进程的加快,农场的养殖业也趋向机械化。lyz决定购置若干台自动喂草机来减少自己每天的工作量。为了简化问题,lyz决定将草地建模成一条线段,总长为\(n\),即共有\(n\)个单位长度,编号从左至右为\(1∼n\)。lyz可以在每个单位长度独......
  • blocks 单调栈、单调队列题解
    blocks题解:1、题面:2、分析:题意大概就是说,找一段最长的区间,并且这段区间的平均值>=k,那么我们可以对他的每一个值减去k,最终求和>=0即可。那我们需要对每个可能的左端点和右端点进行考虑,并以此让他们进行配对,看他们之间的区间和是否非负。那么我们先定住一个右端点,再依次考虑......
  • 理想的正方形 题解
    题目描述有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小。输入格式第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行为b个非负整数,表示矩阵中相应位置上的数。每行相邻两数之间用一空格分隔。100%的数据2<......
  • P5344 【XR-1】逛森林 题解
    题目链接:逛森林很早就想写写倍增优化建图,尤其是这题,奈何之前知识点没点够,本题线段树优化建图要优一些,不再赘述,没注意\(m\)是\(1e6\),挂了\(n\)多发才发现。后续再详细讲解倍增优化建图,这里简述本题做法。倍增优化建图其实和线段树优化建图恰不多的思想,为倍增求\(LCA\)的每......
  • 2024年2月21号题解
    106.从中序与后序遍历序列构造二叉树力扣题目链接解题思路找到根节点在中序序列的位置计算左子树的节点个数开辟一个节点,并把根节点的值赋值给这个节点根节点的左孩子和右孩子重复上面几个步骤代码实现/***Definitionforabinarytreenode.*structTreeNode{......
  • Day-7 模拟赛题解
    Day-7模拟赛题解S+N---【玄英计划】---2月21日---模拟测#3【补题】-比赛-梦熊联盟T1数据点3-5枚举每一个问号对应的字母Kmp,把s当作模式串匹配T\(O(26^k|T|)\),k是?的个数代码(我也不知道为啥T了,鸽着)正解有种被诈骗了的感觉根据期望的可加性,答案等于......
  • P10064 [SNOI2024] 公交线路 题解
    非常好题目。思路可以发现限制最严的一定是两个叶子的联通性。我们不妨把一个叶子向外起到联通性作用的路径称为有用的路径。也就是这个叶子走这条路径一定可以两步以内到达任意点。这个路径集合有什么作用呢。有一个性质:整个集合的路径的交最终会形成一个连通块。那么我们......
  • Atm/抢掠计划——题解
    题目描述样例671223352441266510128161514435647解析题目明显是最长路,可以用spfa求最长路,但数据范围5e5明显不允许,所以我们可以用tarjan优化一下,然后这就变成了一道tarjan板子题,先用tarjan缩点,点权为几个点之和,把所有点再存到一个数组中,再按......
  • 洛谷P4447题解
    md这篇反正不交题解的,随便写,不管它格式题意简化下,给你N个数,求出连续值分组人数最小的那组的人数最大值。这个题目还挺经典的,原本23年8月份过了到现在来又不会了(划掉,bushi对于这种,很容易想到的是输入,之后排序,然后分组这种模板就不多说了,就在我24年2月份重温这道题再打一遍代码......