题意概述:

给出一个长度为N的序列和M组询问,问假设把某个位置的值改成另一个给出的值之后,序列的最长上升子序列的长度。

N,M<=400000.

分析:

考虑某个位置的值改动后这个位置和最长上升子序列(lis)的关系:

1、这个位置包含在lis中(这种情况答案可能+1,可计算经过这个点的lis来等效决策)。

2、这个位置不包含在lis中,那么需要看是否任意的lis都经过这个位置。如果是的话此决策的结果在原来长度基础上-1,否则就等于原来的长度。

有了大体思路,接下来想想维护。

任务1:对于任意位置x,求包含这个点的lis长度,令g1(i)表示1->N方向长度为i的lis的最长上升子序列的最后一个值的最小值,g2(i)表示N->1方向长度为i的最长下降子序列的最后一个值的最大值(更新就不说了,基础),如果知道位置x状态下恰好未更新的g1,g2数组内容,就同更新一样用前后以这个点结尾的最长序列长度相加-1就是此决策的结果。

任务2:这个实际上是重点,对于任意位置x,知道的东西和任务1一样,同时知道是否存在一对g1(a),g2(b)满足a+b=len(len是原序列lis长度)&&g1(a)<g2(b),如果存在那么此决策的结果为原来答案,否则为原来答案-1。

如果要强行在线的话三棵主席树维护一下两个g数组和一个vis数组(vis(x)表示对于当前状态来说g1(x),g2(len-x)是否合法,当前状态是否合法实际上就是看vis的所有值的和是否不为0)然后随便乱搞就可以了。

然而。。。我选择离线,询问按照位置排序,正反扫一遍序列处理第一种情况,然后再正着扫一遍把第二种情况用一个数组处理了就可以了。能离线何苦去在线码主席树呢(手动滑稽)(手动链表或者vector都可以帮忙离线)?!!

然而我还是弄了半天,污浊的机房CO2,QAQ

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int maxn=; int N,M,h[maxn];
struct que{
int id,pos,v;
friend bool operator < (que a,que b){
return a.pos<b.pos;
}
}q[maxn];
int g1[maxn],g2[maxn],cnt1,cnt2,ans[maxn],len,vis[maxn];
struct mlink{
static const int max_sz=;
int np,w[maxn],first[maxn],next[maxn];
mlink(){
np=,w[]=next[]=;
memset(first,,sizeof(first));
}
void ins(int i,int x) { w[++np]=x,next[np]=first[i],first[i]=np; }
void del(int i) { first[i]=next[first[i]]; }
int val(int i) { return w[first[i]]; }
}dd; void data_in()
{
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++) scanf("%d",&h[i]);
for(int i=;i<=M;i++){
scanf("%d%d",&q[i].pos,&q[i].v);
q[i].id=i;
}
sort(q+,q+M+);
}
bool cmp(int x,int y) { return x>y; }
void work()
{
for(int i=;i<=N;i++){
int j=lower_bound(q+,q+M+,(que){,i,})-q;
while(j<=M&&q[j].pos==i)
ans[q[j].id]=lower_bound(g1+,g1+cnt1+,q[j].v)-g1,j++;
int x=lower_bound(g1+,g1+cnt1+,h[i])-g1;
if(x>cnt1) cnt1=x;
g1[x]=h[i];
}
for(int i=N;i>=;i--){
int j=lower_bound(q+,q+M+,(que){,i,})-q;
while(j<=M&&q[j].pos==i)
ans[q[j].id]+=lower_bound(g2+,g2+cnt2+,q[j].v,cmp)-g2-,j++;
int x=lower_bound(g2+,g2+cnt2+,h[i],cmp)-g2;
if(x>cnt2) cnt2=x;
g2[x]=h[i];
dd.ins(x,g2[x]);
}
memset(g1,,sizeof(g1)); len=cnt1,cnt1=;
int x=lower_bound(g2+,g2+cnt2+,h[],cmp)-g2,sum=;
dd.del(x); g2[x]=dd.val(x);
if(g2[len]) vis[]=,sum++;
for(int i=;i<=N;i++){
int j=lower_bound(q+,q+M+,(que){,i,})-q;
while(j<=M&&q[j].pos==i)
ans[q[j].id]=max(ans[q[j].id],len-(sum==)),j++;
int x1=lower_bound(g1+,g1+cnt1+,h[i])-g1;
if(x1>cnt1) cnt1=x1; g1[x1]=h[i];
int x2=lower_bound(g2+,g2+cnt2+,h[i+],cmp)-g2;
dd.del(x2); g2[x2]=dd.val(x2);
if(!g2[x2]) cnt2--;
if((x1==len||g1[x1]<g2[len-x1])&&!vis[x1]) vis[x1]=,sum++;//请注意这一句的第一个条件
if(g1[len-x2]>=g2[x2]&&vis[len-x2]) vis[len-x2]=,sum--;
}
for(int i=;i<=M;i++) printf("%d\n",ans[i]);
}
int main()
{
data_in();
work();
return ;
}

Codeforces Round #345 Div.1 D.Zip-line 动态最长上升子序列的更多相关文章

  1. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  2. 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 ...

  3. Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力

    B. Beautiful Paintings 题目连接: http://www.codeforces.com/contest/651/problem/B Description There are n ...

  4. codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)

    Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...

  5. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  6. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  7. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  8. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  9. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  10. Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分

    D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...

随机推荐

  1. Linux学习——目录结构

    在Linux当中,一切皆为文件,包括目录也属于文件.FHS(Filesystem Hierarchy Standard)的出现对文件目录系统做出了统一规范. Linux的目录结构: / - 根 /bi ...

  2. Python 学习笔记(三)数字

    Python 数字 int 整型  是正或负整数  2 long 长整型  整数最后是一个大写或小写的L   2L float  浮点型 由整数部分和小数部分组成   2.0 complex 复数 小 ...

  3. IOS 浅谈闭包block的使用

    前言:对于ios初学者,block通常用于逆向传值,遍历等,会使用,但是可能心虚,会感觉block很神秘,那么下面就一起来揭开它的面纱吧. ps: 下面重点讲叙了闭包的概念,常用的语法,以及访问变量, ...

  4. jzoj5196. 【NOIP2017提高组模拟7.3】B (数论,巧妙的暴力)

    Description

  5. 使用Linux命名将代码上传到GitHub

    GitHub代码上传教程 https://my.oschina.net/baishi/blog/520791 这篇文章讲得挺清楚的,但是在上传的时候出现了问题 ! [rejected] master ...

  6. 今天看到的一篇文章:一位资深程序员大牛给予Java初学者的学习路线建议

    一位资深程序员大牛给予Java初学者的学习路线建议 持续学习!

  7. 设置Vim编辑器里Tab的长度,行号

    使用Vim编辑器写脚本时,经常会遇到多重循环语句,习惯上会用tab键来补齐.这时设置tab键占用的长度,可以调节界面的松紧度,使其达到令人满意的效果. 在针对个别用户和所有用户来设置时,与编辑SSH相 ...

  8. spring的事务处理

    说到事务,无非就是事务的提交commit和回滚rollback. 事务是一个操作序列,这些操作要么全部都执行成功,事务去提交,要么就是有一个操作失败,事务去回滚. 要知道事务的4大特性ACID.即原子 ...

  9. 【linux下dhcp服务的简单搭建及优化部署】

    dhcp server: 1::vim /etc/sysconfig/network-scripts/ifcfg-scfg:配置 server的 static IP: 2:vim /etc/dhcpd ...

  10. vs2017升级、安装

    图解VS 2017升级并安装.NET Core 2.1 SDK https://jingyan.baidu.com/album/ff42efa9fb5816c19e2202ef.html?picind ...