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<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
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<<sum[j]<<" ";
//			cout<<endl;
//			for(int j=1;j<=50;++j)
//				cout<<tree1[j]<<" ";
//			cout<<endl;
//			for(int j=1;j<=50;++j)
//				cout<<tree2[j]<<" ";
//			cout<<endl;
		char s[10];
		scanf("%s",s);
//		cout<<s<<endl;
		if(strcmp(s,"Q")==0)
		{
			ll a,b;
			scanf("%lld%lld",&a,&b);
			ll res=sum[b]-sum[a-1];
			res=res+(b+1)*Query(b,tree1)-a*Query(a-1,tree1);
			res=res-(Query(b,tree2)-Query(a-1,tree2));
			cout<<res<<endl;
			//sum=sum_tree1-sum_tree2
//			cout<<(sum[b]+(b+1)*Query(b,tree1)-Query(b,tree2))-
//			(sum[a-1]+a*Query(a-1,tree1)-Query(a-1,tree2))
//			<<endl;
			
		}
		else if(strcmp(s,"C")==0)
		{
			ll a,b,x;
			scanf("%lld%lld%lld",&a,&b,&x);
			updata(a,x,tree1);
			updata(b+1,-x,tree1);
			
			updata(a,a*x,tree2);
			updata(b+1,-(b+1)*x,tree2);
			
		}
	}
}

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

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
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<<i<<" "<<tree[i]<<":"<<tree[i<<1]<<" "<<tree[i<<1|1]<<endl;
//	}
//}

int main()
{
	std::ios::sync_with_stdio(false);
	int n,q;
	cin>>n>>q;
	build_tree(1,n,1);
	for(int i=0;i<q;++i)
	{
		//find_error();
		char ch;
		cin>>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<<Query(a,b,1,n,1)<<endl;
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值