hdu 3308 LCIS(线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=3308
LCIS
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).
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
4
2
3
1
2
5
pid=2871">2871
pid=1255">1255
#include <iostream>
#include <cstdio> using namespace std; struct node
{
int l,r;
int mmax;
int lmax,rmax;
int lnum,rnum;
}s[100000*4+10]; void InitTree(int l,int r,int k)
{
s[k].l=l;
s[k].r=r;
s[k].mmax=1;
s[k].rmax=1;
s[k].lmax=1;
s[k].lnum=s[k].rnum=0;
if (l==r)
return ;
int mid=(l+r)/2;
InitTree(l,mid,2*k);
InitTree(mid+1,r,2*k+1);
} void UpdataTree(int i,int change,int k)
{
if (s[k].l==s[k].r&&s[k].l==i)
{
s[k].lnum=s[k].rnum=change;
return ;
}
int mid=(s[k].l+s[k].r)/2;
if (i>mid)
UpdataTree(i,change,2*k+1);
else if (i<=mid)
UpdataTree(i,change,2*k); s[k].lnum=s[k*2].lnum;
s[k].rnum=s[k*2+1].rnum; s[k].lmax=s[k*2].lmax;
s[k].rmax=s[k*2+1].rmax; s[k].mmax=max(s[k*2].mmax,s[k*2+1].mmax);
if(s[k*2].rnum<s[k*2+1].lnum)
{
s[k].mmax=max(s[k].mmax,s[k*2].rmax+s[k*2+1].lmax);
if(s[k*2].lmax==s[k*2].r-s[k*2].l+1)
s[k].lmax=s[k*2].lmax+s[k*2+1].lmax;
if(s[k*2+1].rmax==s[k*2+1].r-s[k*2+1].l+1)
s[k].rmax=s[k*2].rmax+s[k*2+1].rmax;
}
} int SearchTree(int l,int r,int k)
{
if (s[k].l==l&&s[k].r==r)
return s[k].mmax;
else
{
int mid=(s[k].l+s[k].r)/2;
if (l>mid)
return SearchTree(l,r,2*k+1);
else if (r<=mid)
return SearchTree(l,r,2*k);
/*else
{
if (s[k].lnum<s[k].rnum)
return SearchTree(l,mid,2*k)+SearchTree(mid+1,r,2*k+1);
else if
}*/
else
{
int a=min(s[k*2].rmax,mid-l+1);
int b=min(s[k*2+1].lmax,r-mid);
int Max=max(a,b);
Max=max(Max,SearchTree(l,mid,2*k));
Max=max(Max,SearchTree(mid+1,r,2*k+1));
if(s[k*2].rnum<s[2*k+1].lnum)
{
Max=max(Max,a+b);
}
return Max;
} }
} int main()
{
int t;
char ch[70];
scanf("%d",&t);
while (t--)
{
int n,m,w,a,b;
scanf("%d%d",&n,&m);
InitTree(0,n-1,1);
for (int i=0; i<n; i++)
{
scanf("%d",&w);
UpdataTree(i,w,1);
}
for (int i=0; i<m; i++)
{
//getchar();
scanf("%s%d%d",ch,&a,&b);
if (ch[0]=='Q')
{
//cout<<"!!!!!!!!!!!!!!!!!"<<endl;
int ans=SearchTree(a,b,1);
printf ("%d\n",ans); }
else if (ch[0]=='U')
{
UpdataTree(a,b,1);
}
}
}
return 0;
}
hdu 3308 LCIS(线段树区间合并)的更多相关文章
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- LCIS HDU - 3308 (线段树区间合并)
LCIS HDU - 3308 Given n integers. You have two operations: U A B: replace the Ath number by B. (inde ...
- HDU 3308 (线段树区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3308 题意: 两个操作 : 1 修改 单点 a 处的值. 2 求出 区间[a,b]内的最长上升子序列. 做法 ...
- HDU 3308 LCIS 线段树区间更新
最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛 ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...
- hud 3308 LCIS 线段树 区间合并
题意: Q a b 查询[a, b]区间的最长连续递增子序列的长度 U a b 将下表为a的元素更新为b 区间合并一般都有3个数组:区间最值,左区间最值和右区间最值 具体详见代码 #include & ...
- HDU 3308 LCIS (线段树·单点更新·区间合并)
题意 给你一个数组 有更新值和查询两种操作 对于每次查询 输出相应区间的最长连续递增子序列的长度 基础的线段树区间合并 线段树维护三个值 相应区间的LCIS长度(lcis) 相应区间以左 ...
- hdu-3308 LCIS (线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 3308 LCIS(线段树单点更新区间合并)
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
- hdu 1540(线段树区间合并)
题目链接:传送门 参考文章:传送门 题意:n个数字初始连在一条线上,有三种操作, D x表示x号被摧毁: R 表示恢复剩下的通路 Q表示查询标号为x所在的串的最长长度. 思路:线段树的区间合并. #i ...
- hdu 3308 LCIS 线段树
昨天热身赛的简单版:LCIS.昨天那题用树链剖分,不知道哪里写错了,所以水了水这题看看合并.更新方式是否正确,发现没错啊.看来应该是在树链剖分求lca时写错了... 题目:给出n个数,有两种操作: 1 ...
随机推荐
- 进一步优化ListView
之前我已经分享过一篇:viewHodler的通用写法,就是专门用来优化listview的加载的,但是对于复杂的布局,我们还需要在listview滑动和不滑动时进行自己的处理,今天我看到一篇文章就是讲这 ...
- 同一个ImageView根据xml文件来显示不同的图片--level-list
感谢:http://blog.sina.com.cn/s/blog_6111ce890100psq9.html 有时候,我们为了在一个ImageView中显示不同的图片,平时往往会使用: if (条件 ...
- tf.argmax
tf.argmax(input, axis=None, name=None, dimension=None) Returns the index with the largest value acro ...
- FinalizableReference, FinalizablePhantomReference, FinalizableReferenceQueue
FinalizableReference /* * Copyright (C) 2007 The Guava Authors * * Licensed under the Apache License ...
- go语言之进阶篇网络编程
一.网络编程 1.网络分层架构 2.每层协议的功能 3.网络通信条件 网卡,mac地址(不需要用户处理) arp --->通过IP找mac 逻辑地址,ip地址(需要用户指定) ---> ...
- go语言之进阶篇多任务资源竞争问题
1.多任务资源竞争问题 示例: package main import ( "fmt" "time" ) //定义一个打印机,参数为字符串,按每个字符打印 // ...
- 热修复 AndFix 阿里 apkpatch MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- QMUI UI库 控件 弹窗 列表 工具类 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Android -- View流程
在自定义view中打log,view的显示共有三种,visible.invisible和gone,分别看一下log: gone onVisibilityChanged construct 2 para ...
- [转]0.python:scikit-learn基本用法
感谢百小度治哥,该文原地址:here 经Edwin Chen的推荐,认识了scikit-learn这个非常强大的python机器学习工具包.这个帖子作为笔记.(其实都没有笔记的意义,因为他家文档做的太 ...