ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study

本文介绍了一个涉及区间查询与更新操作的编程问题,并提供了一种基于线段树的数据结构解决方案。该方案通过维护两个线段树来高效处理不同类型的查询与更新操作。

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i].

Unfortunately, the longer he learns, the fewer he gets.

That means, if he reads books from ll to rr, he will get a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (LL is the length of [ ll, rr ] that equals to r - l + 1r−l+1).

Now Ryuji has qq questions, you should answer him:

11. If the question type is 11, you should answer how much knowledge he will get after he reads books [ ll, rr ].

22. If the question type is 22, Ryuji will change the ith book's knowledge to a new value.

Input

First line contains two integers nn and qq (nn, q \le 100000q≤100000).

The next line contains n integers represent a[i]( a[i] \le 1e9)a[i](a[i]≤1e9) .

Then in next qq line each line contains three integers aa, bb, cc, if a = 1a=1, it means question type is 11, and bb, cc represents [ ll , rr ]. if a = 2a=2 , it means question type is 22 , and bb, cc means Ryuji changes the bth book' knowledge to cc

Output

For each question, output one line with one integer represent the answer.

样例输入复制

5 3
1 2 3 4 5
1 1 3
2 5 0
1 4 5

样例输出复制

10
8

题意:问区间[l,r]中的每一个值乘以对应的权值求和;

思路:板子,维护两个区间和,照着板子都打错。。。

下面附上我的代码:

#include<bits/stdc++.h>
#define lson o<<1
#define rson o<<1|1
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
LL n, q, b, c;
struct Tree{
	LL l, r;
	LL sum;
};
Tree tree1[maxn << 2];
Tree tree2[maxn << 2];
LL a[maxn];
void Pushup(int o)
{
	tree1[o].sum = tree1[lson].sum + tree1[rson].sum;
	tree2[o].sum = tree2[lson].sum + tree2[rson].sum;
}
void build(LL o, LL l, LL r)
{
	tree1[o].l = l, tree1[o].r = r;
	tree2[o].l = l; tree2[o].r = r;
	if(l == r)
	{
		LL a;
		scanf("%lld", &a);
		tree1[o].sum = a * (n - l + 1);
		tree2[o].sum = a;
		return ;
	}
	LL mid = (tree1[o].l + tree1[o].r) >> 1;
	build(o<<1, l, mid);
	build(o<<1|1, mid + 1, r);
	Pushup(o);
}
void update(LL o, LL pos, LL val)
{
	if(tree1[o].l == tree1[o].r)
	{
		tree1[o].sum = val * (n - tree1[o].l + 1);
		tree2[o].sum = val;
		return ; 
	}
	LL mid = (tree1[o].l + tree1[o].r) >> 1;
	if(mid >= pos)
		update(lson, pos, val);
	else
		update(rson, pos, val);
	Pushup(o);
}
LL Query(LL o, LL L, LL R)
{
	if(L <= tree1[o].l && R >= tree1[o].r)
		return tree1[o].sum;
	LL mid = (tree1[o].l + tree1[o].r) >> 1;
	if(R <= mid)
		return Query(lson, L, R);
	else if(L > mid)
		return Query(rson, L, R);
	else{
		return Query(lson, L, mid) + Query(rson, mid + 1, R);
	}
}
LL Query1(LL o, LL L, LL R)
{
	if(L <= tree2[o].l && R >= tree2[o].r)
		return tree2[o].sum;
	LL mid = (tree2[o].l + tree2[o].r) >> 1;
	if(R <= mid)
		return Query1(lson, L, R);
	else if(L > mid)
		return Query1(rson, L, R);
	else{
		return Query1(lson, L, mid) + Query1(rson, mid + 1, R);
	}
}
int main()
{
	int e;
	cin >> n >> q;
	build(1, 1, n);
	while(q--)
	{
		scanf("%d %lld %lld", &e, &b, &c);
		if(e == 2)
			update(1, b, c);
		else
		{
			printf("%lld\n", Query(1, b, c) - Query1(1, b, c) * (n - c));
		}	
	}	
	return 0;
} 

 

(SCI三维路径规划对比)25年最新五种智能算法优化解决无人机路径巡检三维路径规划对比(灰雁算法真菌算法吕佩尔狐阳光生长研究(Matlab代码实现)内容概要:本文档主要介绍了一项关于无人机三维路径巡检规划的研究,通过对比2025年最新的五种智能优化算法(包括灰雁算法、真菌算法、吕佩尔狐算法、阳光生长算法等),在复杂三维环境中优化无人机巡检路径的技术方案。所有算法均通过Matlab代码实现,并重点围绕路径安全性、效率、能耗和避障能力进行性能对比分析,旨在为无人机在实际巡检任务中的路径规划提供科学依据和技术支持。文档还展示了多个相关科研方向的案例与代码资源,涵盖路径规划、智能优化、无人机控制等多个领域。; 适合人群:具备一定Matlab编程基础,从事无人机路径规划、智能优化算法研究或自动化、控制工程方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 对比分析新型智能算法在三维复杂环境下无人机路径规划的表现差异;② 为科研项目提供可复现的算法代码与实验基准;③ 支持无人机巡检、灾害监测、电力线路巡查等实际应用场景的路径优化需求; 阅读建议:建议结合文档提供的Matlab代码进行仿真实验,重点关注不同算法在收敛速度、路径长度和避障性能方面的表现差异,同时参考文中列举的其他研究案例拓展思路,提升科研创新能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值