hud1166
来源:互联网 发布:多媒体管理系统 源码 编辑:程序博客网 时间:2024/05/19 01:11
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 50005
int num[N];
struct Tree
{
int l,r,sum;
}
tree[N*4];
void build(int root,int l,int r)//root表示根节点,区间范围为[1,r]
{
tree[root].l=l;
tree[root].r=r;
if(tree[root].l==tree[root].r)
{
tree[root].sum=num[l];//赋除值
return;
}
int mid=(l+r)>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
}
void update(int root,int pos,int val)//pos点的值更新为val
{
if(tree[root].l==tree[root].r)
{
tree[root].sum=val;
return;
}
int mid=(tree[root].l+tree[root].r)>>1;
if(pos<=mid) update(root<<1,pos,val);
else update(root<<1|1,pos,val);
tree[root].sum= tree[root<<1].sum+ tree[root<<1|1].sum;
}
int query(int root,int L,int R)//[L,R]表示要查询的区间
{
if(L<=tree[root].l&&R>=tree[root].r)
{
return tree[root].sum;
}
int mid=(tree[root].l+tree[root].r)>>1;
int ret=0;
if(L<=mid)
ret+=query(root<<1,L,R);
if(R>mid)
ret+=query(root<<1|1,L,R);
return ret;
}
int main()
{
int t,cas=1,n,a,b;
char str[10];
scanf("%d",&t);
{
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
build(1,1,N);
printf("Case %d:\n",cas++);
while(scanf("%s",str),strcmp(str,"End"))
{
scanf("%d%d",&a,&b);
if(strcmp(str,"Query")==0)
{
if(a>b) swap(a,b);
printf("%d\n",query(1,a,b));
}
else if(strcmp(str,"Add")==0)
{
num[a]=num[a]+b;
update(1,a,num[a]);
}
else if(strcmp(str,"Sub")==0)
{
num[a]=num[a]-b;
update(1,a,num[a]);
}
}
}
}return 0;
}
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 50005
int num[N];
struct Tree
{
int l,r,sum;
}
tree[N*4];
void build(int root,int l,int r)//root表示根节点,区间范围为[1,r]
{
tree[root].l=l;
tree[root].r=r;
if(tree[root].l==tree[root].r)
{
tree[root].sum=num[l];//赋除值
return;
}
int mid=(l+r)>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
}
void update(int root,int pos,int val)//pos点的值更新为val
{
if(tree[root].l==tree[root].r)
{
tree[root].sum=val;
return;
}
int mid=(tree[root].l+tree[root].r)>>1;
if(pos<=mid) update(root<<1,pos,val);
else update(root<<1|1,pos,val);
tree[root].sum= tree[root<<1].sum+ tree[root<<1|1].sum;
}
int query(int root,int L,int R)//[L,R]表示要查询的区间
{
if(L<=tree[root].l&&R>=tree[root].r)
{
return tree[root].sum;
}
int mid=(tree[root].l+tree[root].r)>>1;
int ret=0;
if(L<=mid)
ret+=query(root<<1,L,R);
if(R>mid)
ret+=query(root<<1|1,L,R);
return ret;
}
int main()
{
int t,cas=1,n,a,b;
char str[10];
scanf("%d",&t);
{
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
build(1,1,N);
printf("Case %d:\n",cas++);
while(scanf("%s",str),strcmp(str,"End"))
{
scanf("%d%d",&a,&b);
if(strcmp(str,"Query")==0)
{
if(a>b) swap(a,b);
printf("%d\n",query(1,a,b));
}
else if(strcmp(str,"Add")==0)
{
num[a]=num[a]+b;
update(1,a,num[a]);
}
else if(strcmp(str,"Sub")==0)
{
num[a]=num[a]-b;
update(1,a,num[a]);
}
}
}
}return 0;
}
0 0