hdu1166线段树法
#include "iostream"
#include "string"
#define Max 50005
using namespace std;
int N,sum;
struct node_tree
{
int left;
int right;
int num;
};
node_tree node[Max*3];
void build(int i,int l,int r)//建树
{
node[i].left=l;
node[i].right=r;
node[i].num=0;
if (l<r)
{
int mid=(l+r)>>1;
build(2*i,l,mid);
build(2*i+1,mid+1,r);
}
}
void insert(int i,int l,int r,int num)//插入人数
{
if (node[i].left<=l&&node[i].right>=r)
{
node[i].num+=num;
}
if(node[i].left<node[i].right)
{
int mid=(node[i].left+node[i].right)>>1;
if (mid>=r)
{
insert(2*i,l,r,num);
}
else if (mid<l)
{
insert(2*i+1,l,r,num);
}
else
{
insert(2*i,l,mid,num);
insert(2*i+1,mid+1,r,num);
}
}
}
void cnt(int i,int l,int r)//计算人数
{
if (node[i].left>=l&&node[i].right<=r)
{
sum+=node[i].num;
return ;
}
if (node[i].left<node[i].right)
{
int mid=(node[i].left+node[i].right)>>1;
if (mid>=r)
{
cnt(2*i,l,r);
}
else if (mid<l)
{
cnt(2*i+1,l,r);
}
else
{
cnt(2*i,l,mid);
cnt(2*i+1,mid+1,r);
}
}
}
int main()
{
int T,num=1;
char str[6];
cin>>T;
while(T--)
{
int i,j,m;
scanf("%d",&N);
build(1,1,N);
for (i=1;i<=N;i++)
{
scanf("%d",&m);
insert(1,i,i,m);
}
printf("Case %d:\n",num++);
while(1)
{
scanf("%s",str);
if(str[0]=='A')
{
scanf("%d%d",&i,&m);
insert(1,i,i,m);
}
else if (str[0]=='S')
{
scanf("%d%d",&i,&j);
insert(1,i,i,-j);
}
else if (str[0]=='Q')
{
sum=0;
scanf("%d%d",&i,&j);
cnt(1,i,j);
printf("%d\n",sum);
}
else
{
break;
}
}
}
return 0;
}