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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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>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;
}

你可能感兴趣的:(HDOJ,query,build,c,include,system)