昨天热身赛的简单版: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. leetcode:Path Sum (路径之和) 【面试算法题】

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. C++100款开源界面库[转]

    (声明:Alberl以后说到开源库,一般都是指著名的.或者不著名但维护至少3年以上的.那些把代码一扔就没下文的,Alberl不称之为开源库,只称为开源代码.这里并不是贬低,像Alberl前面那个系列的 ...

  3. php资料站

    Veda 原型:http://www.nowamagic.net/librarys/veda/cate/PHP

  4. Java Zip压缩实现

    最近在自学javaWeb,先复习一下java,把还给老师的东西再找回来(知识如果不用很快就会忘记啊).. 今天看到了zip压缩,决定要整理一下. java将有关zip压缩的内容都封装在java.uti ...

  5. 怎样对ListView的项进行排序

    当您使用资源浏览器查看文件时,您能够随心所欲的按名称.大小.类型及改动日期不同的列对文件进行大小排序..Net提供的ListView组件没有直接提供这样的功能,但要实现并不难.   ListView. ...

  6. ProcessBuilder和Runtime远程执行

    http://desert3.iteye.com/blog/1596020 ProcessBuilder.start() 和 Runtime.exec() 方法都被用来创建一个操作系统进程(执行命令行 ...

  7. 什么是双线双IP,什么叫双线双IP

    双线双IP实现双线路,拥有中国电信.中国网通骨干网的接入,在该机房托管的服务器,实现了电信和网通的双线路接入,使电信和网通的用户都能以非常快的速度连接到服务器,解决了电信和网通互相访问速度慢的问题.这 ...

  8. hihocoder #1178 : 计数 暴力

    #1178 : 计数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/1178 ...

  9. 【JavaScript】停不下来的前端,自动化流程

    http://kb.cnblogs.com/page/501270/ 流程 关于流程,是从项目启动到发布的过程.在前端通常我们都做些什么? 切图,即从设计稿中获取需要的素材,并不是所有前端开发都被要求 ...

  10. BigDecimal带精度的运算的两篇文章

    转自:http://guoliangqi.iteye.com/blog/670908 之前提到过在商业运算中要使用BigDecimal来进行相关的钱的运算(java中关于浮点运算需要注意的 ),可是实 ...