POJ3468--A Simple Problem with Integers

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

455915

/*
注意在修改的时候边维护区间和。
*/
#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 110008
long long int A[maxn];
struct ST
{
	int l,r;
	long long int add,sum;//sum用来表示这一段的区间和。
}st[4*maxn];
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l==r)
	{
		st[id].sum=A[l];
		st[id].add=0;
		return;
	}
	int mid=(l+r)>>1;
	buildtree(2*id,l,mid);
	buildtree(2*id+1,mid+1,r);
	st[id].sum=st[2*id].sum+st[2*id+1].sum;
	st[id].add=0;
}
void update(int id,int l,int r,int key)//主要是更新的时候怎么确定哪些还是不能用的。
{
	if(st[id].l==l&&st[id].r==r)
	{
		st[id].add+=key;
		st[id].sum+=(st[id].r-st[id].l+1)*key;
		return;
	}
	if(st[id].add)
	{
		st[2*id].add+=st[id].add;
		st[2*id].sum+=(st[2*id].r-st[2*id].l+1)*(st[id].add);
		st[2*id+1].add+=st[id].add;
		st[2*id+1].sum+=(st[2*id+1].r-st[2*id+1].l+1)*(st[id].add);
		st[id].add=0;
	}
	if(st[2*id].r>=r)
	{
		update(2*id,l,r,key);
		st[id].sum=st[2*id].sum+st[2*id+1].sum;
		return;
	}
	if(st[2*id+1].l<=l)
	{
		update(2*id+1,l,r,key);
		st[id].sum=st[2*id].sum+st[2*id+1].sum;
		return;
	}
	update(2*id,l,st[2*id].r,key);
	update(2*id+1,st[2*id+1].l,r,key);
	st[id].sum=st[2*id].sum+st[2*id+1].sum;
}
long long int query(int id,int l,int r)
{
	if(st[id].l==l&&st[id].r==r)
	{
		return st[id].sum;
	}
	if(st[id].add)
	{
		st[2*id].add+=st[id].add;
		st[2*id].sum+=(st[2*id].r-st[2*id].l+1)*(st[id].add);
		st[2*id+1].add+=st[id].add;
		st[2*id+1].sum+=(st[2*id+1].r-st[2*id+1].l+1)*(st[id].add);
		st[id].add=0;
	}
	if(st[2*id].r>=r)
	{
		return query(2*id,l,r);
	}
	if(st[2*id+1].l<=l)
	{
		return query(2*id+1,l,r);
	}
	return query(2*id,l,st[2*id].r)+query(2*id+1,st[2*id+1].l,r);
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)==2)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%lld",&A[i]);
		}
		buildtree(1,1,n);
		char ope;int u,v,w;
		for(int i=1;i<=m;i++)
		{
			getchar();
			ope=getchar();
			if(ope=='C')
			{
				scanf("%d%d%d",&u,&v,&w);
				update(1,u,v,w);
			}
			else
			{
				scanf("%d%d",&u,&v);
				printf("%lld\n",query(1,u,v));
			}
		}
	}
	return 0;
}

代码风格更新后:


 

/*
更新的时候不仅要更新add 还要更新sum.
PS:当区间更新有难度的时候,可以试着多添加的懒惰标记。
然后传递的时候多传递个懒惰标记
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define maxn 100008
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
int A[maxn];
struct ST
{
	int l,r;
	long long int add,sum;
}st[4*maxn];
void PushDown(int id)
{
	st[2*id].add+=st[id].add;
	st[2*id+1].add+=st[id].add;
	st[2*id].sum+=st[id].add*(st[2*id].r-st[2*id].l+1);
	st[2*id+1].sum+=st[id].add*(st[2*id+1].r-st[2*id+1].l+1);
	st[id].add=0;
}
void PushUp(int id)
{
	st[id].sum=st[2*id].sum+st[2*id+1].sum;
}
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l==r)
	{
		st[id].add=0;
		st[id].sum=A[l];
		return;
	}
	int mid=(l+r)>>1;
	buildtree(lson);
	buildtree(rson);
	st[id].add=0;
	st[id].sum=st[2*id].sum+st[2*id+1].sum;
}
void update(int id,int l,int r,int key)
{
	if(st[id].l==l && st[id].r==r)
	{
		st[id].add+=key;
		st[id].sum+=key*(r-l+1);
		return;
	}
	PushDown(id);
	if(st[2*id].r>=r)
	{
		update(2*id,l,r,key);
		PushUp(id);
		return;
	}
	if(st[2*id+1].l<=l)
	{
		update(2*id+1,l,r,key);
		PushUp(id);
		return;
	}
	update(2*id,l,st[2*id].r,key);
	update(2*id+1,st[2*id+1].l,r,key);
	PushUp(id);
}
long long int query(int id,int l,int r)
{
	if(st[id].l==l && st[id].r==r)
	{
		return st[id].sum;
	}
	PushDown(id);
	if(st[2*id].r >= r)
	{
		return query(2*id,l,r);
	}
	if(st[2*id+1].l <= l)
	{
		return query(2*id+1,l,r);
	}
	return query(2*id,l,st[2*id].r)+query(2*id+1,st[2*id+1].l,r);
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)==2)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&A[i]);
		}
		buildtree(1,1,n);
		char c;int u,v,w;
		for(int i=1;i<=m;i++)
		{
			cin>>c;
			if(c=='C')
			{
				scanf("%d%d%d",&u,&v,&w);
				update(1,u,v,w);
			}
			else 
			{
				scanf("%d%d",&u,&v);
				printf("%lld\n",query(1,u,v));
			}
		}
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值