区间修改区间查询好像也可以树状数组做?
  • 板块灌水区
  • 楼主monodev
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/10/21 20:46
  • 上次更新2024/10/21 21:53:44
查看原帖
区间修改区间查询好像也可以树状数组做?
533102
monodev楼主2024/10/21 20:46

https://www.luogu.com.cn/problem/P3374

#include <iostream>
using namespace std;

const int MAXN = 1e5 + 1;

long long treeA[MAXN], treeB[MAXN];

int n;

int lowbit(int x) {
	return x & -x;
}

void add(long long tree[], int pos, int val) {
	while(pos <= n) {
		tree[pos] += val;
		pos += lowbit(pos);
	}
}
long long query(long long tree[], int pos) {
	long long res = 0;
	while(pos > 0) {
		res += tree[pos];
		pos -= lowbit(pos);
	}
	return res;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int m;
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		int x;
		cin >> x;
		add(treeB, i, x);
	}
	for(int i = 0; i < m; ++i) {
		int op;
		cin >> op;
		if(op == 1) {
			int x, y, k;
			cin >> x >> y >> k;
			add(treeA, x, k);
			add(treeA, y + 1, -k);
			add(treeB, x, -k * (x - 1));
			add(treeB, y + 1, k * y);
		} else {
			int x, y;
			cin >> x >> y;
			cout << query(treeA, y) * y + query(treeB, y) - query(treeA, x - 1) * (x - 1) - query(treeB, x - 1) << endl;
		}
	}
	
	return 0;
}
2024/10/21 20:46
加载中...