\(\mathcal{Description}\)

  Link.

  给定 \(m\) 个长度为 \(n\) 的有严格升序且不包含重复元素的序列 \(a_1,a_2,\cdots,a_m\),\(q\) 个询问,每次询问给出 \(x\),求 \(x\) 在每个序列中的非严格后继的异或和。强制在线。

  \(m\le100\),\(n\le10^4\),\(q\le10^5\)。

\(\mathcal{Solution}\)

  算是一种对多序列二分的优化科技/叭。

  思考两种暴力做法:

  第一种,直接在每个序列里二分求答案,则有单次 \(\mathcal O(m\log n)\)。

  第二种,把 \(m\) 个序列归并为一个长度为 \(nm\) 的大序列,对于其每个位置记录其在原来 \(m\) 个序列中的非严格后继。则有 \(\mathcal O(nm\log m)-\mathcal O(m+\log(nm))\),不过空间复杂度难以接受。

  而所谓“分散层叠算法”,就是对以上两种算法的平衡——假设我们求出了序列集 \(b_1,b_2,\cdots,b_m\),其中 \(b_1=a_1\),\(b_i~(i>1)\) 是对 \(a_1,a_2,\cdots,a_i\) 某种形式的“概括”,满足我们在 \(b_i\) 中二分 \(x\),能够找到 \(x\) 在实际 \(a_i\) 中的后继,同时找到 \(x\) 在 \(b_{i-1}\) 中后继的近似位置,那么就能在 \(\mathcal O(1)\) 调整该位置后迭代入 \(b_{i-1}\) 的子问题啦。

  具体地,构造 \(b_i\) 为

\[b_i=\langle a_{i,1},a_{i,2},\cdots,a_{i,n},b_{i-1,2},b_{i-1,4},\cdots\rangle_{\text{(sorted)}}
\]

即,\(b_i\) 是 \(a_i\) 和 \(b_{i-1}\) 的偶数位置元素构成的有序序列列。归纳可证,\(b_i\) 的长度不 过 \(2n\)。同时对于 \(b_i\) 中的每个元素,记录其在 \(a_i\) 中的后继位置以及其在 \(b_{i-1}\)(完整的,包括奇数位置和偶数位置)中的后继位置(若不存在,设为最后一项,因为我们需要继续迭代调整)。

  预处理时间为 \(\mathcal O(nm)\),结合上文查询方法,做到了 \(\mathcal O(nm)-\mathcal O(\log n+m)\)。这种 trick 常常与分块结合,可以加以扩展呢!

\(\mathcal{Code}\)

  常数挺小的。(

/* Clearink */

#include <cstdio>
#include <algorithm> #define rep( i, l, r ) for ( int i = l, repEnd##i = r; i <= repEnd##i; ++i )
#define per( i, r, l ) for ( int i = r, repEnd##i = l; i >= repEnd##i; --i ) inline char fgc() {
static char buf[1 << 17], *p = buf, *q = buf;
return p == q && ( q = buf + fread( p = buf, 1, 1 << 17, stdin ), p == q )
? EOF : *p++;
} inline int rint() {
int x = 0, f = 1, s = fgc();
for ( ; s < '0' || '9' < s; s = fgc() ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = fgc() ) x = x * 10 + ( s ^ '0' );
return x * f;
} template<typename Tp>
inline void wint( Tp x ) {
if ( x < 0 ) putchar( '-' ), x = -x;
if ( 9 < x ) wint( x / 10 );
putchar( x % 10 ^ '0' );
} const int MAXN = 1e4, MAXM = 100;
int n, m, q, D, a[MAXM + 5][MAXN + 5]; int len[MAXM + 5], top[MAXN * 2 + 5];
struct Atom { int val, nxa, nxb; };
Atom b[MAXM + 5][MAXN * 2 + 5]; inline void init() {
len[m] = n;
rep ( i, 1, n ) b[m][i] = { a[m][i], i, 0 };
static Atom tmp[MAXN + 5];
per ( i, m - 1, 1 ) {
int tlen = 0;
Atom* curb = b[i];
for ( int j = 2; j <= len[i + 1]; j += 2 ) {
tmp[++tlen] = { b[i + 1][j].val, 0, j };
}
int p = 1, q = 1;
rep ( j, 1, n ) {
for ( ; p <= tlen && tmp[p].val <= a[i][j]; ++p ) {
*++curb = tmp[p], curb->nxa = j;
}
for ( ; q < len[i + 1] && b[i + 1][q].val <= a[i][j]; ++q );
*++curb = { a[i][j], j, q };
}
for ( ; p <= tlen; *++curb = tmp[p++] );
len[i] = curb - b[i];
#ifdef RYBY
printf( "b[%d]:\n", i );
rep ( j, 1, len[i] ) {
printf( "(%d,%d,%d) ", b[i][j].val, b[i][j].nxa, b[i][j].nxb );
}
putchar( '\n' );
#endif
}
rep ( i, 1, len[1] ) top[i] = b[1][i].val;
} int main() {
n = rint(), m = rint(), q = rint(), D = rint();
rep ( i, 1, m ) rep ( j, 1, n ) a[i][j] = rint();
init();
for ( int qid = 1, x, ans = 0; qid <= q; ++qid ) {
x = rint() ^ ans, ans = 0;
int p = std::lower_bound( top + 1, top + len[1] + 1, x ) - top;
ans ^= a[1][b[1][p].nxa];
#ifdef RYBY
printf( "in %d, p=%d: %d\n", 1, p, a[1][b[1][p].nxa] );
#endif
rep ( i, 2, m ) {
p = b[i - 1][p].nxb;
for ( ; p < len[i] && b[i][p + 1].val <= x; ++p );
for ( ; p > 1 && b[i][p - 1].val >= x; --p );
#ifdef RYBY
printf( "in %d, p=%d: %d\n", i, p, a[i][b[i][p].nxa] );
#endif
if ( a[i][b[i][p].nxa] >= x ) ans ^= a[i][b[i][p].nxa];
}
if ( !( qid % D ) ) wint( ans ), putchar( '\n' );
}
return 0;
}

Note/Solution -「洛谷 P6466」分散层叠算法的更多相关文章

  1. Note/Solution -「洛谷 P5158」「模板」多项式快速插值

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \((x_i,y_i)\),求一个不超过 \(n-1\) 次的多项式 \(f(x)\),使得 \(f(x ...

  2. Solution -「洛谷 P4372」Out of Sorts P

    \(\mathcal{Description}\)   OurOJ & 洛谷 P4372(几乎一致)   设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...

  3. Solution -「洛谷 P4198」楼房重建

    \(\mathcal{Description}\)   Link.   给定点集 \(\{P_n\}\),\(P_i=(i,h_i)\),\(m\) 次修改,每次修改某个 \(h_i\),在每次修改后 ...

  4. Solution -「洛谷 P6577」「模板」二分图最大权完美匹配

    \(\mathcal{Description}\)   Link.   给定二分图 \(G=(V=X\cup Y,E)\),\(|X|=|Y|=n\),边 \((u,v)\in E\) 有权 \(w( ...

  5. Solution -「洛谷 P6021」洪水

    \(\mathcal{Description}\)   Link.   给定一棵 \(n\) 个点的带点权树,删除 \(u\) 点的代价是该点点权 \(a_u\).\(m\) 次操作: 修改单点点权. ...

  6. Solution -「洛谷 P4719」「模板」"动态 DP" & 动态树分治

    \(\mathcal{Description}\)   Link.   给定一棵 \(n\) 个结点的带权树,\(m\) 次单点点权修改,求出每次修改后的带权最大独立集.   \(n,m\le10^5 ...

  7. Solution -「洛谷 P5236」「模板」静态仙人掌

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的仙人掌,\(q\) 组询问两点最短路.   \(n,q\le10^4\),\(m\ ...

  8. Solution -「洛谷 P4320」道路相遇

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的连通无向图,并给出 \(q\) 个点对 \((u,v)\),询问 \(u\) 到 ...

  9. Solution -「洛谷 P5827」边双连通图计数

    \(\mathcal{Description}\)   link.   求包含 \(n\) 个点的边双连通图的个数.   \(n\le10^5\). \(\mathcal{Solution}\)    ...

随机推荐

  1. Ant <Delete> 如何只删掉文件夹下所有文件和文件夹

    用 fileset 来过滤要删掉的目录和文件 <project name="ant-project" default="example"> < ...

  2. nvm切换node版本出现乱码 exit status 1:

    nvm切换nodejs版本出现exit status 1:乱码 跟着网上的教程一步一步做,还是出现问题.浪费一下午的时间 最后发现却因为我没用CMD管理员权限运行 扑街 解决方法: 用管理员身份运行就 ...

  3. C# 开源一个基于 yarp 的 API 网关 Demo,支持绑定 Kubernetes Service

    关于 Neting 刚开始的时候是打算使用微软官方的 Yarp 库,实现一个 API 网关,后面发现坑比较多,弄起来比较麻烦,就放弃了.目前写完了查看 Kubernetes Service 信息.创建 ...

  4. 校招面试之——Java容器

    最近校招季,特把自己面试中遇到的问题整理整理,以巩固自己的知识. Java中对于容器有两大类存储方式,一种是单元素存放,还有一种就是key-value这种有关联的双元素存放了.对于Java中的容器,有 ...

  5. Redhat 如何使用yum 源(转)

    1.需要把Redhat注册的信息给解决掉 This system is not registered with an entitlement server. You can use subscript ...

  6. VictoriaMetrics:使用vmctl来实现vm-storage向victoria-metrics-prod(单机版)迁移数据

    前一篇提到了,vm-storage的备份数据,无法被victoria-metrics-prod(单机版)读取. 继续翻文档发现vmctl可以实现这个效果: 1.启动vm-restore恢复数据 vmr ...

  7. 【解决了一个小问题】vmselect对应的vmstorage端口配置错误导致的问题

    从vmselect查询的时候,出现如下错误: error when executing query="up" on the time range (start=1639388706 ...

  8. C++与lua交互之C++访问lua

    假设lua中: name="gzw" id=17010805 sex=man tab={ num=100, str="hello" } foo_one=func ...

  9. golang中函数的参数

    1. 函数当做函数的参数 package main import "fmt" type HandleFunc func(int) (int, bool) func add10(nu ...

  10. Sweetalert模态对话框与Swiper轮播插件、Bootstrap样式组件、AdminLTE后台管理模板地址

    Sweetalert纯JS模态对话框插件地址:http://mishengqiang.com/sweetalert/ AdminLTE后台管理模板系统地址(基于Bootstrap):https://a ...