hdu6315 Naive Operations(线段树)

本文介绍了一种使用线段树数据结构解决NaiveOperations问题的方法,该问题涉及在一个整数序列上进行加法操作和查询操作,具体包括在指定区间内增加数值以及查询区间内元素除以对应静态排列后向下取整的和。通过维护区间和、最小增加次数和懒惰标记,线段树能够高效地处理大量操作。

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 4311    Accepted Submission(s): 1891


 

Problem Description

In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋

 

 

Input

There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there're no more than 5 test cases.

 

 

Output

Output the answer for each 'query', each one line.

 

 

Sample Input

 

5 12 1 5 2 4 3 add 1 4 query 1 4 add 2 5 query 2 5 add 3 5 query 1 5 add 2 4 query 1 4 add 2 5 query 2 5 add 2 2 query 1 5

 

 

Sample Output

 

1 1 2 4 4 6

 

 

Source

2018 Multi-University Training Contest 2

 

解题思路

线段树维护以下元素

struct T{
	int l, r;   //区间范围 
	int sum;    //区间和 
	int minn;   //此区间最少还需要增加多少,sum才会增加 
	int lazy;   //懒惰标记 
}tree[maxn];

每次更新区间的时候,让minn--,当minn等于0的时候,说明此区间的sum应该增加了,且应该更新新的minn,因为这个增加sum的叶子节点i需要bi才能再增加sum了。所以此时就要找到minn为0了的叶子节点i,使sum++,minn变为bi,再向上更新。

代码如下

#include <iostream>
#include <cstdio>
#define maxn 400005
using namespace std;
typedef long long ll;
struct T{
	int l, r;   //区间范围 
	int sum;    //区间和 
	int minn;   //此区间最少还需要增加多少次sum才会增加 
	int lazy;   //懒惰标记 
}tree[maxn];
int a[100005];
void build(int l, int r, int k)
{
	tree[k].l = l;
	tree[k].r = r;
	if(l == r){
		tree[k].minn = a[l];
		tree[k].lazy = tree[k].sum = 0;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, 2*k);
	build(mid + 1, r, 2*k+1);
	tree[k].minn = min(tree[2*k].minn, tree[2*k+1].minn);
	tree[k].sum = tree[2*k].sum + tree[2*k+1].sum;
	tree[k].lazy = 0;
}
void down(int k)
{
	tree[2*k].minn -= tree[k].lazy;
	tree[2*k+1].minn -= tree[k].lazy;
	tree[2*k].lazy += tree[k].lazy;  //+=
	tree[2*k+1].lazy += tree[k].lazy;
	tree[k].lazy = 0;
}
void work(int k)
{
	if(tree[k].l == tree[k].r){
		tree[k].minn = a[tree[k].l];
		tree[k].sum ++;
		return;
	}
	if(tree[k].lazy)
		down(k);
	if(tree[2*k].minn == 0)
		work(k*2);
	if(tree[k*2+1].minn == 0)
		work(k*2+1);
	tree[k].minn = min(tree[2*k].minn, tree[2*k+1].minn);
	tree[k].sum = tree[2*k].sum + tree[2*k+1].sum;
}
void update(int l, int r, int k)
{
	if(tree[k].l >= l && tree[k].r <= r){
		tree[k].lazy ++;
		tree[k].minn --;
		if(tree[k].minn == 0)
			work(k);
		return;		
	}
	if(tree[k].lazy)
		down(k);
	int mid = (tree[k].l + tree[k].r) / 2;
	if(l <= mid)
		update(l, r, 2*k);
	if(r > mid)
		update(l, r, 2*k+1);
	tree[k].minn = min(tree[2*k].minn, tree[2*k+1].minn);
	tree[k].sum = tree[2*k].sum + tree[2*k+1].sum; 
}
int query(int l, int r, int k)
{
	if(tree[k].l >= l && tree[k].r <= r){
		return tree[k].sum;
	}
	if(tree[k].lazy)
		down(k);
	int mid = (tree[k].l + tree[k].r) / 2;
	int sum = 0;
	if(l <= mid)
		sum += query(l, r, 2*k);
	if(r > mid)
		sum += query(l, r, 2*k+1);
	return sum;
}
int main()
{
	int n, q;
	while(scanf("%d%d", &n, &q) != EOF){
		for(int i = 1; i <= n; i ++)
			scanf("%d", &a[i]);
		build(1, n, 1);
		for(int i = 1; i <= q; i ++){
			char str[10];
			int l, r;
			scanf("%s%d%d", str, &l, &r);
			if(str[0] == 'a'){
				update(l, r, 1);
			}
			else
				printf("%d\n", query(l, r, 1));
		}
	}
	return 0;
}

 

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值