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 ...
随机推荐
- md5目录下的文件包括子目录
find ./ -type f -print0 | xargs -0 md5sum
- 用开源项目JazzyViewPager实现ViewPager切换动画
JazzyViewPager这个项目可以让viewpager有各种绚丽的动画,而且还可以自由扩展.但从官网下载的lib导入时会出现找不到视图的问题,不知道是不是我人品不行,所以我就自己写了lib.总之 ...
- 无需SherlockActionbar的SlidingMenu使用详解(一)——通过SlidingMenu设置容器并解决滑动卡顿的问题
想必很多人都听过这个开源框架,一年前真的是风靡一时.只是它的配置较为繁琐,还需要sherlockActionbar的支持,我这里下载了最新的开源库,并且在实际用套用了AppCompat的官方库,这样就 ...
- pssh,pdsh,mussh,cssh,dsh运维工具介绍
pssh 1 安装:#wget http://peak.telecommunity.com/dist/ez_setup.pypython ez_setup.py#wget http://paralle ...
- PHP xhprof性能优化
xhprof window http://dev.freshsite.pl/php-extensions/xhprof.html http://php.net/manual/en/book.xhpro ...
- jQuery.data() 的实现方式,jQuery16018518865841457738的由来,jQuery后边一串数字的由来
原文地址: http://xxing22657-yahoo-com-cn.iteye.com/blog/1042440 jQuery.data() 的实现方式 jQuery.data() 的作用是为普 ...
- Spring(AbstractRoutingDataSource)实现动态数据源切换
转自: http://blog.51cto.com/linhongyu/1615895 一.前言 近期一项目A需实现数据同步到另一项目B数据库中,在不改变B项目的情况下,只好选择项目A中切换数据源,直 ...
- Linux修改终端显示前缀及环境变量
Linux终端前面默认显示一长串,如: [work@aaa.baidu.com dir]$ 这是由PS1环境变量决定的: [work@aaa.baidu.com dir]$ echo $PS1 [\u ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- Scramble String leetcode java
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...