[POJ3468]关于整数的简单题 (你想要的)树状数组区间修改区间查询

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
template<class T>inline void read(T &num)
{
	char ch; int flag=1;
	while(!isdigit(ch=getchar()))if(ch=='-')flag=-flag;
	for(num=ch-'0';isdigit(ch=getchar());num=num*10+ch-'0');
	num*=flag;
}
#define lowbit(x) (x&(-x))
#define LL long long
const int MAXN = 100005;
int n, q;
LL seq[MAXN], T[2][MAXN], ret[2];
inline void update(int x, int val) { for(int i = x; i <= n; i += lowbit(i)) T[0][i] += val, T[1][i] += 1ll * val * x; }
inline void mdf(int l, int r, int val) { update(l, val), update(r+1, -val); }
inline LL sum(int x)
{
	ret[0] = ret[1] = 0;
	for(int i = x; i; i -= lowbit(i))
		ret[0] += T[0][i], ret[1] += T[1][i];
	return ret[0] * (x+1) - ret[1];
}
int main()
{
    read(n), read(q);
	for(int i = 1; i <= n; i++) read(seq[i]), seq[i] += seq[i-1];
	char s[2]; int x, y, z;
	while(q--)
	{
		scanf("%s", s), read(x), read(y);
		if(s[0] == 'Q') printf("%lld\n", sum(y) - sum(x-1) + seq[y] - seq[x-1]);
		else read(z), mdf(x, y, z);
	}
}

 

posted @ 2019-12-14 14:52  _Ark  阅读(51)  评论(0编辑  收藏  举报