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. PHP计算两个已知经纬度之间的距离

    /** *求两个已知经纬度之间的距离,单位为千米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离,单位千米 **/ privat ...

  2. Python基础 之 set集合 与 字符串格式化

    数据类型的回顾与总结 可变与不可变1.可变:列表,字典2.不可变:字符串,数字,元组 访问顺序:1.直接访问:数字2.顺序访问:字符串,列表,元祖3.映射:字典 存放元素个数:容器类型:列表,元祖,字 ...

  3. 上楼梯问题(递归C++)

    [问题描述] 小明上楼梯,一次可以迈1步,2步和3步,假设楼梯共有n个台阶,输出他所有的走法. [代码展示] #include<iostream>using namespace std;i ...

  4. LeetCode - 459. Repeated Substring Pattern - O(n)和O(n^2)两种思路 - KMP - (C++) - 解题报告

    题目 题目链接 Given a non-empty string check if it can be constructed by taking a substring of it and appe ...

  5. Python面向对象-访问限制

    在Class内部,可以有字段,方法和属性,而外部代码可以通过直接调用实例变量的方法来操作数据, (1)私有普通字段 比如对于下面的Student类,name字段可以在外面通过对象进行直接访问: cla ...

  6. Java实验二实验报告:java面向对象程序设计

    java实验二实验报告 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计模式 实验 ...

  7. object-oriented 第二次作业(2)

    面向对象程序设计自学计划 由于我的英文实在是很差,所以我就没有去考虑看英文的课程视频.网络上的课程有很多,什么学校的也有,一开始我不知道该如何开始选择课程.感觉每个都还可以.后来在群里的看到别人推荐的 ...

  8. [基于NetCore的简单博客系统]-登录

    0-项目背景 一个基于.NET CORE RAZOR PAGES的简单博客系统 技术栈全部采用微软官方实现方式,目的是熟悉新技术 项目地址:https://github.com/ganqiyin/BL ...

  9. 原生javascript自定义input[type=radio]效果

    2018年6月27日 更新 找到最为简单的仅仅使用css3的方案 <!DOCTYPE html> <html lang="en"> <head> ...

  10. 【ASP.NET Core】- 搭建MVC框架

    1.使用最新版本的VS2017,并安装.NET Core2.0中相关开发工具   2.打开VS2017,点击文件-新建-项目,选择.NET Core中的ASP.NET Core Web 应用程序   ...