I hate it--线段树

#include<iostream>
using namespace std;
const int maxn=10005;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
int tree_sum[maxn<<2];

void build(int root,int l,int r)
{
	if(l==r)
	{
		cin>>tree_sum[root];
		return ;
	}
	int mid=(l+r)>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	tree_sum[root]=tree_sum[root<<1]+tree_sum[root<<1|1];
}
void update(int root,int l,int r,int pos,int val)
{
	if(l==r&&l==pos)
	{
		tree_sum[root]=val;
		return ;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
		update(root<<1,l,mid,pos,val);
	else
		update(root<<1|1,mid+1,r,pos,val);
	tree_sum[root]=tree_sum[root<<1]+tree_sum[root<<1|1];
}
int query(int root,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		return tree_sum[root];
	}
	int mid=(r+l)>>1,sum=0;
	if(L<=mid)
		sum+=query(root<<1,l,mid,L,R);
	if(mid<R)
		sum+=query(root<<1|1,mid+1,r,L,R);
	return sum;
}

int main()
{
   int n,m;
   while(~scanf("%d%d",&n,&m))
   {
	   build(l,n,l);
	   while(m--)
	   {
		   char op[2];
		   int a,b;
		   cin>>op>>a>>b;
		   if(op[0]=='Q')
			   printf("%d\n",query(a,b,l,n,l));
		   else update(a,b,l,n,l);
	   }
   }
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值