poj 3243:A Simple Problem with Integers

3243:A Simple Problem with Integers

总时间限制: 
5000ms 
单个测试点时间限制: 
2000ms 
内存限制: 
131072kB
描述
You have N integers, A1, A2, ... , 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.
输入
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
输出
You need to answer all Q commands in order. One answer in a line.
样例输入
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
样例输出
4
55
9
15
提示
The sums may exceed the range of 32-bit integers.
来源
POJ Monthly--2007.11.25, Yang Yi

线段树
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
int n,q,a,b,c,cnt=0;
struct Node
{
	int l,r;
	long long sum,inc;
	Node*left;
	Node*right; 
}T[200010];
void Build(Node*root,int l,int r)
{
	root->l=l;
	root->r=r;
	root->sum=0;
	root->inc=0;
	if(l==r)return;
	int mid=(l+r)/2;
	cnt++;
	root->left=T+cnt;
	cnt++;
	root->right=T+cnt;
	Build(root->left,l,mid);
	Build(root->right,mid+1,r);
}
void  Insert(Node*root,int index,int a) 
{
	root->sum+=a;
	if(root->l==root->r)return;
	int mid=(root->l+root->r)/2;
	if(index<=mid)
	{
		Insert(root->left,index,a);
	}
	else
	{
		Insert(root->right,index,a);
	}
}
void Add(Node*root,int l,int r,int c)
{
	if(root->l==l&&root->r==r)
	{
		root->inc+=c;
		return; 
	}
	root->sum+=(r-l+1)*c;
	int mid=(root->l+root->r)/2;
	if(r<=mid)
	{
		Add(root->left,l,r,c);
	}
	else if(l>mid)
	{
		Add(root->right,l,r,c);
	}
	else
	{
		Add(root->left,l,mid,c);
		Add(root->right,mid+1,r,c);
	}
}
long long Query(Node*root,int l,int r)
{
	if(root->l==l&&root->r==r)
	{
		return root->sum+root->inc*(r-l+1);
	}	
	int mid=(root->l+root->r)/2;
	root->sum+=(root->r-root->l+1)*root->inc;
	Add(root->left,root->l,mid,root->inc);
	Add(root->right,mid+1,root->r,root->inc);
	root->inc=0;
	if(r<=mid)
	{
		return Query(root->left,l,r);
	}
	else if(l>mid)
	{
		return Query(root->right,l,r);
	}
	else
	{
		return Query(root->left,l,mid)+Query(root->right,mid+1,r);
	}
}
int main()
{
	cin>>n>>q;
	Build(&T[0],1,n);
	for(int i=1;i<=n;++i)
	{
		cin>>a;
		Insert(&T[0],i,a);
	}
	
	for(int i=0;i<q;++i)
	{
		string s;
		cin>>s;
		if(s[0]=='C') 
		{
			cin>>a>>b>>c;
			Add(T,a,b,c);
		}
		else
		{
			cin>>a>>b;
			cout<<Query(T,a,b)<<endl;
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值