hdu 1166(线段树)

/*
  裸题
*/

#include<stdio.h>
#include<string.h>

#define N  50000+10

struct node
{
  int l,r,sum;       
}Tree[3*N];

int a[N];

void build(int l,int r,int root)
{
    Tree[root].l=l;
    Tree[root].r=r;  
    if(l==r)
    {
     Tree[root].sum=a[l];
     
     return ;    
    }
  int mid=(l+r)>>1;
  build(l,mid,2*root);
  build(mid+1,r,2*root+1);
  Tree[root].sum=(Tree[2*root].sum+Tree[2*root+1].sum);
}

int query(int l,int r,int root)
{
    if(l==Tree[root].l && r==Tree[root].r)
    {
      return  Tree[root].sum;
    }
    int mid=(Tree[root].l+Tree[root].r)>>1;
    
    if(r<=mid)
    {
          query(l,r,2*root);      
    }
    else if(l>mid)
    {
          query(l,r,2*root+1);  
    }
    else 
    {
       return  query(l,mid,2*root)+ query(mid+1,r,2*root+1);
    }
}


void update(int l,int r,int changenumber,int root)
{
     Tree[root].sum+=changenumber; 
     if(Tree[root].l==l && Tree[root].r==r)
     {
        return ;        
     }
     int mid=(Tree[root].l+Tree[root].r)>>1;
     
     if(r<=mid)
       update(l,r,changenumber,2*root);
     else if(l>mid)
       update(l,r,changenumber,2*root+1);
     else 
     {
        update(l,mid,changenumber,2*root);
        update(mid+1,r,changenumber,2*root+1);          
     }
     
     Tree[root].sum=Tree[2*root].sum+Tree[2*root+1].sum;
}


int main()
{
    int t,n,x,y;
    char str[20];
    scanf("%d",&t);
    //while(t--)
	for(int Case=1;Case<=t;Case++)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
          scanf("%d",&a[i]);
        printf("Case %d:\n",Case);
        build(1,n,1);
        getchar();
        while(scanf("%s",str))
        {
          if(strcmp(str,"End")==0)
            break;
          
          scanf("%d%d",&x,&y);  
          
          if(str[0]=='Q')
          {
              printf("%d\n",query(x,y,1));           
          }
          else if(str[0]=='A')
          {
              update(x,x,y,1); 
          }
          else
          {
              update(x,x,-y,1);
          }
       }           
    }
  return 0;    
}



#include<stdio.h>
#include<string.h>
#define maxn 50001
int a[maxn];
int lowbit(int x)
{
    return (x&(-x));
}
void updata(int x,int y)
{
    int i;
    for(i=x;i<maxn;i+=lowbit(i))
        a[i]+=y;
}
int getsum(int x)
{
    int sum=0,i;
    for(i=x;i>0;i-=lowbit(i))
        sum+=a[i];
    return sum;
}

int main()
{
    char chars[10];
    int  t,x,y,i,n,k,j;
    scanf("%d",&t);
    for(j=1;j<=t;j++)
    {
        
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&k);
            updata(i,k);
        }
        printf ("Case %d:\n", j) ;
        while(scanf("%s",chars))
        {
            if(chars[0]=='E')
                break;
            else if(chars[0]=='A')
            {
                scanf("%d%d",&x,&y); //第x增加y人
                updata(x,y);
            }
            else if(chars[0]=='S')
            {
                scanf("%d%d",&x,&y);
                updata(x,-y);
            }
            else
            {
                scanf("%d%d",&x,&y);
                printf("%d\n",getsum(y)-getsum(x-1));
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值