实现区间改单点查的树状数组求调
查看原帖
实现区间改单点查的树状数组求调
446979
masonxiong楼主2024/5/3 22:31

RE on #2 #9 #10

不知道为什么,但是在其它 OJ 上没有问题

#include <iostream>
using namespace std;

const int MOD = 1e9 + 7;

template<class T>
class my_Fenwick_Tree {
	private:
		unsigned int n;
		T *my_fenwick_tree[2];
		int lowbit(const int &num) {
			return num & (-num);
		}
		void _modify(int pos, const T &value) {
			for (T v = pos * value; pos <= n; pos += lowbit(pos)) {
				my_fenwick_tree[0][pos] += value;
				my_fenwick_tree[1][pos] += v;
			}
		}
		T _query(const bool &index, int pos) {
			T res = 0;
			for (; pos; pos -= lowbit(pos))
				res += my_fenwick_tree[index][pos];
			return res;
		}
	public:
		my_Fenwick_Tree() {};
		void modify(int L, int R, const T &value) {
			_modify(L, value);
			_modify(R + 1, 0 - value);
		}
		T query(int L, int R) {
			return (_query(0, R) * (R + 1) - _query(0, L - 1) * L
				   -_query(1, R) + _query(1, L - 1)) % MOD;
		}
		void build(const T *begin, const T *end) {
			n = end - begin;
			begin = begin - 1;
			for (int i = 0; i < 2; i++) {
				my_fenwick_tree[i] = new T [n + 1];
				for (int j = 0; j <= n; j++)
					my_fenwick_tree[i][j] = 0;
			}
			for (int i = 1; i <= n; i++)
				modify(i, i, begin[i]);
		}
};

long long a[114514];
my_Fenwick_Tree<long long> mst;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	long long n, q, op, x, y, k;
	cin >> n >> q;
	for (long long i = 1; i <= n; i++)
		cin >> a[i];
	mst.build(a + 1, a + n + 1);
	while (q--) {
		cin >> op >> x;
		if (op == 2)
			cout << mst.query(x, x) << '\n';
		else
			cin >> y >> k, mst.modify(x, y, k);
	}
	return 0;
}
2024/5/3 22:31
加载中...