LCIS

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7193    Accepted Submission(s): 3069

Problem Description
Given n integers.
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].
 
Input
T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
OR
Q A B(0<=A<=B< n).
 
Output
For each Q, output the answer.
 
Sample Input
1
10 10
7 7 3 3 5 9 9 8 1 8
Q 6 6
U 3 4
Q 0 1
Q 0 5
Q 4 7
Q 3 5
Q 0 2
Q 4 6
U 6 10
Q 0 9
 
Sample Output
1
1
4
2
3
1
2
5
 
Author
shǎ崽
题意:
n个数,m次操作,u a,b 表示a位置的数变成b,q a,b 表示询问[a,b]中最大递增子串是多大。
代码:
//最大递增子串sub可能是区间[l,r]中前缀部分,后缀部分或者中间部分(横跨两个子区间)。
//线段树维护信息:val(表节点的值),sub(最长上升子序列的长度),pre(最
//长上升前缀的长度),suf(最长上升后缀的长度).由于一个区间[L,R]内的LCIS,
//可能在左半边,也可能跨越两边,也可能在右半边,
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int val[maxn],suf[maxn*],pre[maxn*],sub[maxn*];
void pushup(int id,int l,int r)
{
int m=(l+r)>>;
sub[id]=max(sub[id<<],sub[id<<|]);
if(val[m]<val[m+]) sub[id]=max(sub[id],suf[id<<]+pre[id<<|]);
//如果两个子区间连续,左子区间后缀+右子区间前缀
pre[id]=pre[id<<];
if((pre[id]==m-l+)&&val[m]<val[m+]) pre[id]+=pre[id<<|];
//如果前缀是左子区间的所有数并且左子区间右边界值小于右子区间左边界值
suf[id]=suf[id<<|];
if((suf[id]==r-m)&&val[m]<val[m+]) suf[id]+=suf[id<<];
//如果后缀是右子区间的所有数并且左子区间右边界值小于右子区间左边界值
}
void build(int id,int l,int r)
{
if(l==r){
sub[id]=suf[id]=pre[id]=;
return;
}
int m=(l+r)>>;
build(id<<,l,m);
build(id<<|,m+,r);
pushup(id,l,r);
}
void update(int a,int b,int id,int l,int r)
{
if(l==r){
val[l]=b;
sub[id]=suf[id]=pre[id]=;
return;
}
int m=(l+r)>>;
if(a<=m) update(a,b,id<<,l,m);
else update(a,b,id<<|,m+,r);
pushup(id,l,r);
}
int query(int ql,int qr,int id,int l,int r)
{
if(ql<=l&&qr>=r) return sub[id];
int m=(l+r)>>;
if(qr<=m) return query(ql,qr,id<<,l,m);
else if(ql>m) return query(ql,qr,id<<|,m+,r);
int subx=max(query(ql,qr,id<<,l,m),query(ql,qr,id<<|,m+,r));
int prex=min(qr-m,pre[id<<|]);//右子区间中在查询范围中的前缀
int sufx=min(m-ql+,suf[id<<]);//左子区间中在查询范围中的后缀
if(val[m]<val[m+]) subx=max(subx,prex+sufx);
return subx;
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&val[i]);
build(,,n);
char ch[];int a,b;
while(m--){
scanf("%s%d%d",ch,&a,&b);
if(ch[]=='U') update(a+,b,,,n);
else if(ch[]=='Q') printf("%d\n",query(a+,b+,,,n));
}
}
return ;
}

  

最新文章

  1. tomcat解决加载JSP文件过大错误
  2. mint安装相关数据库lib
  3. 实体写到redis写不进去--误把类当成实体类
  4. 最牛X的GCC 内联汇编
  5. mq_open
  6. POJ 2942.Knights of the Round Table (双连通)
  7. Ubuntu 11.10 安装GMONE3,卸载 UNITY和UNITY 2D
  8. FreeBSD 系统的配置.
  9. [PKU2389]Bull Math (大数运算)
  10. Hibernate学习笔记①
  11. LintCode题解之斐波纳契数列
  12. openlayers一:显示地图与鼠标地理坐标
  13. K-means算法性能评估及其优化
  14. MariaDB dos 下连接
  15. 【新特性】JDK10
  16. Debian搭建WordPress
  17. 大明A+B
  18. 使用map()的小陷阱:parseInt
  19. 17秋 软件工程 团队第五次作业 Alpha Scrum11
  20. 网页启用Gzip压缩 提高浏览速度

热门文章

  1. Python数据分析基础——Numpy tutorial
  2. Python入门(4)
  3. 统计学习三:2.K近邻法代码实现(以最近邻法为例)
  4. 基础数据类型-dict
  5. 文件操作---基于python
  6. 二叉搜索树(BST)---python实现
  7. C#中堆和栈的区别?
  8. 【week3】psp (技术随笔)
  9. 【Linux】- CentOS查看IP
  10. phpcms 最多上传 10 个附件 解决办法