D. Zip-line

题目连接:

http://www.codeforces.com/contest/650/problem/D

Description

Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long as possible but he doesn't remember exactly the heights of all trees in the forest. He is sure that he remembers correct heights of all trees except, possibly, one of them.

It is known that the forest consists of n trees staying in a row numbered from left to right with integers from 1 to n. According to Vasya, the height of the i-th tree is equal to hi. The zip-line of length k should hang over k (1 ≤ k ≤ n) trees i1, i2, ..., ik (i1 < i2 < ... < ik) such that their heights form an increasing sequence, that is hi1 < hi2 < ... < hik.

Petya had been in this forest together with Vasya, and he now has q assumptions about the mistake in Vasya's sequence h. His i-th assumption consists of two integers ai and bi indicating that, according to Petya, the height of the tree numbered ai is actually equal to bi. Note that Petya's assumptions are independent from each other.

Your task is to find the maximum length of a zip-line that can be built over the trees under each of the q assumptions.

In this problem the length of a zip line is considered equal to the number of trees that form this zip-line.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 400 000) — the number of the trees in the forest and the number of Petya's assumptions, respectively.

The following line contains n integers hi (1 ≤ hi ≤ 109) — the heights of trees according to Vasya.

Each of the following m lines contains two integers ai and bi (1 ≤ ai ≤ n, 1 ≤ bi ≤ 109).

Output

For each of the Petya's assumptions output one integer, indicating the maximum length of a zip-line that can be built under this assumption.

Sample Input

4 4

1 2 3 4

1 1

1 4

4 3

4 5

Sample Output

4

3

3

4

Hint

题意

给你n个数,m个询问

每次单点修改,然后问你现在整个序列的lis长度。

修改完之后,要求修改回去。

题解:

离线做,在线的话,得用持久化线段树。我的智障队友就是持久化线段树强行在线过的。

我们维护四个东西,dp1[i]表示从1开始到第i个位置的最长上升子序列长度,dp2[i]表示从n开始到第i个位置的最长递减子序列长度。dp3[i]表示第i个询问的那个位置从1开始到第x(即询问的位置)个位置的最长上升子序列长度,dp4[i]表示递减。

假如询问是x,y那么

然后我们判断一下第x个位置是不是lis的关键位置,是的话,ans=lis。否则的话,ans=lis-1。关键位置就是这个位置是全局lis不可替代的一个数。

然后ans = max(ans,dp3[i]+dp4[i]-1)这个很显然……

然后就完了。

细节部分,就是需要离散化一下,然后就没了,感觉还是很好写的。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7; int n,m;
int a[maxn],dp1[maxn],dp2[maxn];
typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , lazy;
void update(SgTreeDataType v)
{
sum = max(sum,v);
}
};
struct Seg
{
treenode tree[maxn*4];
inline void push_down(int o)
{ } inline void push_up(int o)
{
tree[o].sum = max(tree[2*o].sum , tree[2*o+1].sum);
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = 0;
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
}
} inline void update(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].update(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) update(QL,QR,v,o*2);
if (QR > mid) update(QL,QR,v,o*2+1);
push_up(o);
}
} inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res = 0;
if (QL <= mid) res = max(res, query(QL,QR,2*o));
if (QR > mid) res = max(res, query(QL,QR,2*o+1));
push_up(o);
return res;
}
}
}L,R; map<int,int> H;
vector<int> P;
struct node
{
int x,y;
}Q[maxn];
vector<pair<int,int> > Qx[maxn];
int cnt[maxn];
int dp3[maxn],dp4[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),P.push_back(a[i]);
for(int i=1;i<=m;i++)
scanf("%d%d",&Q[i].x,&Q[i].y),P.push_back(Q[i].y); sort(P.begin(),P.end());
P.erase(unique(P.begin(),P.end()),P.end());
for(int i=0;i<P.size();i++)
H[P[i]]=i+1;
for(int i=1;i<=n;i++)
a[i]=H[a[i]];
for(int i=1;i<=m;i++)
Q[i].y=H[Q[i].y],Qx[Q[i].x].push_back(make_pair(i,Q[i].y));
L.build_tree(1,P.size()+5,1);
for(int i=1;i<=n;i++)
{
dp1[i]=L.query(1,a[i]-1,1)+1;
for(int j=0;j<Qx[i].size();j++)
{
int id = Qx[i][j].first;
int x = Qx[i][j].second;
dp3[id]=L.query(1,x-1,1)+1;
}
L.update(a[i],a[i],dp1[i],1);
}
reverse(a+1,a+1+n);
R.build_tree(1,P.size()+5,1);
for(int i=1;i<=n;i++)
{
dp2[i]=R.query(a[i]+1,P.size(),1)+1;
for(int j=0;j<Qx[n-i+1].size();j++)
{
int id = Qx[n-i+1][j].first;
int x = Qx[n-i+1][j].second;
dp4[id]=R.query(x+1,P.size(),1)+1;
}
R.update(a[i],a[i],dp2[i],1);
}
int Lis = 0;
for(int i=1;i<=n;i++)
Lis = max(Lis,dp1[i]);
for(int i=1;i<=n;i++)
if(dp1[i]+dp2[n-i+1]==Lis+1)
cnt[dp1[i]]++;
for(int i=1;i<=m;i++)
{
int ans = Lis;
int x = Q[i].x;
if(dp1[x]+dp2[n-x+1]==Lis+1&&cnt[dp1[x]]==1)ans--;
ans=max(ans,dp3[i]+dp4[i]-1);
printf("%d\n",ans);
}
}

Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树的更多相关文章

  1. Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树

    E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #345 (Div. 1) D - Zip-line 带单点修改的LIS 主席树 | 离线树状数组

    D - Zip-line #include<bits/stdc++.h> #define LL long long #define fi first #define se second # ...

  3. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  4. Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树

    如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...

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

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

  6. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  7. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

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

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

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

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

随机推荐

  1. mysql 复制表结构 / 从结果中导入数据到新表

    这只会复制结构: mysql> create table a like mysql1; Query OK, 0 rows affected (0.03 sec) mysql> desc a ...

  2. Perl6 Bailador框架(3):路径匹配

    use v6; use Bailador; =begin pod 注意的是, 当/:one设置时 虽然你有/admin或/about, 但这个/:one不会跟现有的匹配 只跟没有的匹配: 也就是说, ...

  3. 【bzoj1923】外星千足虫

    这个gauss消元有点naive啊. 由于只有01,位操作显然是方便的多. 那么用bitset代替之前的增广矩阵就行了. #include<bits/stdc++.h> #define N ...

  4. C基础 大文件读取通过标准库

    引言 - 问题的构建 C大部分读取文件的时候采用fgetc, 最近在使用过程中发现性能不是很理想.都懂得fgetc每次只能读取一个字符, IO操作太频繁. 所以性能低. 本文希望通过标准库函数frea ...

  5. Python-生成器/你不知道的点

    1.什么是生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素, ...

  6. android intent 传数据

    1. 基本数据类型 Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起点和目标 ...

  7. clearcase command (linux 常用命令)

    http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m0/index.jsp?topic=/com.ibm.rational.clearcase.h ...

  8. JVM对象分配和GC分布【JVM】

    最近在学习java基础结构,刚好学到了jvm,总结了以下并可以结合思维导图认识以下Jvm的对象: 栈:什么是栈? 先说一下栈的数据结构吧,栈它是一种先进后出的数据结构(FILO),跟队列刚好相反(先进 ...

  9. dubbo支持的远程调用方式

    dubbo RPC(二进制序列化 + tcp协议).http invoker(二进制序列化 + http协议,至少在开源版本没发现对文本序列化的支持).hessian(二进制序列化 + http协议) ...

  10. DNS解析原理与Bind部署DNS服务

    DNS是什么? DNS(Domain Name System,域名系统)是互联网上最核心的带层级的分布式系统,它负责把域名转换为IP地址.反查IP到域名的反向解析以及宣告邮件路由等信息,使得基于域名提 ...