树状数组求调
  • 板块P2357 守墓人
  • 楼主rainygame
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/3/21 17:34
  • 上次更新2023/10/23 20:56:18
查看原帖
树状数组求调
804607
rainygame楼主2023/3/21 17:34

RT。开了 long long 了,但过不去样例。

#include <bits/stdc++.h>
using namespace std;
#define MAXN 500001

int n, f, l, r, opt;
long long k;
long long a[MAXN], sum[MAXN], c1[MAXN], c2[MAXN];

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

void add(int i, long long k){
	long long x = k-1;
	while (i <= n){
		c1[i] += k;
		c2[i] += x*k;
		i += lowbit(i);
	}
}

long long get_sum(int x){
	long long ans = 0;
	while (x){
		ans += x*c1[k]-c2[k];
		x -= lowbit(x);
	}
	return ans;
}

void add(int l, int r, long long k){
	add(l, k);
	add(r+1, -k);
}

long long query(int l, int r){
	return get_sum(r) - get_sum(l-1);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> n >> f;
	for (int i=1; i<=n; i++){
		cin >> a[i];
		add(i, a[i]-a[i-1]);
	}
	
	while (f--){
		cin >> opt;
		switch (opt){
			case 1:
				cin >> l >> r >> k;
				add(l, r, k);
				break;
			case 2:
				cin >> k;
				add(1, 1, k);
				break;
			case 3:
				cin >> k;
				add(1, 1, -k);
				break;
			case 4:
				cin >> l >> r;
				cout << query(l, r) << '\n';
				break;
			case 5:
				cout << query(1, 0) << '\n';
		}
	}
	
	return 0;
}

2023/3/21 17:34
加载中...