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 ...
随机推荐
- Spring4+quartz2集群借助邮箱或是短信实现生日的农历提醒(Quartz实现农历、阴历、公历生日提醒)
自己记性差,除了老婆.老大和自己的生日以外,一直记不住亲朋好友的生日,长辈们的生日基本上又都是用农历来算,公历的话,直接用Quartz设置循环提醒,农历就没辙了,每每搞的自己很尴尬,需要别人来提醒自己 ...
- Java生成8位随机邀请码,不重复
public static String[] chars = new String[] { "a", "b", "c", "d&q ...
- Mysql 和 Postgresql(PGSQL) 对比
Mysql 和 Postgresql(PGSQL) 对比 转载自:http://www.oschina.net/question/96003_13994 PostgreSQL与MySQL比较 MySQ ...
- [转]PHP利用Gearman来处理并行多进程问题
From : http://www.yuansir-web.com/2013/11/25/php%E5%88%A9%E7%94%A8gearman%E6%9D%A5%E5%A4%84%E7%90%86 ...
- IIS7增加mine类型,以便可以访问apk
1.打开IIS 2.找到mine类型,单击右边的添加 3.输入apk的配置 application/vnd.android-package-archive .这样,用户就可以直接访问apk了
- java.io包的总体框架图(转)
原文链接:java.io包的总体框架图, 便于记忆!
- 【电信我想问一下,网页上多出的隐藏广告】究竟谁在耍流氓,还要不要脸了??? 0817tt 植入广告
最近总是有网页 莫名的有声音,是网页游戏的,一刷新就没了. 这次 我怒了! 我觉得不可能是这个网站的chinaunix的广告.左边是 有广告的,右侧标签 是无广告的. 有广告的 实际上 隐藏了一个页面 ...
- pchar,pwidechar,pansichar作为返回参数时内存访问错误
function Test:pachr: var str: string; begin str := 'Test Char'; result:=pchar(str); end; 上面的Te ...
- (使用STL中的数据结构进行编程7.3.15)UVA 630 Anagrams (II)(求一个单词在字典中出现的次数)
/* * UVA_630.cpp * * Created on: 2013年11月4日 * Author: Administrator */ #include <iostream> #in ...
- 大型Web 网站 Asp.net Session过期你怎么办
在 WEB 系统中, 我们一般会用session来保存一些简单但是却很重要的信息.比如Asp.net中经常会用Session来保存用户登录信息,比如UserID.为了解决 WEB场大家采用了把sess ...