题目

题意简述

  维护一个长度为 \(n\) 的序列 \(\{a_n\}\),并给出 \(q\) 个操作:

  • 将下标为 \(x\) 的数修改为 \(y\)。
  • 给定 \(l,r,k\),求最大的 \(m\) 使得 \(\{k,k+1,\dots,k+m\}\) 是区间 \([l,r]\) 内元素的子序列。

数据规模

  \(n,q\le10^6;~a_i,y,k\le n\)。

题解

  首先不考虑修改操作,如何处理询问呢?

  不难想到维护一列指针。令 \(suf_i\) 为 \(i\) 之后第一个满足 \(a_j=a_i+1\) 的 \(j\)。那么对于询问,相当于求从区间内的第一个 \(k\) 作为链头,链尾不超过 \(r\) 的链长。可以用 std::set 维护每个值出现的下标,再预处理出 \(suf\) 链的倍增,做到 \(O(n\log n)-O\left(q\log n\right)\) 求解。

  这时再引入修改,自然地想到用动态树来维护一下 \(suf\) 链的形状。不过时间复杂度却因如下数据无法保证:若有 \(a=\{1,1,\dots,1,2\}\),则 \(suf=\{n,n,\dots,n,/\}\),再修改 \(a_n\) 的值,发现 \(suf_1\) 到 \(suf_{n-1}\) 的值都需要被修改啦!

  为应对这一情况,我们放宽 \(suf\) 的限制,令 \(suf_i\) 为 \(i\) 之后第一个满足 \(a_j\in\{a_i,a_i+1\}\) 的 \(j\),并为链加上边权:当 \(a_j=a_i\),没有贡献,边权为 \(0\),否则边权为 \(1\)。实际上,由于每个点仅会向右连一条边,可以把 \(suf_i\) 边权作为 \(i\) 的点权。并加入虚拟结点 \(n+1\) 作为动态树的根,对于任意 \(suf_i\),若没有满足条件的 \(j\),则它指向 \(n+1\)。

  这样一来,我们就有能力处理修改与查询啦。

  • 查询操作:先找到区间中的第一个 \(k\) 所对应的下标 \(u\),在 LCT 中提取 \(u\rightarrow n+1\) 这条路径(由 \(suf\) 的定义,一定存在),并在路径 Splay 上查找 \(r\) 的前驱结点 \(v\) ——由于 \(n+1\) 为根,所以深度越浅,结点编号越大,所以查前驱的时候需要把 Splay 当做“左大右小”的平衡树。最后,提取路径 \(u\rightarrow v\),计算子树和,注意判断 \(v\) 结点是否产生贡献。
  • 修改操作:分别考虑去掉该点所产生的印象和加入该点所产生的印象。利用 std::set 进行 lower_bound 等操作即可。

代码

  由于码的时候思路比较混乱,所以没有封装,而且压行有些丑呢 qwq。

#include <set>
#include <cstdio>
#include <assert.h>
#include <iostream> typedef std :: set<int> :: iterator IT; inline int rint () {
int x = 0, f = 1; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x * f;
} template<typename Tp>
inline void wint ( Tp x ) {
if ( x < 0 ) putchar ( '-' ), x = ~ x + 1;
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
} const int MAXN = 2e6;
int n, q, a[MAXN + 5], suf[MAXN + 5];
std :: set<int> apr[MAXN + 5]; int ch[MAXN + 5][2], fa[MAXN + 5], val[MAXN + 5], siz[MAXN + 5], sum[MAXN + 5];
bool flip[MAXN + 5]; inline bool nroot ( const int x ) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; } inline void pushrv ( const int x ) { flip[x] ^= 1, ch[x][0] ^= ch[x][1] ^= ch[x][0] ^= ch[x][1]; } inline void pushup ( const int x ) { sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + val[x]; } inline void pushdn ( const int x ) {
if ( ! flip[x] ) return ;
if ( ch[x][0] ) pushrv ( ch[x][0] );
if ( ch[x][1] ) pushrv ( ch[x][1] );
flip[x] = false;
} inline void rotate ( const int x ) {
int y = fa[x], z = fa[y], k = ch[y][1] == x;
fa[x] = z; if ( nroot ( y ) ) ch[z][ch[z][1] == y] = x;
ch[y][k] = ch[x][k ^ 1]; if ( ch[x][k ^ 1] ) fa[ch[x][k ^ 1]] = y;
ch[x][k ^ 1] = y, fa[y] = x, pushup ( y ), pushup ( x );
} inline void splay ( const int x ) {
static int y, z, stk[MAXN + 5];
for ( stk[z = 1] = y = x; nroot ( y ); stk[++ z] = y = fa[y] );
for ( ; z; pushdn ( stk[z --] ) );
for ( ; nroot ( x ); rotate ( x ) ) {
if ( nroot ( y = fa[x] ) ) {
rotate ( x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y );
}
}
pushup ( x );
} inline void access ( int x ) { for ( int y = 0; x; x = fa[y = x] ) splay ( x ), ch[x][1] = y, pushup ( x ); } inline void makeRoot ( const int x ) { access ( x ), splay ( x ), pushrv ( x ); } inline void link ( const int x, const int y ) { makeRoot ( x ), fa[x] = y; } inline void cut ( const int x, const int y ) { makeRoot ( x ), access ( y ), splay ( x ), fa[y] = ch[x][1] = 0, pushup ( x ); } inline void find ( int& u, const int k ) { for ( pushdn ( u ); ch[u][k < u] && k ^ u; pushdn ( u = ch[u][k < u] ) ); splay ( u ); } inline int getPre ( int u, const int k ) {
find ( u, k ); if ( u <= k ) return u;
pushdn ( u ), u = ch[u][1];
for ( pushdn ( u ); ch[u][0]; u = ch[u][0], pushdn ( u ) );
return u;
} inline void linksuf ( const int i, const int k ) {
IT it1 ( apr[k].upper_bound ( i ) ), it2 ( apr[k + 1].upper_bound ( i ) );
suf[i] = std :: min ( *it1, *it2 );
val[i] = suf[i] == *it2, pushup ( i ), link ( i, suf[i] );
} inline void relink ( const int i, const int k ) {
IT it = apr[k].lower_bound ( i );
if ( it != apr[k].begin () ) {
-- it;
cut ( *it, suf[*it] );
linksuf ( *it, a[*it] );
}
} int main () {
n = rint (), q = rint ();
for ( int i = 1; i <= n; ++ i ) apr[a[i] = rint ()].insert ( i );
for ( int i = 0; i <= n + 1; ++ i ) apr[i].insert ( n + 1 );
val[n + 1] = 0, pushup ( n + 1 );
for ( int i = 1; i <= n; ++ i ) linksuf ( i, a[i] );
for ( int opt, l, r; q --; ) {
opt = rint (), l = rint (), r = rint ();
if ( opt & 1 ) {
int t = a[l]; a[l] = r;
apr[t].erase ( l ), apr[r].insert ( l );
relink ( l, t ), relink ( l, t - 1 ), relink ( l, r ), relink ( l, r - 1 );
cut ( l, suf[l] ), linksuf ( l, a[l] );
} else {
int p = *apr[rint ()].lower_bound ( l ), q;
if ( p > r ) { puts ( "-1" ); continue; }
makeRoot ( n + 1 ), access ( p ), splay ( p );
q = getPre ( p, r );
makeRoot ( p ), access ( q ), splay ( q );
wint ( sum[q] - val[q] ), putchar ( '\n' );
}
}
return 0;
}

Solution -「NOI.AC 省选膜你赛」array的更多相关文章

  1. Solution -「NOI.AC 省选膜你赛」T2

      这道题就叫 T2 我有什么办法www 题目 题意简述   给定一个字符串 \(s\),其长度为 \(n\),求无序子串对 \((u,v)\) 的个数,其中 \((u,v)\) 满足 \(u,v\) ...

  2. Solution -「NOI.AC 省选膜你赛」寄蒜几盒

    题目 题意简述   给定一个含有 \(n\) 个顶点的凸多边形( \(n\) 是偶数),对于每一对相对的边(即中间有 \(\frac{n}2-1\) 条其它边),延长它们以将平面分割为多块,并把包含原 ...

  3. Solution -「NOI.AC 省选膜你赛」union

    题目 题意简述   给定两颗树 \(A,B\),\(A\) 中的任一结点 \(u\) 与 \(B\) 中的任一结点 \(v\) 都有一个关系值 \(f(u,v)\),初始为 \(0\).再给出 \(q ...

  4. cdcqの省选膜你赛

    cdcqの省选膜你赛 比赛当天因为在杠hnoi2016的大数据结构没有参加,今天补了一下.挺好玩的虽然不看一句话题意的话真的卡读题 此生无悔入东方,来世愿生幻想乡 2651. 新史「新幻想史 -现代史 ...

  5. Solution -「NOI 2021」「洛谷 P7740」机器人游戏

    \(\mathcal{Description}\)   Link.   自己去读题面叭~ \(\mathcal{Solution}\)   首先,参悟[样例解释 #2].一种暴力的思路即为钦定集合 \ ...

  6. Solution -「NOI 2020」「洛谷 P6776」超现实树

    \(\mathcal{Description}\)   Link.   对于非空二叉树 \(T\),定义 \(\operatorname{grow}(T)\) 为所有能通过若干次"替换 \( ...

  7. Solution -「NOI 模拟赛」彩色挂饰

    \(\mathcal{Description}\)   给定一个含 \(n\) 个点 \(m\) 条边的简单无向图,设图中最大点双的大小为 \(s\),则保证 \(s\le6\).你将要用 \(k\) ...

  8. Solution -「NOI 模拟赛」出题人

    \(\mathcal{Description}\)   给定 \(\{a_n\}\),求一个 \(\{b_{n-1}\}\),使得 \(\forall x\in\{a_n\},\exists i,j\ ...

  9. Solution -「NOI 2016」「洛谷 P1587」循环之美

    \(\mathcal{Description}\)   Link.   给定 \(n,m,k\),求 \(x\in [1,n]\cap\mathbb N,y\in [1,m]\cap \mathbb ...

随机推荐

  1. git 不小心把某个文件给 add 了 的解决方法

    1.我不小心把这两个文件给add 进来本地仓库 2.解决 进入指令框 ,执行 git rm --cached  文件名 如下图 注意,必须指定文件否则会删除所有

  2. Git 的配置 config

    Git 的配置 config Git 的配置 config config 文件简述 config 文件位置 信息查询 修改 config 文件 编辑配置文件 增加指定配置项 删除指定配置项 自助餐   ...

  3. 论文解读DEC《Unsupervised Deep Embedding for Clustering Analysis》

    Junyuan Xie, Ross B. Girshick, Ali Farhadi2015, ICML1243 Citations, 45 ReferencesCode:DownloadPaper: ...

  4. 《剑指offer》刷题目录

    <剑指offer>刷题目录 面试题03. 数组中重复的数字 面试题04. 二维数组中的查找 面试题05. 替换空格 面试题06. 从尾到头打印链表 面试题07. 重建二叉树 面试题09. ...

  5. JAVA实现对阿里云DNS的解析管理

    1.阿里云DNS的SDK依赖 <dependency> <groupId>com.aliyun</groupId> <artifactId>tea-op ...

  6. thinkpad s5 电源功率不足提示

    相关答案 作者:路灯瓜 链接:https://www.zhihu.com/question/47551448/answer/122578101 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权 ...

  7. 使用kubeadm搭建k8s集群

    1.初始化集群信息 这里我才用了两台虚拟机来搭建集群,一个master,一个node 角色 IP地址 组件 master 192.168.126.137 docker, kubectl, kubead ...

  8. linux文件系统讲解(一)

    首先拿个一个硬盘,不能直接使用,要进行分区,比如下面的一块内存: 如果要进行分区,那么怎么分区,所以要有一个内存,用来保存怎么分区的信息,该块内存的名字叫启动块(BootBlock),他的大小是固定的 ...

  9. Django settings.py配置文件

    import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 这里用到了python中一个神奇的变量 file 这个变量可以获取到当前 ...

  10. python3 requests的content和text方法

    text返回的是Unicode型的数据 content返回的是是二进制的数据. 也就是说,如果你想取文本,可以通过r.text. 如果想取图片,文件,则可以通过r.content >>&g ...