Solution -「NOI.AC 省选膜你赛」array
题目
题意简述
维护一个长度为 \(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的更多相关文章
- Solution -「NOI.AC 省选膜你赛」T2
这道题就叫 T2 我有什么办法www 题目 题意简述 给定一个字符串 \(s\),其长度为 \(n\),求无序子串对 \((u,v)\) 的个数,其中 \((u,v)\) 满足 \(u,v\) ...
- Solution -「NOI.AC 省选膜你赛」寄蒜几盒
题目 题意简述 给定一个含有 \(n\) 个顶点的凸多边形( \(n\) 是偶数),对于每一对相对的边(即中间有 \(\frac{n}2-1\) 条其它边),延长它们以将平面分割为多块,并把包含原 ...
- Solution -「NOI.AC 省选膜你赛」union
题目 题意简述 给定两颗树 \(A,B\),\(A\) 中的任一结点 \(u\) 与 \(B\) 中的任一结点 \(v\) 都有一个关系值 \(f(u,v)\),初始为 \(0\).再给出 \(q ...
- cdcqの省选膜你赛
cdcqの省选膜你赛 比赛当天因为在杠hnoi2016的大数据结构没有参加,今天补了一下.挺好玩的虽然不看一句话题意的话真的卡读题 此生无悔入东方,来世愿生幻想乡 2651. 新史「新幻想史 -现代史 ...
- Solution -「NOI 2021」「洛谷 P7740」机器人游戏
\(\mathcal{Description}\) Link. 自己去读题面叭~ \(\mathcal{Solution}\) 首先,参悟[样例解释 #2].一种暴力的思路即为钦定集合 \ ...
- Solution -「NOI 2020」「洛谷 P6776」超现实树
\(\mathcal{Description}\) Link. 对于非空二叉树 \(T\),定义 \(\operatorname{grow}(T)\) 为所有能通过若干次"替换 \( ...
- Solution -「NOI 模拟赛」彩色挂饰
\(\mathcal{Description}\) 给定一个含 \(n\) 个点 \(m\) 条边的简单无向图,设图中最大点双的大小为 \(s\),则保证 \(s\le6\).你将要用 \(k\) ...
- Solution -「NOI 模拟赛」出题人
\(\mathcal{Description}\) 给定 \(\{a_n\}\),求一个 \(\{b_{n-1}\}\),使得 \(\forall x\in\{a_n\},\exists i,j\ ...
- Solution -「NOI 2016」「洛谷 P1587」循环之美
\(\mathcal{Description}\) Link. 给定 \(n,m,k\),求 \(x\in [1,n]\cap\mathbb N,y\in [1,m]\cap \mathbb ...
随机推荐
- CentOS7添加开机启动服务或脚本
方法一(rc.local) 改方式配置自动启动最为简单,只需要修改rc.local文件 由于在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod + ...
- 关闭SpringBoot logo图标
public static void main(String[] args) {// SpringApplication.run(LicenseApp.class, args); //关闭Spring ...
- 利用static来实现单例模式
一:之前旧的写法 class Singleton{ private Singleton() {} private static Singleton instance = null; public sy ...
- 简单的Dos 命令
1.1.如何操作DOS命令 开始---运行---输入cmd--回车 或者 Win + R ---运行---输入cmd--回车 1.2.基本命令 1. 命令:color f0 帮助:color ? 作用 ...
- PIKACHU之文件包含漏洞
PIKUCHU靶场之文件包含 一.file inclusion(local) 实验源码: <?php /** * Created by runner.han * There is nothing ...
- F2BPM的流程仿真
仿真概述 F2BPM工作流仿真是一种通过建立工作流虚拟运行环境执行工作流仿真的方法.集中式仿真引擎解释工作流仿真模型,仿真活动的执行,处理仿真过程中的不确定性,从而完成工作流模型的仿真.同时,会实时显 ...
- 《剑指offer》面试题54. 二叉搜索树的第k大节点
问题描述 给定一棵二叉搜索树,请找出其中第k大的节点. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 输出: 4 示例 2: 输入: ...
- 图片不清晰?Graphics 高质量绘制
Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; Graphics ...
- RT-Thread移植到stm32
一.移植RT-Thread准备 RT-Thread源码 源码版本和下载方式,可以参考RT-Thread移植入门学习. keil软件 STM32工程项目模板 因为每一厂家提供的库文件可能有一些区别,在移 ...
- ArrayList实现类
特点 数组结构实现,查询快,增删慢 运行效率高,线程不安全 可重复 常用方法 Modifier and Type Method and Description boolean add(E e) 将指定 ...