hdu 3308
终于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的更多相关文章
- hdu 3308 最长连续上升区间
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并
看到讲课安排上 线段树有一节课"区间合并" 我是迷茫的 因为并没有见过 然后了解了一下题目 发现以前写过 还是很麻烦的树链剖分 大概是 解决带修改的区间查询"连续问题&q ...
- 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 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- hdu 3308 LCIS(线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3308 LCIS Time Limit: 6000/2000 MS (Java/Others) ...
- HDU 3308 (线段树区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3308 题意: 两个操作 : 1 修改 单点 a 处的值. 2 求出 区间[a,b]内的最长上升子序列. 做法 ...
- 线段树 HDU 3308
t 题目大意:给你n个数,m个操作.操作有两种:1.U x y 将数组第x位变为y 2. Q x y 问数组第x位到第y位连续最长子序列的长度.对于每次询问,输出一个答案 #include< ...
- HDU 3308 LCIS(线段树)
题目链接 模板题吧,忘了好多,终于A了... #include <cstring> #include <cstdio> #include <string> #inc ...
- HDU 3308 LCIS
题意: U A B: 把第A个数变成BQ A B: 输出[A,B]最长连续上升子序列(注意是连续 相当于子串) 思路:单点更新 ,区间合并几下左边开头最小 和右边结束最大的两个数即可. #incl ...
- HDU 3308 LCIS(线段树)
Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (in ...
随机推荐
- C++中new和malloc
1.malloc的工作原理: malloc使用一个数据结构(链表)来维护分配空间链表的构成:分配的空间/上一个空间的数据/下一个空间/空间大小等信息. 对malloc分配的空间不要越界访问,因为 ...
- 关于OC头文件互相引用的问题
在OC中头文件互相引用是很常见的一件事,如: A的头文件#import "B.h" 而B的头文件#import "A.h" 这个时候就会出现意想不到的问题.系统 ...
- 【原】hive 操作笔记
1.建表: hive> CREATE TABLE pokes (foo INT, bar STRING); hive> CREATE TABLE invites (foo INT, bar ...
- CHS与LBA之间转换程序
原理及介绍来自维基百科 http://zh.wikipedia.org/wiki/%E9%82%8F%E8%BC%AF%E5%8D%80%E5%A1%8A%E4%BD%8D%E5%9D%80 个人用的 ...
- Google翻译
本博文的主要内容有 .Google翻译的介绍 .Google翻译之一:内容板块的翻译 .Google翻译之二:网页的翻译 .Google翻译之四:文档的翻译 .Google翻译之四 ...
- [转]jquery中使用event.target的几点
转自: http://www.cnblogs.com/12go/archive/2011/12/28/2304502.html 1.this和event.target的区别: js中事件是会冒泡的,所 ...
- 【Android - 框架】之刷新加载框架Ultra-Pull-To-Refresh的使用
Ultra-Pull-To-Refresh框架是用来嵌套其他布局,实现下拉刷新和上拉加载的框架.它其中可以嵌套任何控件,ListView.GridView.ScrollView.RecyclerVie ...
- asp.net 动态添加多附件上传.
最近有人问起动态多文件上传,想要做到类似于邮箱添加附件的效果,这个功能其实比较简单,就是往form中添加file元素.在用户选择完文件后,再添加一个file控件,由于file控件过多,视觉上不好看,所 ...
- android 49 广播接收者中启动其他组件
main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...
- android 06 LinearLayout
xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:la ...