终于A了,我好想砍人,虽然这是一道基础的区间合并。但是这错误我也是醉了。

错误我表在注释里。

题目意思不多说,sha崽题目出的很简洁。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=100010;
int lsum[maxn<<2],rsum[maxn<<2],msum[maxn<<2],p[maxn];
struct node {
int x,y;
} cover[maxn<<2];
void pushup(int l,int r,int rt)
{
cover[rt].x=cover[rt<<1].x;
cover[rt].y=cover[rt<<1|1].y;
lsum[rt]=lsum[rt<<1];
rsum[rt]=rsum[rt<<1|1];
msum[rt]=max(msum[rt<<1],msum[rt<<1|1]);
int mid=(l+r)>>1;
if(cover[rt<<1].y<cover[rt<<1|1].x)
{
if(lsum[rt]==mid-l+1)
lsum[rt]+=lsum[rt<<1|1];
if(rsum[rt]==r-mid)
rsum[rt]+=rsum[rt<<1];
msum[rt]=max(rsum[rt<<1]+lsum[rt<<1|1],msum[rt]);
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
msum[rt]=lsum[rt]=rsum[rt]=1;
cover[rt].x=cover[rt].y=p[l];
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(l,r,rt) ;
}
void update(int xx,int yy,int l,int r,int rt)
{
if(l==r)
{
cover[rt].x=cover[rt].y=yy;
return ;
}
int mid=(l+r)>>1;
if(xx<=mid)
update(xx,yy,lson);
else
update(xx,yy,rson);
pushup(l,r,rt);
}
int query(int L,int R,int l,int r,int rt)//错就错在这个查找
{
if(L<=l&&r<=R)
return msum[rt];
int mid=(l+r)>>1;
int ans = 0; //就错在这,ans要初始化0
if(L<=mid)ans = query(L,R,lson);
if(R>mid)ans = max ( ans, query(L,R,rson) );
if(cover[rt<<1].y<cover[rt<<1|1].x)ans=max ( ans , min(mid-L+1, rsum[rt<<1])+min(R-mid,lsum[rt<<1|1]) );
return ans;
}
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)scanf("%d",&p[i]);
build(0,n-1,1);

int a,b;
char op[5];
while(m--)
{
scanf("%s%d%d",op,&a,&b);//printf("%d %d\n",a,b);
if(*op=='Q')printf("%d\n",query(a,b,0,n-1,1));
else update(a,b,0,n-1,1);
//for(int i=1;i<=5;i++)printf("%d %d %d %d %d\n",cover[i].x,cover[i].y,lsum[i],rsum[i],msum[i]);
}
}
}

下面我把为什么不把ans初始化的问题写一下。

比如原序列是1 2 3 那么要查找 【1 ,2】 区间的话。

看图

反正意思就差不多这样吧,但是好像图上这种情况是不返回数的,所以ans是系统随机给的一个数。

hdu 3308的更多相关文章

  1. hdu 3308 最长连续上升区间

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并

    看到讲课安排上 线段树有一节课"区间合并" 我是迷茫的 因为并没有见过 然后了解了一下题目 发现以前写过 还是很麻烦的树链剖分 大概是 解决带修改的区间查询"连续问题&q ...

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

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

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

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

  5. hdu 3308 LCIS(线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3308 LCIS Time Limit: 6000/2000 MS (Java/Others)     ...

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

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

  7. 线段树 HDU 3308

    t 题目大意:给你n个数,m个操作.操作有两种:1.U x y 将数组第x位变为y   2. Q x y 问数组第x位到第y位连续最长子序列的长度.对于每次询问,输出一个答案 #include< ...

  8. HDU 3308 LCIS(线段树)

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

  9. HDU 3308 LCIS

    题意: U A B: 把第A个数变成BQ A B: 输出[A,B]最长连续上升子序列(注意是连续  相当于子串) 思路:单点更新 ,区间合并几下左边开头最小  和右边结束最大的两个数即可. #incl ...

  10. HDU 3308 LCIS(线段树)

    Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (in ...

随机推荐

  1. octopress的一些总结

    1.编辑_config.yml 的description时,不能使用tab键 2.修改主题‘MediumFox’  description 和 文章展示的宽度,修改文件home_landing_row ...

  2. c++异常安全和copy and swap策略

    异常安全有两个目标: 不泄露任何资源.这个通过RAII可以做到. 不破坏数据结构.这是下文要讨论的事情 异常安全有三个级别: 基本安全:异常发生后对象和数据结构还有合法状态.实现简单,应该作为最低要求 ...

  3. hdu 4411 arrest 最小费用流

    #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...

  4. poj 3465 Corn Fields 状态压缩

    题目链接:http://poj.org/problem?id=3254 #include <cstdio> #include <cstring> #include <io ...

  5. Wall - POJ 1113(求凸包)

    题目大意:给N个点,然后要修建一个围墙把所有的点都包裹起来,但是要求围墙距离所有的点的最小距离是L,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离 ...

  6. MongoDB Java 连接配置

    [前言] 由于处于线程安全等考虑,MongoDBJava从3.0开始已经打算废弃DB开头的类的使用,所以整体调用上有了较大的区别,特以此文志之 [正文] 环境配置 在Java程序中如果要使用Mongo ...

  7. orace owi介绍

    第1章 OWI介绍记录和观察进程所经历的等待现象的功能和界面以及方法论,统称为OWI,也就是Oracle Wait Interface.等待事件的P1.P2.P3值可以通过v$session_wait ...

  8. m版页面判断安卓与ios系统

    安卓系统和ios系统,在做app里面嵌入m版时,有时候会发现,ios上面的那个电池状态栏不占位置,但是安卓的状态栏占位,所以需要区分系统样式单独处理一下! var sUserAgent=navigat ...

  9. http://c7sky.com/works/css3slides/#1

    http://c7sky.com/works/css3slides/#1 css3 学习

  10. MFC编程小技巧——强制杀死进程

    在某些应用场合下,我们可能需要在启动A进程启动时关闭进程B.MFC下该如何做呢?以下是我项目中用到的代码: int KillProcess(DWORD Pid) { //打开进程得到进程句柄 HAND ...