HDU 3308 LCIS(线段树)

题目:

Description

Given n integers. 
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]. 

Input

T in the first line, indicating the case number. 
Each case starts with two integers n , m(0<n,m<=10  5). 
The next line has n integers(0<=val<=10  5). 
The next m lines each has an operation: 
U A B(0<=A,n , 0<=B=10  5
OR 
Q A B(0<=A<=B< n). 

Output

For each Q, output the answer.

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


题意:

给你n个数,每次有两种操作 

U A B 把第A个数变成B(注意是从0开始)

Q A B 求区间【A,B】最长连续递增区间


思路:线段树,既然我们要求最长的递增序列,那我们就要把每个区间的递增序列的长度保留下来。定义一个max来存。

然后考虑到两个区间组成一个新的区间的时候可能还可以进行合并,那么我们就要把每个区间边缘的数字(lnum,rnum)记录下来,并且区间边缘递增的长度(lmax,rmax)记录下来

当合并的时候新的区间的max 就是 左孩子区间的max,右孩子区间的max,如何左右孩子区间的可以合并(就是左孩子区间的右端和右孩子区间的左端是连续递增的),就还要把他们两端的长度加起来,三者中最大的就是新的区间的max。

每次同理也要更新 (lnum,rnum) lnum就是左孩子区间的lnum,rnum就是右孩子区间的rnum

更新(lmax,rmax ) 如果一个区间不是完全递增的,那么lmax就是左孩子区间的lmax ,rmax就是右孩子区间的rmax, 如果区间完全递增就要加上另外一个区间的lmax或rmax


查询的时候,如果刚好就是线段数的一个区间 就是该区间的max

否则 就是分成的左区间,右区间,以及可能合并的,三者的最大值。


代码(代码巨丑,就不要吐槽了):

#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
#define mido int mid=(tree[index].l+tree[index].r)/2;
struct node {
	int l;
	int r;
	int max;
	int lnum;
	int rnum;
	int lmax;
	int rmax;
};
int max(int a, int b) {
	if (a > b) return a;
	return b;
}

node tree[4 * 100005];
void build(int index, int l, int r) {
	tree[index].l = l;
	tree[index].r = r;
	mido;
	if (l == r) {
		int num;
		scanf("%d", &num);
		tree[index].max = 1;
		tree[index].lnum = num;
		tree[index].rnum = num;
		tree[index].lmax = 1;
		tree[index].rmax = 1;
		return;
	}
	build(index * 2, l, mid);
	build(index * 2 + 1, mid + 1, r);
	if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].max = max(tree[index * 2].max, max(tree[index * 2 + 1].max, tree[index * 2].rmax + tree[index * 2 + 1].lmax));
	}
	else {
		tree[index].max = max(tree[index * 2].max, tree[index * 2 + 1].max);
	}
	tree[index].lnum = tree[index * 2].lnum;
	tree[index].rnum = tree[index * 2 + 1].rnum;
	if (tree[index * 2].lmax == tree[index * 2].r - tree[index * 2].l + 1 && tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].lmax = tree[index * 2].lmax + tree[index * 2 + 1].lmax;
	}
	else tree[index].lmax = tree[index * 2].lmax;
	if (tree[index * 2 + 1].rmax == tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1 && tree[index * 2 + 1].lnum>tree[index * 2].rnum) {
		tree[index].rmax = tree[index * 2 + 1].rmax + tree[index * 2].rmax;
	}
	else tree[index].rmax = tree[index * 2 + 1].rmax;
}
void update(int index,int l, int r, int q) {
	if (tree[index].l == l&&tree[index].r == r) {
		tree[index].lnum = q;
		tree[index].rnum = q;
		return;
	}
	mido;
	if (r <= mid) {
		update(index * 2, l, r, q);
	}
	else if (l>mid) {
		update(index * 2 + 1, l, r, q);
	}
	else {
		update(index * 2, l, mid, q);
		update(index * 2 + 1, mid + 1, r, q);
	}
	if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].max = max(tree[index * 2].max, max(tree[index * 2 + 1].max, tree[index * 2].rmax + tree[index * 2 + 1].lmax));
	}
	else {
		tree[index].max = max(tree[index * 2].max, tree[index * 2 + 1].max);
	}
	tree[index].lnum = tree[index * 2].lnum;
	tree[index].rnum = tree[index * 2 + 1].rnum;
	if (tree[index * 2].lmax == tree[index * 2].r - tree[index * 2].l + 1 && tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].lmax = tree[index * 2].lmax + tree[index * 2 + 1].lmax;
	}
	else tree[index].lmax = tree[index * 2].lmax;
	if (tree[index * 2 + 1].rmax == tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1 && tree[index * 2 + 1].lnum>tree[index * 2].rnum) {
		tree[index].rmax = tree[index * 2 + 1].rmax + tree[index * 2].rmax;
	}
	else tree[index].rmax = tree[index * 2 + 1].rmax;
}
int query(int index, int a, int b) {
	if (tree[index].l == a&&tree[index].r == b) {
		return tree[index].max;
	}
	mido;
	if (b <= mid) {
		query(index * 2, a, b);
	}
	else if (a>mid) {
		query(index * 2 + 1, a, b);
	}
	else {
		int t1,t2,t3;
		if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
			if (tree[index * 2].r - a + 1 >= tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 >= tree[index * 2 + 1].lmax) {
				t1 = tree[index * 2].rmax + tree[index * 2 + 1].lmax;
			}
			else if (tree[index * 2].r - a + 1 < tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 >= tree[index * 2 + 1].lmax) {
				t1= tree[index * 2].r - a + 1+ tree[index * 2 + 1].lmax;
			}
			else if (tree[index * 2].r - a + 1 >= tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 < tree[index * 2 + 1].lmax) {
				t1 = tree[index * 2].rmax + b - tree[index * 2 + 1].l + 1;
			}
			else {
				t1= tree[index * 2].r - a + 1 + b - tree[index * 2 + 1].l + 1;
			}
			t2 = query(index*2, a, mid);
			t3 = query(index * 2 + 1, mid + 1, b);
			return max(t1, max(t2, t3));
		}
		else {
			int t1, t2;
			t1= query(index * 2, a, mid);
			t2 = query(index * 2 + 1, mid + 1, b);
			return max(t1, t2);
		}
	}
}
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n,m;
		scanf("%d%d", &n, &m);
		build(1, 1, n);
		while (m--) {
			char oper[2];
			int a, b;
			scanf("%s%d%d", oper, &a, &b);
			if (strcmp(oper, "U") == 0) {
				update(1, a+1, a+1, b);
			}
			else {
				printf("%d\n", query(1, a + 1, b + 1));
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值