昨天热身赛的简单版:LCIS。昨天那题用树链剖分,不知道哪里写错了,所以水了水这题看看合并、更新方式是否正确,发现没错啊。看来应该是在树链剖分求lca时写错了。。。

题目:给出n个数,有两种操作:

  1.单点修改

  2.区间询问:最长连续上升子序列

分析:只需要维护五个域就行:lix,rdx,mix,lval,rval。记录当前区间 最左的值lval ,  最右的值rval,必须从左起连续上升序列的长度lix,必须右起下降的长度rdx。合并时注意下即可。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; char IN;
bool NEG;
inline void Int(int &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
} inline void Char(char &x){
while(!isupper(x=getchar()));
} /******** program ********************/ const int MAXN = 200105; int son[MAXN],sz[MAXN],top[MAXN],fa[MAXN],tid[MAXN],dep[MAXN],tim;
bool use[MAXN];
int a[MAXN]; struct segTree{
int l,r;
int lval,rval;
int lix,mix;
int rdx;
segTree(){
l = r = lval = rval = 0;
lix = mix = 0;
rdx = 0;
}
inline int mid(){
return (l+r)>>1;
}
inline int dis(){
return r-l+1;
}
}tree[MAXN<<1]; inline void update(segTree &now,segTree l,segTree r){
// lix
if(l.lix==l.dis()&&l.rval<r.lval)
now.lix = l.lix+r.lix;
else now.lix = l.lix; // mix
if(l.rval<r.lval)
now.mix = max(max(l.mix,r.mix),l.rdx+r.lix);
else now.mix = max(l.mix,r.mix); // rdx
if(r.dis()==r.rdx&&r.lval>l.rval)
now.rdx = r.rdx+l.rdx;
else now.rdx = r.rdx; now.lval = l.lval;
now.rval = r.rval;
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
if(l==r){
tree[rt].lval = tree[rt].rval = a[l];
tree[rt].lix = tree[rt].mix = 1;
tree[rt].rdx = 1;
return;
}
int mid = tree[rt].mid();
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
update(tree[rt],tree[rt<<1],tree[rt<<1|1]);
} void modify(int pos,int val,int rt){
if(tree[rt].l==tree[rt].r){
tree[rt].lval = tree[rt].rval = val;
return;
}
int mid = tree[rt].mid();
if(pos<=mid)modify(pos,val,rt<<1);
else modify(pos,val,rt<<1|1);
update(tree[rt],tree[rt<<1],tree[rt<<1|1]);
} segTree ask(int l,int r,int rt){
if(l<=tree[rt].l&&tree[rt].r<=r)
return tree[rt];
int mid = tree[rt].mid();
segTree ans;
if(r<=mid)ans = ask(l,r,rt<<1);
else if(l>mid)ans = ask(l,r,rt<<1|1);
else{
segTree a = ask(l,r,rt<<1);
segTree b = ask(l,r,rt<<1|1);
update(ans,a,b);
}
update(tree[rt],tree[rt<<1],tree[rt<<1|1]);
return ans;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif char op;
int n,m,ncase,x,y;
Int(ncase);
while(ncase--){
Int(n);Int(m);
for(int i=1;i<=n;i++)
Int(a[i]);
build(1,n,1);
while(m--){
Char(op);
Int(x);Int(y);
if(op=='Q')printf("%d\n",ask(++x,++y,1).mix);
else modify(++x,y,1);
}
} return 0;
}

  

hdu 3308 LCIS 线段树的更多相关文章

  1. HDU 3308 LCIS (线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

  2. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  3. HDU 3308 LCIS (线段树&#183;单点更新&#183;区间合并)

    题意  给你一个数组  有更新值和查询两种操作  对于每次查询  输出相应区间的最长连续递增子序列的长度 基础的线段树区间合并  线段树维护三个值  相应区间的LCIS长度(lcis)  相应区间以左 ...

  4. HDU 3308 LCIS 线段树区间更新

    最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛  ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...

  5. HDU 3308 LCIS(线段树)

    题目链接 模板题吧,忘了好多,终于A了... #include <cstring> #include <cstdio> #include <string> #inc ...

  6. LCIS HDU - 3308 (线段树区间合并)

    LCIS HDU - 3308 Given n integers. You have two operations: U A B: replace the Ath number by B. (inde ...

  7. HDU 3308 (线段树区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=3308 题意: 两个操作  : 1 修改 单点  a 处的值. 2 求出 区间[a,b]内的最长上升子序列. 做法 ...

  8. hud 3308 LCIS 线段树 区间合并

    题意: Q a b 查询[a, b]区间的最长连续递增子序列的长度 U a b 将下表为a的元素更新为b 区间合并一般都有3个数组:区间最值,左区间最值和右区间最值 具体详见代码 #include & ...

  9. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

随机推荐

  1. 谈Android四大组件之Service篇

    Service简介 Service是Android系统中的四大组件之一,它是一种长生命周期的,没有可视化界面,运行于后台的一种服务程序.Service必须在AndroidManifest.xml中声明 ...

  2. asp获取勾选checkbox的值

    Dim str_select  str_select = CStr(request.Form("c_name")) c_name是checkbox的name

  3. wchar_t 和 char 之间转换

    vc++2005以后,Visual studio 编译器默认的字符集为Unicode.VC中很多字符处理默认为宽字符wchar_t,如CString的getBuffer(),而一些具体操作函数的输入却 ...

  4. VC++ 6.0中实现三叉切分窗口与多视图 [转]

    一.引用 当用户需要同时对文当的不同部分进行编辑时,常常会用到切分窗口;这些窗口可以都是相同的视,或者一个窗口为列表视,而另一个为树型视图.应用程序框架有多种方式来表示多视图,切分窗口是其中的方式之一 ...

  5. eclipse设置默认编码格式为UTF-8

    需要设置的几处地方为: Window->Preferences->General ->Content Type->Text->JSP 最下面设置为UTF-8 Window ...

  6. 基于KVM的虚拟化研究及应用

    引言 虚拟化技术是IBM在20世纪70年代首先应用在IBM/370大型机上,这项技术极大地提高了大型机资源利用率.随着软硬件技术的迅速发展,这项属于大型机及专利的技术开始在普通X86计算机上应用并成为 ...

  7. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

  8. 从来没有天才 靠自己创造未来——Leo鉴书(29)

    之前在网上跟朋友们聊起天才这个话题,我认来从来没什么所谓天才,有朋友认为有的,只是我们定义不同,要不你看看苏轼? 持天才论者持两个观点:有些人天生擅长干某些事儿,也许是基因作怪:有些人的能力是上帝或者 ...

  9. iOS开发——UI高级OC篇&自定义控件之调整按钮中子控件(图片和文字)的位置

    自定义控件之调整按钮中子控件(图片和文字)的位置 其实还有一种是在storyBoard中实现的,只需要设置对应空间的左右间距: 这里实现前面两种自定义的方式 一:imageRectForContent ...

  10. OpenRisc-67-OR的汇编

    引言 之前我们写过OR的裸机程序,写过基于OR的linux设备驱动程序,也反汇编过OR的机器码. 本小节,我们将通过一个简单的实验,对OR的汇编(指令集)做一个简单的梳理和測试. 1,基本思想 要想了 ...