poj 3468 A Simple Problem with Integers【线段树】

#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define P(x) (x>>1)
#define LL long long
#define PAUSE system("pause)
using namespace std;
#define MAXN 100005
LL s[MAXN<<2];
LL c[MAXN<<2];//add value
inline void pushdown(int l, int r, int p, int m)
{
	if(c[p])
	{
		c[L(p)] += c[p];
		c[R(p)] += c[p];
		s[L(p)] += c[p] * (m-l+1);
		s[R(p)] += c[p] * (r-m);
		c[p] = 0;
	}
}
void build(int l, int r, int p)
{
	c[p] = 0;
	if(l == r)
	{
		scanf("%lld", &s[p]);
		return;
	}
	int m = (l+r)>>1;
	build(l, m, L(p));
	build(m+1,r,R(p));
	s[p] = s[L(p)] + s[R(p)];
}
int L, R, v;
long long query(int l, int r, int p)
{
	if(L<=l && r<=R) return s[p];
	int m = (l+r)>>1;
	pushdown(l, r, p, m);
	LL ans = 0;
	if(L<=m) ans+=query(l, m, L(p));
	if(m<R) ans+=query(m+1,r, R(p));
	return ans;
}
void updata(int l, int r, int p)
{
	if(L<=l && r<=R)
	{
		c[p] += v;
		s[p] += (LL)v * (r-l+1);
		return;
	}
	int m = (l+r)>>1;
	pushdown(l, r, p, m);
	if(L <= m) updata(l, m, L(p));
	if(m < R) updata(m+1, r, R(p));
	s[p] = s[L(p)] + s[R(p)];
}
int main()
{
	int n, q;
	char ch;
	while(scanf("%d%d", &n, &q) != EOF)
	{
		build(1, n, 1);
		while(q--)
		{
			scanf(" %c%d%d", &ch, &L, &R);
			if(ch == 'Q')	
				printf("%lld\n",query(1,n,1) );
			else if(ch == 'C')
			{
				scanf("%d", &v);
				updata(1, n, 1);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值