HDOJ 题目3308 LCIS(线段树,区间查询,区间合并)
LCIS
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5319 Accepted Submission(s): 2361
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].
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).
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
1
1
4
2
3
1
2
5
pid=1542" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1542
1828 2871 1255#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b? a:b)
#define min(a,b) (a>b? b:a)
struct s
{
int lnum,rnum;
int lmax,rmax,mmax;
}node[1000100<<2];
void init(int tr,int num)
{
node[tr].lnum=num;
node[tr].rnum=num;
node[tr].lmax=1;
node[tr].rmax=1;
node[tr].mmax=1;
}
void pushup(int tr,int m)
{
node[tr].lnum=node[tr<<1].lnum;
node[tr].rnum=node[tr<<1|1].rnum;
node[tr].lmax=node[tr<<1].lmax;
node[tr].rmax=node[tr<<1|1].rmax;
node[tr].mmax=max(node[tr<<1].mmax,node[tr<<1|1].mmax);
if(node[tr<<1].rnum<node[tr<<1|1].lnum)
{
node[tr].mmax=max(node[tr].mmax,node[tr<<1].rmax+node[tr<<1|1].lmax);
if(node[tr<<1].lmax==m-(m>>1))
node[tr].lmax+=node[tr<<1|1].lmax;
if(node[tr<<1|1].rmax==(m>>1))
node[tr].rmax+=node[tr<<1].rmax;
}
}
void build(int l,int r,int tr)
{
if(l==r)
{
int num;
scanf("%d",&num);
init(tr,num);
return;
}
int mid=(l+r)>>1;
build(l,mid,tr<<1);
build(mid+1,r,tr<<1|1);
pushup(tr,r-l+1);
}
void update(int pos,int num,int l,int r,int tr)
{
if(l==r)
{
init(tr,num);
return;
}
int mid=(l+r)>>1;
if(pos<=mid)
{
update(pos,num,l,mid,tr<<1);
}
else
update(pos,num,mid+1,r,tr<<1|1);
pushup(tr,r-l+1);
}
int query(int L,int R,int l,int r,int tr)
{
if(L<=l&&R>=r)
{
return node[tr].mmax;
}
int mid=(l+r)>>1;
int temp1=0,temp2=0,temp3=0;
if(L<=mid)
temp1=query(L,R,l,mid,tr<<1);
if(R>mid)
temp2=query(L,R,mid+1,r,tr<<1|1);
if(L<=mid&&R>mid&&node[tr<<1].rnum<node[tr<<1|1].lnum)
temp3=min(mid-L+1,node[tr<<1].rmax)+min(R-mid,node[tr<<1|1].lmax);
int ans=max(temp1,max(temp2,temp3));
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
build(1,n,1);
while(m--)
{
char s[2];
int a,b;
scanf("%s%d%d",s,&a,&b);
if(s[0]=='U')
{
update(a+1,b,1,n,1);
}
else
{
printf("%d\n",query(a+1,b+1,1,n,1));
}
}
}
}
HDOJ 题目3308 LCIS(线段树,区间查询,区间合并)的更多相关文章
- 线段树的区间合并 B - LCIS
B - LCIS HDU - 3308 这个是一个很简单很明显的线段树的区间合并,不过区间合并的题目都还是有点难写,建议存个板子. #include <cstdio> #include & ...
- 线段树:CDOJ1592-An easy problem B (线段树的区间合并)
An easy problem B Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't mak ...
- CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)
题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数 做法一:对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线 ...
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- HDU 3308 LCIS 线段树区间更新
最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛 ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...
- HDU 3308 LCIS(线段树单点更新区间合并)
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
- HDU 3308 LCIS (线段树·单点更新·区间合并)
题意 给你一个数组 有更新值和查询两种操作 对于每次查询 输出相应区间的最长连续递增子序列的长度 基础的线段树区间合并 线段树维护三个值 相应区间的LCIS长度(lcis) 相应区间以左 ...
- hud 3308 LCIS 线段树 区间合并
题意: Q a b 查询[a, b]区间的最长连续递增子序列的长度 U a b 将下表为a的元素更新为b 区间合并一般都有3个数组:区间最值,左区间最值和右区间最值 具体详见代码 #include & ...
- POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并
看到讲课安排上 线段树有一节课"区间合并" 我是迷茫的 因为并没有见过 然后了解了一下题目 发现以前写过 还是很麻烦的树链剖分 大概是 解决带修改的区间查询"连续问题&q ...
随机推荐
- F - Modular Exponentiation
Problem description The following problem is well-known: given integers n and m, calculate 2n mod m, ...
- MySQL学习笔记之内连接
不多说,直接上干货! MySQL的内连接 #内连接,两个表按照条件匹配 select class1.stuid,class1.stuname,class1.sex,course from class ...
- lsit集合去重复 顶级表达式
updateList = updateList.Where((x, i) => updateList.FindIndex(z => z.ID == x.ID) == i).ToList() ...
- Dotnet Core2.1 使用CodeFirst
一.添加Mysql引用 二.添加连接字符串 三.配置startup.cs 三.初始化数据库 Add-Migration init Update-Database 四.数据迁移 user实体添加了pas ...
- Java基础13一异常
1.异常概念 异常是程序在运行期发生的不正常的事件,它会打断指令的正常执行流程. 设计良好的程序应该在异常发生时提供处理这些不正常事件的方法,使程序不会因为异常的发生而阻断或产生不可预见的结果. Ja ...
- System.DateTime.Now 24小时制。
this.Label6.Text = "当前时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ...
- ASP.NE 上传文件控件
protected void Button1_Click(object sender, EventArgs e) { //if (Request["id"]==null & ...
- Mysql 之实现多字段模糊查询
在一个table中有省,市,县,期,栋,单元,室几个字段,然后用户输入一个地址从表中的字段拼接起来进行模糊查询. 解决办法: <MySQL权威指南>中CONCAT的使用方法,在书中的对CO ...
- vue.js的ajax和jsonp请求
首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...
- 团体程序设计天梯赛-练习集-L1-036. A乘以B
L1-036. A乘以B 看我没骗你吧 —— 这是一道你可以在10秒内完成的题:给定两个绝对值不超过100的整数A和B,输出A乘以B的值. 输入格式: 输入在第一行给出两个整数A和B(-100 < ...