树状数组WA,分数70,求助
查看原帖
树状数组WA,分数70,求助
330653
道长先生楼主2020/10/12 15:12
#include  "bits/stdc++.h"
using namespace std;
typedef long long LL;


#define lowbit(x) ((x) & (-x))

vector<LL> BinIdxTree;
vector<LL> helper;
inline void updata(int pos, int val) {
	// 建树从一开始
	// 为什么update自下而上?
	for (int i = pos; i<BinIdxTree.size(); i += lowbit(i)) {
		BinIdxTree[i] += val;
		helper[i] += val * pos;
	}
}

inline LL query(int pos) {
	LL ans = 0;
	for (int i = pos; i; i -= lowbit(i)) {
		ans += (pos + 1) * BinIdxTree[i] - helper[i];
	}
	return ans;
}

inline LL query(int a, int b) {
	return query(b) - query(a - 1);
}


int main() {
	#ifdef OJ
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
	#endif

	#ifndef OJ
	std::ios::sync_with_stdio(false);
	#endif
	LL n, m, op, lo, hi, k, t, last = 0, mn = 0;
	cin >> n >> m;
	helper.resize(n + 5, 0);
	BinIdxTree.resize(n + 5, 0);
	for (int i = 1;i <= n;i++) {
		cin >> t;
		updata(i, t - last);
		last = t;
	}
	for (int i = 1;i <= m;i++) {
		cin >> op;
		switch (op) {
			case 1:
				cin >> lo >> hi >> k;
				updata(lo, k);
				updata(hi + 1, -k);
				break;
			case 2:
				cin >> k;
				updata(1, k);
				updata(2, -k);
				break;
			case 3:
				cin >> k;
				updata(1, -k);
				updata(2, k);
				break;
			case 4:
				cin >> lo >> hi;
				cout <<query(lo, hi) << endl;
				break;
			case 5:
				cout <<query(1)<< endl;
				break;
		}
	}
	return 0;
}

看半天愣是没发现哪里有问题…

2020/10/12 15:12
加载中...