P3368 【模板】树状数组 2 (区间修改,单点查询)

本题链接:【模板】树状数组 2 - 洛谷

题目:

|------------------------------------------------|
| 5 5 1 5 4 2 3 1 2 4 2 2 3 1 1 5 -1 1 3 5 7 2 4 |
[输入]

|--------------|
| 6 10 |
[输出]

思路:

根据题意,这里是需要区间添加值,单点查询值。如果区间添加值中暴力去一个个加值,肯定会TLE,所以我们这里运用到了模板树状数组的重要作用了。

根据 差分 的性质,我们知道,区间加值,我们可以构造一个前缀和数组来表示当前原数组的元素值,对此,进行区间的修改,有效的避免O(n)的时间复杂度。

所以我们可以结合,树状数组的前缀和 + 差分 性质,达到区间修改,单点查询的效果。

下面给出操作函数:

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| cpp // 单点添加元素 inline void Add_pos(int pos,int x) { for(int i = pos;i <= n + 1;i+=lowbit(i)) arr[i] += x; } // 区间添加元素 inline void Add_section(int L,int R,int x) { // 利用差分数组的原理, // 差分树状数组, // 达到区间修改值的效果 Add_pos(L,x); Add_pos(R+1,-x); } |
[区间修改]

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| cpp // 差分前缀和 单点查询 inline int Ask_pos(int pos) { // 利用 差分 性质 // 差分的前缀和,就是当前的元素值 // 所以树状数组求前缀和,返回当前下标的元素值 int ans = 0; for(int i = pos;i;i-=lowbit(i)) ans += arr[i]; return ans; } |
[单点查询]

代码详解如下:

cpp 复制代码
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define endl '\n'
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define lowbit(x) (x&(-x))
#define umap unordered_map
#define All(x) x.begin(),x.end()
//#pragma GCC optimize(3,"Ofast","inline")
#define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 7e7 + 10;

int n,m;
int arr[N];	// 构造 差分树状数组
int a[N];	// 记录原数组初始值

// 单点添加元素
inline void Add_pos(int pos,int x)
{
	for(int i = pos;i <= n + 1;i+=lowbit(i)) arr[i] += x;
}

// 区间添加元素
inline void Add_section(int L,int R,int x)
{
	// 利用差分数组的原理,
	// 差分树状数组,
	// 达到区间修改值的效果
	Add_pos(L,x);
	Add_pos(R+1,-x);	
}

// 差分前缀和 单点查询
inline int Ask_pos(int pos)
{
	// 利用 差分 性质
	// 差分的前缀和,就是当前的元素值
	// 所以树状数组求前缀和,返回当前下标的元素值
	int ans = 0;
	for(int i = pos;i;i-=lowbit(i)) ans += arr[i];
	return ans;
}

inline void solve()
{
	cin >> n >> m;
	for(int i = 1;i <= n;++i)
	{
		cin >> a[i];
		Add_pos(i,a[i] - a[i - 1]);	// 单点添加 初始值 的 差分元素
	}
	while(m--)
	{
		int op;
		cin >> op;
		if(op == 1)
		{
			int L,R,x;
			cin >> L >> R >> x;
			Add_section(L,R,x);	// 区间添加值	
		}else
		{
			int pos;
			cin >> pos;	// 差分前缀和单点查询
			cout << Ask_pos(pos) << endl;
		}
	}
}

signed main()
{
//	freopen("a.txt", "r", stdin);
//	IOS;
	int _t = 1;
//	cin >> _t;
	while (_t--)
	{
		solve();
	}

	return 0;
}

最后提交:

相关推荐
代码雕刻家19 分钟前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain19 分钟前
算法 | 位运算(哈希思想)
算法
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
我是哈哈hh4 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
Tisfy4 小时前
LeetCode 2187.完成旅途的最少时间:二分查找
算法·leetcode·二分查找·题解·二分
Mephisto.java4 小时前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
robin_suli4 小时前
滑动窗口->dd爱框框
算法
丶Darling.4 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5205 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法