hdu 1166数状数组

http://acm.hdu.edu.cn/showproblem.php?pid=1166

code :

#include <iostream>
#include "string.h"
#include "stdio.h"
#include "memory.h"
using namespace std;
int n,c[50005];
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
void inster(int x,int i)
{
   while(x<=n)
    {
        c[x]+=i;
        x+=lowbit(x);
    }
}
int main(int argc, char *argv[])
{
    int t;
    scanf("%d",&t);
    int casea=1;
    while(t--)
    {
    
        memset(c,0,sizeof(c));
        int i;
        
        scanf("%d",&n);
        int x;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            inster(i,x);
        }
        
        char str[10];
        int a,b;
            printf("Case %d:\n",casea);
        casea+=1;
        scanf("%s",&str);
        while(strcmp(str,"End")!=0)
        {
            scanf("%d %d",&a,&b);
            if(strcmp(str,"Query")==0)    //问人数 
            {
             
                  int i;
                  
                      printf("%d\n",sum(b)-sum(a-1));
                  
                                
               
               
            }else if(strcmp(str,"Add")==0)    //问人数 
            {
                inster(a,b);
                //inster(c[a+1],-b);
            }else if(strcmp(str,"Sub")==0)    //问人数 
            {
                inster(a,-b);
                //inster(c[a+1],+b);
            }
            scanf("%s",&str);
            if(strcmp(str,"End")==0){
                break;
            }else
            {
                scanf("%d %d",&a,&b);
            }
        }
        
    }
    return 0;
}

 

 

 

你可能感兴趣的:(hdu 1166数状数组)