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

题意:给定一个数组,Q次询问,询问之间彼此独立。每次询问改动一个数,问改动后的LIS。

思路:同今年南京(湘潭)的邀请赛J题,当时思路差不多对了,但是想复杂了,我和陈队花了一个半小时还是写WA了。注意到新LIS和原来的LIS最多相差1。也就是max-1,max,max+1三种情况,那么我们分两步去验证即可:

  第一步,考虑没了原来的值:假设原来的LIS一定经过更改点,那么先把ans赋值为max-1;否则赋值ans为max。

第二步,考虑有了新来的值:即用到新的值,那么预处理的时候从前向后后从后向前扫,两遍预处理完,ans=max(ans,f[i]+g[i]-1)。

第二步用线段树或者树状数组即可搞定,常规操作。

第一步,其实也不难想,当时就是这里想复杂了。我们得到LISmax,然后f[i]表示以i为结束的LIS,g[i]表示以i为起点的LIS。如果f[i]+g[i]-1==max,那么种数那么cnt[f[i]]++;   讨论替换i点时,如果cnt[f[i]]==1,则说明原来的LIS必定经过i点。

CodeForces - 650D:Zip-line (LIS & DP)的更多相关文章

  1. CodeForces - 1073E :Segment Sum (数位DP)

    You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...

  2. Codeforces Gym101201B:Buggy Robot(BFS + DP)

    题目链接 题意 给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作. 思路 和上次比赛做的 ...

  3. SPOJ:House Fence(分治&DP)

    "Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...

  4. Codeforces Gym100502H:Clock Pictures(KMP算法)

    http://codeforces.com/gym/100502/attachments 题意:有两个时钟上面有n个指针,给出的数字代表指针的角度.问能否在某一时刻使得两个时钟的指针重合. 思路:容易 ...

  5. Codeforces 777E:Hanoi Factory(贪心+栈)

    http://codeforces.com/problemset/problem/777/E 题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且b ...

  6. Codeforces 758C:Unfair Poll(思维+模拟)

    http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...

  7. codeforces 486 D. Valid Sets(树形dp)

    题目链接:http://codeforces.com/contest/486/problem/D 题意:给出n个点,还有n-1条边的信息,问这些点共能构成几棵满足要求的树,构成树的条件是. 1)首先这 ...

  8. POJ 2533 Longest Ordered Subsequence (LIS DP)

    最长公共自序列LIS 三种模板,但是邝斌写的好像这题过不了 N*N #include <iostream> #include <cstdio> #include <cst ...

  9. BZOJ 1026:windy数(数位DP)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1026 1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memor ...

随机推荐

  1. android 对话框中的进度条 (ProgressDialog)

    from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...

  2. 通过调节坐标进行jfree图的放大缩小

    http://blog.csdn.net/lt1983lt/article/details/5665085 import Java.awt.BorderLayout;import java.awt.C ...

  3. IMX6Q RTC驱动分析

    对于在工作中学习驱动的,讲究的是先使用,再理解.好吧,我们来看看板子里是如何注册的? 在板文件里,它的注册函数是这样的: imx6q_add_imx_snvs_rtc() 好吧,让我们追踪下去: 1 ...

  4. 【转载】iptables、tc和ip命令

    2.3 CommandListener中的命令 CL一共定义了11个命令,这些命令充分反映了Netd在Android系统中网络管理和控制方面的职责.本节首先介绍Linux系统中常用的三个网络管理工具, ...

  5. php数组函数-array_merge()

    array_merge()函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值.如果数组是数字 索引,则键名会以连续方式重新索引. 注:如果仅仅向array_merg ...

  6. Cocos2d-x项目移植到WP8系列之一:前传

    原文链接: http://www.cnblogs.com/zouzf/p/3969993.html 许久没动笔了,随想一直都有动笔的想法,但拖来拖去,归根到底还是一个懒字吧 .发现人的惰性真是太强大了 ...

  7. MySQL数据库基本操作(一)

    进入mysql 本地连接: mysql -u用户名 -p 输入密码 qwe123 mysql -uroot -pqwe123 sudo apt-get install mysql-server # p ...

  8. iOS应用适配IPV6

    网络收集,连接如下: 针对苹果iOS最新审核要求为应用兼容IPv6 iOS应用支持IPV6,就那点事儿 iOS 适配iPV6的修改(AF及其他第三方库)

  9. poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】

    题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...

  10. java进阶之-Maven,svn,git,maven合拼多个项目

    git的使用介绍(写很容易懂得哦) maven合拼多个项目(写得很好哦) MAVEN作用:统一开发规范与工具:统一管理jar包 1.下载MAVEN  下载绿色版的面安装 2.环境配置 eclipse想 ...