$ >Codeforces \space 650 D.\ Zip-line<$

题目大意 :

有一个长度为 \(n\) 的序列 \(h\) ,\(m\) 次询问,每一次询问求如果把序列中第 \(x\) 元素变成 \(y\) 后的 \(lis\) 长度

\(1 \leq n, m \leq 4 \times 10^5\)

解题思路 :

考虑答案的形态由两部分组成,一部分是包含 \(x\) 的 \(lis\) ,一部分是不包含 \(x\) 的 \(lis\)

前者显然可以维护左右两个 \(dp\) 值然后主席树数一下点,难度在于后者。

对于第二部分,如果 \(x\) 是之前所有 \(lis\) 共有的点,那么第二部分的答案就是 \(lis-1\) ,否则是 \(lis\)

考虑怎么判断一个点是否是所有 \(lis\) 共有,下面先给出方法:

统计每一个点在 \(lis\) 中出现的位置情况,设 \(l[i]\) 表示从左到右以 \(i\) 结尾的 \(lis\) 长度,\(r[i]\) 表示从右到左以 \(i\) 结尾的 \(lis\) 长度,如果 \(l[i]+r[i]-1=lis\),那么 \(i\) 在 \(lis\) 中的出现位置就是 \(l[i]\),记为 \(pos[i]\)

如果说点 \(x\) 在 \(lis\) 中的出现位置 \(z\) 只有 \(x\) 满足 \(pos[x] =z\) ,那么显然 \(x\) 是不能被替代的,其是所有 \(lis\) 共有的点。不然的话必然存在一种方案不经过 \(x\) 到另外一个满足 \(pos[i] = z\) 的点 \(i\) ,因为 \(x\) 在任何方案下不能存在于两个位置,这样的话 \(lis\) 长度就会更长,有矛盾。

所以只需要统计一下每一个 \(i\) 是不是在所有的 \(lis\) 中出现即可,总复杂度 \(O((n+m)logn)\)

某位神仙表示直接上分治 \(O(nlog^2n)\) 就过了

/*program by mangoyang*/
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 400005;
int h[N], L[N], R[N], g[N], pos[N], tot[N], lis, n, m; struct SegmentTree{
int s[N*25], lc[N*25], rc[N*25], rt[N], size;
inline SegmentTree(){ memset(s, 127, sizeof(s)); }
inline void ins(int &u, int pr, int l, int r, int pos, int x){
u = ++size, lc[u] = lc[pr], rc[u] = rc[pr];
if(l == r) return (void)(s[u] = min(s[u], x));
int mid = l + r >> 1;
if(pos <= mid) ins(lc[u], lc[pr], l, mid, pos, x);
else ins(rc[u], rc[pr], mid + 1, r, pos, x);
s[u] = min(s[lc[u]], s[rc[u]]);
}
inline int query(int u, int l, int r, int x){
if(l == r) return l;
int mid = l + r >> 1;
if(s[rc[u]] < x) return query(rc[u], mid + 1, r, x);
if(s[lc[u]] < x) return query(lc[u], l, mid, x);
return 0;
}
}S1, S2;
int main(){
read(n), read(m);
for(int i = 1; i <= n; i++) read(h[i]);
for(int i = 1; i <= n; i++) g[i] = inf;
for(int i = 1; i <= n; i++){
L[i] = lower_bound(g, g + n, h[i]) - g;
g[L[i]] = min(g[L[i]], h[i]), lis = Max(lis, L[i]);
}
for(int i = 0; i <= n; i++) g[i] = 0; g[0] = -inf;
for(int i = n; i >= 1; i--){
R[i] = lower_bound(g, g + n, -h[i]) - g;
g[R[i]] = min(g[R[i]], -h[i]);
}
for(int i = 1; i <= n; i++)
if(L[i] + R[i] - 1 == lis) pos[i] = L[i], tot[pos[i]]++;
for(int i = 1; i <= n; i++)
S1.ins(S1.rt[i], S1.rt[i-1], 1, n, L[i], h[i]);
for(int i = n; i >= 1; i--)
S2.ins(S2.rt[i], S2.rt[i+1], 1, n, R[i], -h[i]);
for(int i = 1, x, y; i <= m; i++){
read(x), read(y); int res = 0;
if(x > 1) res += S1.query(S1.rt[x-1], 1, n, y);
if(x < n) res += S2.query(S2.rt[x+1], 1, n, -y);
printf("%d\n", max(res + 1, (pos[x] && (tot[pos[x]] == 1)) ? lis - 1 : lis));
}
return 0;
}

Codeforces 650 D. Zip-line的更多相关文章

  1. [codeforces 549]G. Happy Line

    [codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...

  2. codeforces 251A Points on Line(二分or单调队列)

    Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...

  3. codeforces 650 C. Watchmen(数学公式)

    C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...

  4. codeforces 431 B Shower Line【暴力】

    题意:给出五个人的编号,分别为 1 2 3 4 5,他们在排队, 最开始的时候,1和2可以交谈,3和4可以交谈 然后1走了之后,2和3交谈,4和5可以交谈 2走了之后,3和4可以交谈, 3走了之后,4 ...

  5. 【Codeforces 650 D】Zip-line

    题意:给一个序列以及\(n\)个查询,每一个查询是问(假装)把第\(a_i\)个数改为\(b_i\)之后原序列的最长上升子序列的长度. 思路:线段树优化\(dp\). 肯定离线做啊. 首先我们考虑\( ...

  6. Codeforces 1159F Winding polygonal line(叉积)

    其实这个几何写起来还是比较方便,只用到了叉积.首先我们贪心的考虑一种情况,对于任意给定的LR串,我们起点的选择肯定是在这些点围成的凸包端点上,对于这样的起点来说,他对于L或者R都是有选择的机会,而且一 ...

  7. @codeforces - 594E@ Cutting the Line

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个字符串 s 与正整数 k.现在你需要进行恰好一次操作: ...

  8. 【Codeforces Round 650】Codeforces #334 (Div. 1)

    模拟CF650,ABC三题,RK90 Codeforces 650 A 思路:首先看式子 \(\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}=|x_i-x_j|+|y_i-y_j|\) ...

  9. Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树

    D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...

随机推荐

  1. MagicB.0—怎样设置电脑自动关机?

    天太晚了,该睡觉了,可是你的东西也许正在下载,软件正在更新,总之电脑还有一些工作没有完成,又不需要你人为的守着,随他去吧!可是电脑已经工作了一天了,它也要休息一下,再者也不能浪费电力资源呀,那么就来使 ...

  2. Spring整合Quartz分布式调度

    前言 为了保证应用的高可用和高并发性,一般都会部署多个节点:对于定时任务,如果每个节点都执行自己的定时任务,一方面耗费了系统资源,另一方面有些任务多次执行,可能引发应用逻辑问题,所以需要一个分布式的调 ...

  3. c++刷题(6/100)最长上升子序列

    题目一:区间子数组个数 给定一个元素都是正整数的数组A ,正整数 L 以及 R (L <= R). 求连续.非空且其中最大元素满足大于等于L 小于等于R的子数组个数. 例如 : 输入: A = ...

  4. JS中短路运算符&&和||

    在JS函数中我们经常会使用到短路运算符,主要是逻辑与(&&) 和 逻辑或(||) 1.逻辑与 && 的运算方式 var a = 5 && 6; cons ...

  5. php常用函数——数学函数

    php常用函数——数学函数

  6. 【zTree】zTree的3.5.26静态树与动态树(实用)

    1.静态树: 目录结构:(css与js为下载的原文件夹)

  7. 3-Python内置结构-列表

    目录 1 Python内置数据结构 1.1 数值型 1.2 math模块 1.3 round圆整 1.4 常用的其他函数 1.5 类型判断 2 列表 2.1 索引访问 2.2 列表和链表的区别 2.3 ...

  8. DAY1-GO初识(概述)

    一.概述 1.特征 1.1.语法简单:规则严谨.保留指针.但默认阻止指针运算.将切片和字典作为内置类型.更好的维护性: 1.2.并发模型:运行时用GOroutine,一个关键字.简单而自然:搭配cha ...

  9. python实战===python程序打包成exe

    推荐PyInstaller项目www.pyinstaller.org   安装方法: 先跑pip install pywin32再跑pip install pyinstaller即可 可用一句命令打包 ...

  10. IT人员必备linux安全运维之Ssh用途、安全性、身份认证以及配置……【转】

    SSH一般用途 提供shell,解决telnet不安全的传输 1.修改默认ssh默认端口 vi /etc/ssh/sshd_config 修改之后重启 >systemctl restart ss ...