poj-3468-A Simple Problem with Integers(树状数组||线段树,区间刷新,区间求和)

题目链接:http://poj.org/problem?id=3468

Description

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.

Input

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 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

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

ti'm题目大意:给你n个数,m个操作,

每个操作有两种情况,输入Q时,输入两个数,a,b,求a~b的值的和

输入C时,输入三个数,a,b,x,让a~b区间的每个数都加上x。

区间修改+qu'j区间查询,模板

原理:


区间修改,区间查询:
引入一个函数:sigma(a,i)表示a数组的前i项和
引入一个差分数组,c[i]=a[i]-a[i-1];
因此:a[i]=sigma(c,i);
a[i]=c[1]+c[2]+c[3]+...+c[k];
因此:
sum(1~k)=a[1]+a[2]+a[3]+a[4]+...+a[k];
=c[1]+( c[1]+c[2] )+( c[1]+c[2]+c[3] )+...+(c[1]+c[2]+c[3]+...+c[k]).
=k*c[1]+(k-1)*c[2]+(k-2)*c[3]+...+ 1*c[k];
合并:
sum(1~k)=sigma( (k-i+1)c[i] );//这个就是求和,1~k项的和,中间的是通项
将它分开:
sum(1~k)=sigma( (k+1)*c[i] )-sigma( i*c[i] );
因此分成两个数组,建立两个树状数组, 
分别为:
tree1[i]=c[i];
/*-------*/
tree2[i]=i*c[i]
用第一个减去第二个就是区间查询的结果了
计算的时候:
ans=(k+1)*tree1[i]-tree2[i];

ac:

#include
#include  
#include  
  
//#include   
//#include
#include  
#include  
#include  
#include 
#include  
#include  
#include  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
#define clean(a,b) memset(a,b,sizeof(a))// 水印 

const ll N=100100;

ll tree1[N],tree2[N];
ll sum[N];
ll n,q;

ll lowbit(ll i)
{
	return i&(-i);
}

void updata(ll i,ll x,ll *tree)
{
	while(i<=n)
	{
		tree[i]=tree[i]+x;
		i=i+lowbit(i);
	}
}

ll Query(ll i,ll *tree)
{
	ll sum=0;
	while(i>0)
	{
		sum=sum+tree[i];
		i=i-lowbit(i);
	}
	return sum;
}

int main()//poj-3468
{
	scanf("%lld%lld",&n,&q);
	clean(tree1,0);
	clean(tree2,0);
	clean(sum,0);
	for(ll i=1;i<=n;++i)
	{
		scanf("%lld",&sum[i]);
		sum[i]=sum[i]+sum[i-1];
	}
	for(ll i=1;i<=q;++i)
	{
//			for(int j=1;j<=50;++j)
//				cout<

补充一下,这个题也可以用线段树解决,虽然感觉有点麻烦:

ac:

#include
#include  
#include  
  
//#include   
//#include
#include  
#include  
#include  
#include 
#include  
#include  
#include  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

ll tree[100100<<2],add[100100<<2];


void build_tree(int l,int r,int rt)
{
	if(l==r)
	{
		cin>>tree[rt];
		return ;
	}
	int mid=(r+l)>>1;
	build_tree(l,mid,rt<<1);
	build_tree(mid+1,r,rt<<1|1);
	tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}

void push_down(int rt,int ln,int rn)
{
	if(add[rt])
	{
		add[rt<<1]=add[rt<<1]+add[rt];
		add[rt<<1|1]=add[rt<<1|1]+add[rt];
		
		tree[rt<<1]=add[rt]*ln+tree[rt<<1];
		tree[rt<<1|1]=add[rt]*rn+tree[rt<<1|1];
		
		add[rt]=0;
	}
}

void updata(int L,int R,int x,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	{
		tree[rt]=tree[rt]+(ll)(x*(r-l+1));
		add[rt]=add[rt]+x;
		return ;
	}
	int mid=(r+l)>>1;
	push_down(rt,mid-l+1,r-mid);
	
	if(L<=mid)
		updata(L,R,x,l,mid,rt<<1);
	if(R>mid)
		updata(L,R,x,mid+1,r,rt<<1|1);
	tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}

ll Query(int L,int R,int l,int r,int rt)
{
	if(L<=l&&r<=R)
		return tree[rt];
	int mid=(r+l)>>1;
	push_down(rt,mid-l+1,r-mid);
	
	ll res=0;
	if(L<=mid)
		res=res+Query(L,R,l,mid,rt<<1);
	if(R>mid)
		res=res+Query(L,R,mid+1,r,rt<<1|1);
	return res;
}

//void find_error()
//{
//	for(int i=0;i<(n<<1);++i)
//	{
//		cout<>n>>q;
	build_tree(1,n,1);
	for(int i=0;i>ch;
		if(ch=='C')
		{
			int a,b,x;
			cin>>a>>b>>x;
			updata(a,b,x,1,n,1);
		}
		else if(ch=='Q')
		{
			int a,b;
			cin>>a>>b;
			cout<

 

你可能感兴趣的:(树状数组)