Codeforces 650 D. Zip-line
$ >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的更多相关文章
- [codeforces 549]G. Happy Line
[codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...
- codeforces 251A Points on Line(二分or单调队列)
Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...
- codeforces 650 C. Watchmen(数学公式)
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- codeforces 431 B Shower Line【暴力】
题意:给出五个人的编号,分别为 1 2 3 4 5,他们在排队, 最开始的时候,1和2可以交谈,3和4可以交谈 然后1走了之后,2和3交谈,4和5可以交谈 2走了之后,3和4可以交谈, 3走了之后,4 ...
- 【Codeforces 650 D】Zip-line
题意:给一个序列以及\(n\)个查询,每一个查询是问(假装)把第\(a_i\)个数改为\(b_i\)之后原序列的最长上升子序列的长度. 思路:线段树优化\(dp\). 肯定离线做啊. 首先我们考虑\( ...
- Codeforces 1159F Winding polygonal line(叉积)
其实这个几何写起来还是比较方便,只用到了叉积.首先我们贪心的考虑一种情况,对于任意给定的LR串,我们起点的选择肯定是在这些点围成的凸包端点上,对于这样的起点来说,他对于L或者R都是有选择的机会,而且一 ...
- @codeforces - 594E@ Cutting the Line
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个字符串 s 与正整数 k.现在你需要进行恰好一次操作: ...
- 【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|\) ...
- 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 ...
随机推荐
- 【BZOJ】1497: [NOI2006]最大获利 最大权闭合子图或最小割
[题意]给定n个点,点权为pi.m条边,边权为ci.选择一个点集的收益是在[点集中的边权和]-[点集点权和],求最大获利.n<=5000,m<=50000,0<=ci,pi<= ...
- 【BZOJ】3495: PA2010 Riddle 2-SAT算法
[题意]有n个城镇被分成了k个郡,有m条连接城镇的无向边.要求给每个郡选择一个城镇作为首都,满足每条边至少有一个端点是首都.n,m,k<=10^6. [算法]2-SAT,前后缀优化建图 [题解] ...
- CALayer---iOS-Apple苹果官方文档翻译之CALayer
CHENYILONG Blog CALayer---iOS-Apple苹果官方文档翻译之CALayer CALayer /*技术博客http://www.cnblogs.com/ChenYilong/ ...
- Linux 查看网卡流量【转】
我的系统式RHEL5. 在linux下,查看网卡流量的方法有很多.下面先记录几个,和他们的大概用法.已被以后之需. 一:iptraf 一个很不错的工具.RHEL5 iso自带有,我 ...
- 《深入理解Java虚拟机》笔记--第十三章、线程安全与锁优化
先保证并发的正确性,然后在此基础上来实现高效. 线程安全: 当多个线程访问一个对象时,如果不考虑这些线程在运行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方进行任何其他的协调操 ...
- ps的各种参数
1.CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 2.内存消耗最多的前10个进程 ps auxw|head -1;ps a ...
- springboot + swagger2 生成api文档
直接贴代码: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...
- linux---centos7 安装chromedriver
1.安装浏览器 指定yum 源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.re ...
- HTML常用标签及其属性
基本 <html>…</html> 定义 HTML 文档 <head>…</head> 文档的信息 <meta> HTML 文档的元信息 & ...
- 洛谷P2692 覆盖 题解
题目传送门 这道题一开始想使用二维的bool型数组来存,最后统计.但看到数据范围... 所以就改用两个bool型数组(一维),分别储存横.列,最后将横.列面积求出来,再减去重复算的面积(横的个数*列的 ...