HDU 3308 LCIS(线段树)

本文介绍了一种使用线段树解决最长连续递增子序列问题的方法,通过对线段树节点的特殊定义,能够高效地处理数组元素更新及查询区间内最长连续递增子序列长度的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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;
}


内容概要:本文档详细介绍了基于MATLAB实现的无人机三维路径规划项目,核心算法采用蒙特卡罗树搜索(MCTS)。项目旨在解决无人机在复杂三维环境中自主路径规划的问题,通过MCTS的随机模拟与渐进式搜索机制,实现高效、智能化的路径规划。项目不仅考虑静态环境建模,还集成了障碍物检测与避障机制,确保无人机飞行的安全性和效率。文档涵盖了从环境准备、数据处理、算法设计与实现、模型训练与预测、性能评估到GUI界面设计的完整流程,并提供了详细的代码示例。此外,项目采用模块化设计,支持多无人机协同路径规划、动态环境实时路径重规划等未来改进方向。 适合人群:具备一定编程基础,特别是熟悉MATLAB和无人机技术的研发人员;从事无人机路径规划、智能导航系统开发的工程师;对MCTS算法感兴趣的算法研究人员。 使用场景及目标:①理解MCTS算法在三维路径规划中的应用;②掌握基于MATLAB的无人机路径规划项目开发全流程;③学习如何通过MCTS算法优化无人机在复杂环境中的飞行路径,提高飞行安全性和效率;④为后续多无人机协同规划、动态环境实时调整等高级应用打下基础。 其他说明:项目不仅提供了详细的理论解释和技术实现,还特别关注了实际应用中的挑战和解决方案。例如,通过多阶段优化与迭代增强机制提升路径质量,结合环境建模与障碍物感知保障路径安全,利用GPU加速推理提升计算效率等。此外,项目还强调了代码模块化与调试便利性,便于后续功能扩展和性能优化。项目未来改进方向包括引入深度强化学习辅助路径规划、扩展至多无人机协同路径规划、增强动态环境实时路径重规划能力等,展示了广阔的应用前景和发展潜力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值