**#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <ctime>
using namespace std;
const long long mod=100000007;
const long long maxn=5e4+50;
int tree[maxn<<4],nod[maxn];
void pushup(int root)上推
{
tree[root]=tree[root<<1]+tree[root<<1|1];==*2+1
}
void build(int left,int right,int root)
{
if(left==right)
{
tree[root]=nod[left];
return ;
}
int mid=(left+right)/2;
build(left,mid,root<<1);
build(mid+1,right,root<<1|1);
pushup(root);
}
int query(int ql,int qr,int left,int right,int root)
{
if(ql<=left&&qr>=right){
return tree[root];
}
int ans=0,mid=(left+right)>>1;
if(ql<=mid)
{
ans+=query(ql,qr,left,mid,root<<1);
}
if(qr>mid)
{
ans+=query(ql,qr,mid+1,right,root<<1|1);
}
return ans;
}
void update(int loc,int val,int left,int right,int root)
{
if(left==right)
{
tree[root]+=val;
return;
}
int mid=(left+right)>>1;
if(loc<=mid)
{
update(loc,val,left,mid,root<<1);
}
else
{
update(loc,val,mid+1,right,root<<1|1);
}
pushup(root);
}
int main()
{
// freopen("in1.txt","r",stdin);
// freopen("out1.txt","w",stdout);
int t;
while(~scanf("%d",&t))
{
for(int ti=1;ti<=t;ti++){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&nod[i]);
}
build(1,n,1);
char x[15];
int a,b;
cout<<"Case "<<ti<<":"<<endl;
while(1){
scanf("%s",x);
if(x[0]=='E')break;
scanf("%d%d",&a,&b);
if(x[0]=='Q'){
cout<<query(a,b,1,n,1)<<endl;
}
else if(x[0]=='A'){
update(a,b,1,n,1);
}
else if(x[0]=='S'){
update(a,-b,1,n,1);
}
}
}
}
return 0;
}**
hdu 1166 线段树模板
最新推荐文章于 2021-03-09 20:35:06 发布