$ >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. Centos tomcat jmx 远程连接

    jmx配置: -Dcom.sun.management.jmxremote-Dcom.sun.management.jmxremote.authenticate=false-Dcom.sun.mana ...

  2. 【CodeForces】576 C. Points on Plane

    [题目]C. Points on Plane [题意]给定坐标系中n个点的坐标(范围[0,10^6]),求一种 [ 连边形成链后总长度<=2.5*10^9 ] 的方案.n<=10^6. [ ...

  3. SQL Server 高级SQL

    查询view 的列和列数据类型 SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'vi ...

  4. idea中tomcat乱码问题解决

    在idea中经常遇到jsp的乱码问题,原因是编码不是UTF-8的问题,这次来彻底解决idea的编码问题 首先设置idea编辑器的编码: File-Setting设置如下 然后配置tomcat的编码问题 ...

  5. angular导出文件保存在本地

    $scope.ev_click = function(obj){ var ev = document.createEvent("MouseEvents"); ev.initMous ...

  6. React 16 源码瞎几把解读 【二】 react组件的解析过程

    一.一个真正的react组件编译后长啥样? 我们瞎几把解读了react 虚拟dom对象是怎么生成的,生成了一个什么样的解构.一个react组件不光由若干个这些嵌套的虚拟dom对象组成,还包括各种生命周 ...

  7. Python如何实现文本转语音

    准备 我测试使用的Python版本为2.7.10,如果你的版本是Python3.5的话,这里就不太适合了. 使用Speech API 原理 我们的想法是借助微软的语音接口,所以我们肯定是要进行调用 相 ...

  8. 一致性hash理解

    在做memcached分布式集群时往往要用到一致性hash算法来调节缓存数据的分布. 通常的hash算法是以服务器数量N作为模数,使用key%N的值来获得最终位置,显然当服务器数量发生变化即N发生变化 ...

  9. java版云笔记(六)之AOP

    今天主要是利用aop技术追加service的响应时间的计算和异常的日志记录. AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object O ...

  10. 让R与Python共舞

    转载:http://ices01.sinaapp.com/?p=129      R(又称R语言)是一款开源的跨平台的数值统计和数值图形化展现 工具.通俗点说,R是用来做统计和画图的.R拥有自己的脚本 ...