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