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.

Examples
input
4 4
1 2 3 4
1 1
1 4
4 3
4 5
output
4
3
3
4
input
4 2
1 3 2 6
3 5
2 4
output
4
3
Note

Consider the first sample. The first assumption actually coincides with the height remembered by Vasya. In the second assumption the heights of the trees are (4, 2, 3, 4),
in the third one they are (1, 2, 3, 3) and in the fourth one they are (1, 2, 3, 5).

题意:给你n个数,有q个询问,每一次替换c上的位置为d,问替换后的最长严格上升子序列的长度是多少。

思路:如果普通的替换再查找肯定超时了,所以我们要用线段树来处理。我们设f[i],g[i]分别为以i位置为尾点和起始点的最长严格上升子序列的长度,这个可以用线段树O(nlogn)的复杂度求出来,记录最长上升子序列的长度为maxlen,然后我们再设f1[i],f2[i]表示询问i替换后,询问替换的位置为c,以c位置为尾点和起始点的最长上升子序列长度。接下来我们要判断替换的节点是不是"关键点","关键点"的意思是,如果原来序列没有这个位置的点,那么原来序列的最长上升子序列的长度达不到maxlen。那么这个要怎么判断呢,我们可以开一个数组cnt[i],表示对于一个节点j,以j为尾节点的最长上升子序列的长度为i,且f[j]+g[j]-1==maxlen的这样符合条件的j点的个数总和。对于每一个位置,先判断f[i]+g[i]-1是不是等于maxlen,如果等于maxlen,那么我们就把cnt[f[i]]++。然后对于每一个询问,ans=max(是不是为关键点?maxlen:maxlen-1
,f1[i]+g1[i]-1 )。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 400050
vector<pair<int,int> >ques[maxn]; //<idx,num>
int ans[maxn],a[maxn],pos[2*maxn];
int f1[maxn],g1[maxn],f[maxn],g[maxn];
int cnt[maxn];
int c[maxn],d[maxn]; struct node1{
struct node{
int l,r,maxlen;
}b[8*maxn];
void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
b[th].maxlen=0;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,th*2);
build(mid+1,r,th*2+1);
}
int question(int l,int r,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
return b[th].maxlen;
}
mid=(b[th].l+b[th].r)/2;
if(r<=mid)return question(l,r,th*2);
else if(l>mid)return question(l,r,th*2+1);
return max(question(l,mid,th*2),question(mid+1,r,th*2+1) );
}
void update(int idx,int num,int th)
{
int mid;
if(b[th].l==idx && b[th].r==idx){
b[th].maxlen=num;return;
}
mid=(b[th].l+b[th].r)/2;
if(idx<=mid)update(idx,num,th*2);
else update(idx,num,th*2+1);
b[th].maxlen=max(b[th*2].maxlen,b[th*2+1].maxlen);
}
}L,R; int main()
{
int n,m,i,j,tot;
while(scanf("%d%d",&n,&m)!=EOF)
{
int tot=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
tot++;pos[tot]=a[i];
}
for(i=1;i<=m;i++){
scanf("%d%d",&c[i],&d[i]);
ques[c[i] ].push_back(make_pair(i,d[i] ) );
tot++;pos[tot]=d[i];
}
sort(pos+1,pos+1+tot);
tot=unique(pos+1,pos+1+tot)-pos-1;
L.build(1,tot,1);
R.build(1,tot,1); int maxlen,num,t;
maxlen=0; for(i=1;i<=n;i++){
for(j=0;j<ques[i].size();j++){
t=lower_bound(pos+1,pos+1+tot,ques[i][j].second)-pos;
if(t==1)num=1;
else num=L.question(1,t-1,1)+1;
f1[ques[i][j].first ]=num;
}
t=lower_bound(pos+1,pos+1+tot,a[i])-pos;
if(t==1)f[i]=1;
else f[i]=L.question(1,t-1,1)+1;
L.update(t,f[i],1);
maxlen=max(maxlen,f[i]);
}
for(i=n;i>=1;i--){
for(j=0;j<ques[i].size();j++){
t=lower_bound(pos+1,pos+1+tot,ques[i][j].second)-pos;
if(t==tot)num=1;
else num=R.question(t+1,tot,1)+1;
g1[ques[i][j].first ]=num;
}
t=lower_bound(pos+1,pos+1+tot,a[i])-pos;
if(t==tot)g[i]=1;
else g[i]=R.question(t+1,tot,1)+1;
R.update(t,g[i],1);
}
for(i=1;i<=n;i++){
cnt[i]=0;
} for(i=1;i<=n;i++){
if(f[i]+g[i]-1==maxlen){
cnt[f[i] ]++;
}
} int ans;
for(i=1;i<=m;i++){
if(f[c[i] ]+g[c[i] ]-1==maxlen && cnt[f[c[i] ] ]==1 ){
ans=maxlen-1;
}
else ans=maxlen;
ans=max(f1[i]+g1[i]-1,ans );
printf("%d\n",ans);
}
}
return 0;
} /*
15 14
76 9 32 82 40 91 46 5 12 69 44 97 30 13 29
4 73
13 84
14 51
5 99
7 47
14 32
4 12
11 20
9 65
15 95
10 26
5 25
2 62
11 81
*/

codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)的更多相关文章

  1. Codeforces #345 Div.1

    Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...

  2. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  3. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set

    F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...

  4. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  5. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

  6. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  7. Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  8. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  9. Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. update 表名 set 某列名=now() where user in('user1','user2','user3');

    update 表名  set 某列名=now() where user in('user1','user2','user3');

  2. UVA - 185 Roman Numerals

    题目链接: https://vjudge.net/problem/UVA-185 思路: 剪枝.回溯 注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题! 思路参考 ...

  3. 百度文库Word下载器

    最近我妈的文库VIP用完了,但还有很多资源要下载,于是我便在网上找下载工具. 总算找到个完美的!(虽然没界面) 既然没界面,那就自己写一个呗! 原作者 该程序的下载和写入部分由地球守卫者制作 原文链接 ...

  4. 关于Vue v-model你需要知道的一切

    ​v-model是Vue的一个指令,它提供了input和form数据之间或两个组件之间的双向数据绑定. 这在Vue开发中是一个简单的概念,但是v-model的真正威力需要一些时间才能理解. 到本教程结 ...

  5. 获取Java线程转储的常用方法

    1. 线程转储简介 线程转储(Thread Dump)就是JVM中所有线程状态信息的一次快照. 线程转储一般使用文本格式, 可以将其保存到文本文件中, 然后人工查看和分析, 或者使用工具/API自动分 ...

  6. 转 12 jmeter性能测试实战--web程序

    12 jmeter性能测试实战--web程序   项目背景 项目:XX网站环境:Windows需求:并发登录的性能测试场景:1s增加2个线程,运行2000次(线程数20,Ramp-Up seconds ...

  7. 【Android初级】如何实现一个“模拟后台下载”的加载效果(附源码)

    在Android里面,后台的任务下载功能是非常常用的,比如在APP Store里面下载应用,下载应用时,需要跟用户进行交互,告诉用户当前正在下载以及下载完成等. 今天我将通过使用Android的原生控 ...

  8. 系列trick - 建图

    对偶图 主体思想:平面图的割,等价于对偶图的路 例题:[BeiJing2006]狼抓兔子 网上有114514篇题解,这里不赘述 点变边 主体思想:点带点权,而要在点上实现一些在边上的问题,比如最小割点 ...

  9. xss靶场详解

    一个XSS靶场练习记录 https://blog.csdn.net/qq_41500251/article/details/101116697 001.level 1 反射型 1.观察源码 <s ...

  10. Prometheus 监控之 Blackbox_exporter黑盒监测

    Prometheus 监控之 Blackbox_exporter黑盒监测 1.blackbox_exporter概述 1.1 Blackbox_exporter 应用场景 2.blackbox_exp ...