hdu 3308 最长连续上升区间
LCIS
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6066 Accepted Submission(s): 2634
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).
Sample Input
1
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
Sample Output
1
1
4
2
3
1
2
5
/*
hdu 3308 最长连续上升区间 给你n个数,然后是两个操作:
1.U A B 将第A个数替换成B
2.Q A B 查询[A,B]间的最长连续上升序列长度 看见题就感觉像前面写过的hdu1540最长连续序列,只是它那个序列是固定的
连续递增,只需要维护一下即可
所以在本题中我新增了lval,rval记录区间最左边和最右边的值,然后通过判断
这两个值来进行区间合并。同时用ls,rs,ms分别记录 从区间左端开始,区间
右端开始,整个区间 的最长连续上升序列 然后通过线段树来维护ls,rs,ms的值,然后查询进行一下判断即可 hhh-2016-03-31 19:50:28
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
const int maxn = 200050; struct node
{
int l,r;
int ls,rs,ms;
int lval,rval;
int mid()
{
return (l+r)>>1;
}
int len()
{
return (r-l+1);
}
} tree[maxn<<2]; void push_up(int i)
{
tree[i].ls = tree[lson].ls,tree[i].lval=tree[lson].lval;
tree[i].rs = tree[rson].rs,tree[i].rval=tree[rson].rval;
//如果可以合并(ls,rs可能超过区间的一般)
if(tree[i].ls == tree[lson].len() && tree[lson].rval < tree[rson].lval)
tree[i].ls += tree[rson].ls;
if(tree[i].rs == tree[rson].len() && tree[lson].rval < tree[rson].lval)
tree[i].rs += tree[lson].rs;
tree[i].ms = max(tree[lson].ms,tree[rson].ms);
if(tree[lson].rval < tree[rson].lval)
tree[i].ms = max(tree[i].ms,tree[lson].rs+tree[rson].ls);
//可能跨过了mid界限
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].ls=tree[i].rs=tree[i].ms=0;
tree[i].lval=tree[i].rval=0;
if(l == r)
{
scanf("%d",&tree[i].lval);
tree[i].rval = tree[i].lval;
tree[i].ls=tree[i].rs=tree[i].ms=1;
return ;
}
int mid = tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
push_up(i);
} void push_down(int i)
{ } void update(int i,int k,int va)
{
if(tree[i].l == k && tree[i].r == k)
{
tree[i].lval = va;
tree[i].rval = va;
return;
}
push_down(i);
int mid = tree[i].mid();
if(k <= mid)
update(lson,k,va);
else
update(rson,k,va);
push_up(i);
} int query(int i,int l,int r)
{
if(tree[i].l == l && tree[i].r == r)
{
return tree[i].ms;
}
int mid = tree[i].mid();
if(r <= mid)
return query(lson,l,r);
else if(l > mid)
return query(rson,l,r);
else
{
int ans1 = query(lson,l,mid);
int ans2 = query(rson,mid+1,r);
if(tree[lson].rval < tree[rson].lval) //如果可以合并(ls,rs有可能超出查询区间)
return max(max(ans1,ans2),min(tree[lson].rs,mid-l+1)+min(tree[rson].ls,r-mid));
else
return max(ans1,ans2);
}
}
char op[10];
int x,y;
int T,n,m;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
build(1,0,n-1); while(m--)
{
scanf("%s",op);
scanf("%d%d",&x,&y);
if(op[0] == 'Q')
{
printf("%d\n",query(1,x,y));
}
else
{
update(1,x,y);
}
}
}
return 0;
}
hdu 3308 最长连续上升区间的更多相关文章
- hdu 3308 LCIS(线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3308 LCIS Time Limit: 6000/2000 MS (Java/Others) ...
- (简单) HDU 3308 LCIS,线段树+区间合并。
Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...
- HDU 3308 线段树单点更新+区间查找最长连续子序列
LCIS Time Limit: 6000/2000 MS (Java/Oth ...
- HDU 2144 (最长连续公共子列 + 并查集) Evolution
我发现我一直理解错题意了,这里的子序列指的是连续子序列,怪不得我写的LCS一直WA 顺便复习一下并查集 //#define LOCAL #include <iostream> #inclu ...
- HDU 3308 线段树求区间最长连续上升子序列长度
题意:两种操作,Q L R查询L - R 的最长连续上升子序列长度,U pos val 单点修改值 #include <bits/stdc++.h> #define N 100005 us ...
- HDU 3308 线段树 最长连续上升子序列 单点更新 区间查询
题意: T个测试数据 n个数 q个查询 n个数 ( 下标从0开始) Q u v 查询 [u, v ] 区间最长连续上升子序列 U u v 把u位置改成v #include<iostream> ...
- 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 线段树区间更新
最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛 ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...
随机推荐
- 201621123044《JAVA程序设计》第一周学习总结
1. 本周学习总结 1.了解了JAVA的诞生以及发展历史简介.JAVA语言的特点,以及JAVA的电脑安装以及环境配置. 2.JAVA不仅可以用eclipse进行编写,也可以在记事本和notepad++ ...
- xcode7,ios9 部分兼容设置
神奇的苹果公司,再一次让程序员中枪. 一.xcode7 新建的项目,Foundation下默认所有http请求都被改为https请求. HTTP+SSL/TLS+TCP = HTTPS 也就是说,服务 ...
- ajax的四种type类型
1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影响资源的内容,即该请求不会产生副作用.无论进行多少次操 ...
- python使用tesseract-ocr完成验证码识别(安装部分)
一.tesseract-ocr安装 Ubuntu版本: 1.tesseract-ocr安装 sudo apt-get install tesseract-ocr 2.pytesseract安装 sud ...
- ViurtualBox配置虚拟机Linux的网络环境
之前可以使用VMware配置成功,让虚拟机和本地通信,虚拟机可以访问外网,但是VMware体积太大了,最后终于把virtualBox也配置成功,也使得两者兼备 环境:本地windows7 64位专业版 ...
- 丢掉DDL,我用这招3分钟清空 MySQL 9亿记录数据表
摘要:最近由于福建开机广告生产环境的广告日志备份表主键(int类型)达到上限(21亿多),不能再写入数据,需要重新清空下该表并将主键重置,但由于表里有8亿多记录的数据量,使用重置命令及DDL命令执行地 ...
- Django ORM那些相关操作
一般操作 https://docs.djangoproject.com/en/1.11/ref/models/querysets/ 官网文档 常用的操作 <1> all() ...
- CentOS7 安装eclipse
1. 首先将eclipse的压缩包文件解压到/opt目录下,要使用root权限.执行如下解压命令:tar -zxvf eclipse-jee-oxygen-1a-linux-gtk-x86_64.ta ...
- Python入门之Python在Win10环境下的配置(图文教程)
请在Python官网下载Python2.7和Python3.6安装包,虽然最新的是3.6版本,但是建议两个包都安装,方便后期在IDE工具切换. Python官网:https://www.python. ...
- python API验证
API验证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 API验证: a. 发令牌: 静态 PS: 隐患 key ...