题目

题意简述

  维护一个长度为 \(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. spring boot 启动读取的配置文件优先级

    1.优先级从高到低 1.  file:/config/ 2. file:/ 3. classpath:/config/ 4. classpath:/ 所有位置的application.properti ...

  2. Spark案例练习-PV的统计

    关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新) 云盘目录说明: tools目录是安装包res   目录是每一个课件对应的代码和资源等doc  ...

  3. axios发送两次请求问题解决

    在使用axios的过程中,会发送两次请求. 看了下是因为有一个请求是OPTIONS来判断跨域的时候让不让发送请求的. 这个不算是一个bug,但是发送两个请求着实让人看着不舒服.于是修改了下,原来的请求 ...

  4. Flowable实战(一)启动第一个完整流程

    一.前言:   发现网上关于Flowable的资料基本都是浅尝辄止,对如何构建一个企业级的流程应用说明很少,所以写个实战系列,希望对大家和自己,都有所帮助. 二.认识Flowable   Flowab ...

  5. UVA 10815 Andy's First Dictionary (C++ STL map && set )

    原题链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  6. 联盛德 HLK-W806 (十三): 运行FatFs读写FAT和exFat格式的SD卡/TF卡

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  7. Android官方文档翻译 四 1.2Running Your App

    Running Your App If you followed the previous lesson to create an Android project, it includes a def ...

  8. 感恩陪伴 HelloGitHub 定制的红包封面

    距离放假越来越近了,我们更文的频率也越来越低了. 先别打!听我解释... 我真没偷懒,我是去研究今年的「微信红包封面」玩法了. 这不去年,我们制作的 HelloGitHub 专属红包封面,很多粉丝都说 ...

  9. Kubernetes的Pod进阶(十一)

    一.Lifecycle 官网:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/ 通过前面的分享,关于pod是什么相信看 ...

  10. windows批处理详解

    转:https://mp.weixin.qq.com/s/Ktbl4P16Qye7OxDNEzJI5Q