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 ;
}

  

HDU3308 线段树(区间合并)的更多相关文章

  1. HDU3308 线段树区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 ,简单的线段树区间合并. 线段树的区间合并:一般是要求求最长连续区间,在PushUp()函数中实 ...

  2. LCIS hdu3308 (线段树 区间合并)

    题意: 有两种操作  一种是单点改为b  一种是给出区间ab  区间ab的最大上升子序列个数.. 线段树目前学了三种  第一种单点操作很简单   第二种区域操作加上懒惰标记即可 现在这种 为区间合并. ...

  3. hdu3308 线段树——区间合并

    更新一个点: 求某个区间的最长连续上升序列: 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 #include <cstdio> #in ...

  4. hdu3308 线段树 区间合并

    给n个数字 U表示第A个数改为B.A是从0开始. Q输出最大的递增序列个数. 考虑左边,右边,和最大的. #include<stdio.h> #define lson l,m,rt< ...

  5. hdu-3308 LCIS (线段树区间合并)

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  7. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  8. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  9. HYSBZ 1858 线段树 区间合并

    //Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...

  10. poj3667 线段树 区间合并

    //Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. selenium常用操作方法

    Webdriver中比较常用的操作元素的方法: clear() 清除输入框的默认内容 send_keys("xxx") 在一个输入框里输入xx内容 ——如果输入中文,则需要在脚本开 ...

  2. Trie 树——搜索关键词提示

    当你在搜索引擎中输入想要搜索的一部分内容时,搜索引擎就会自动弹出下拉框,里面是各种关键词提示,这个功能是怎么实现的呢?其实底层最基本的就是 Trie 树这种数据结构. 1. 什么是 "Tri ...

  3. js经典试题之运算符的优先级

    js经典试题之运算符 1.假设val已经声明,可定义为任何值.则下面js代码有可能输出的结果为: console.log('Value is ' + (val != '0') ? 'define' : ...

  4. 算法与数据结构5.1 Just Sort

    ★实验任务 给定两个序列 a b,序列 a 原先是一个单调递增的正数序列,但是由于某些 原因,使得序列乱序了,并且一些数丢失了(用 0 表示).经过数据恢复后,找 到了正数序列 b ,且序列 a 中 ...

  5. C++课堂作业2016.05.04

    GitHub/object-oriented 作业题目 开课后的第一次作业,简单地写了一个类,用成员函数来实现计算圆的面积. [代码] main.cpp #include "Area.h&q ...

  6. 深入理解Java之数据类型

    一.概述 我们通过编程解决一个具体问题时,首先要做的工作是用各种“数据结构”表示问题中的实体对象,而后才能着手研究描述具体业务逻辑的算法.这也正印证了”程序 = 数据结构 + 算法“.而这里的数据结构 ...

  7. animate.css与wow.js制作网站动效

    animate.css 官网:https://daneden.github.io/animate.css/ 包括:attention seekers:关注者 bouncing entrances:跳跃 ...

  8. TCP/IP协议与OSI协议

    OSI协议是一个理想化的协议,它把网络传输过程分为七层模型,以达到形象化的理解的效果,在实际应用中没有被使用.TCP/IP协议可以看作是它的简化版,是目前应用最广泛的网络协议,许多协议都是以它为基础而 ...

  9. Linux中实现在系统启动时自动加载模块

    下面是以前学习Linux时写的,后来仔细研究rc.sysinit后发现,只需要修改下列地方就可以了,不必这么麻烦的: rc.sysinit中有这样的一段代码: # Load other user-de ...

  10. 【Python】Python SQLAlchemy基本操作和常用技巧

    首先说下,由于最新的 0.8 版还是开发版本,因此我使用的是 0.79 版,API 也许会有些不同.因为我是搭配 MySQL InnoDB 使用,所以使用其他数据库的也不能完全照搬本文. 接着就从安装 ...