关于树状数组修改区间,求点值

PS:很多版本的方案都是:long long sum(int x)       
{                          
long long  k=0;           
while (x<=n)              
{                         
 k+=f[x];                
 x+=lowbit(x);          
}                         
return k;                 
}                          
                           
void update(int x,int b)   
{                          
while (x)                 
{                         
  f[x]+=b;               
  x-=lowbit(x);         
}                         
}                          

 

向下更新指,而我总觉得与修改点,求段的方法相反, 比较难记住,那为啥不可以按照修改点的方法去做呢?

事实证明可以的:比如HDU1556就实验可以:

其实想想也应该可以的,当他们的位置倒转,不就可以了吗? 

 两份完整代码:#include<stdio.h>                      

#include<math.h>                       
#include<string.h>                     
long long  f[100005];                  
int n;                                 
int lowbit(int x)                      
{                                      
    return x&(-x);                        
}                                      
long long sum(int x)                   
{                                      
    long long  k=0;                       
    while (x)                             
    {                                     
      k+=f[x];                            
      x-=lowbit(x);                          
    }                                     
    return k;                             
}                                      
                                       
void update(int x,int b)               
{                                      
    while (x<=n)                          
    {                                     
       f[x]+=b;                           
       x+=lowbit(x);                         
    }                                     
}                                      
                                       
int main()                             
{                                      
    while (scanf("%d",&n)!=EOF&&n)        
    {                                     
        memset(f,0,sizeof(f));               
        for (int i=1;i<=n;i++)               
        {                                    
            int l,r;                            
            scanf("%d%d",&l,&r);                
            update(r+1,-1);                     
            update(l,1);                        
        }                                    
        for (int i=1;i<n;i++)                
        printf("%I64d ",sum(i));             
        printf("%I64d\n",sum(n));            
    }                                     
    return 0;                           
}   

2::
 #include<stdio.h>                            
#include<math.h>                             
#include<string.h>                           
long long  f[100005];                        
int n;                                       
int lowbit(int x)                            
{                                            
    return x&(-x);                              
}                                            
long long sum(int x)                         
{                                            
    long long  k=0;                             
    while (x<=n)                                
    {                                           
      k+=f[x];                                  
      x+=lowbit(x);                                
    }                                           
    return k;                                   
}                                            
                                             
void update(int x,int b)                     
{                                            
    while (x)                                   
    {                                           
       f[x]+=b;                                 
       x-=lowbit(x);                               
    }                                           
}                                            
                                             
int main()                                   
{                                            
    while (scanf("%d",&n)!=EOF&&n)              
    {                                           
        memset(f,0,sizeof(f));                     
        for (int i=1;i<=n;i++)                     
        {                                          
            int l,r;                                  
            scanf("%d%d",&l,&r);                      
            update(r,1);                              
            update(l-1,-1);                           
        }                                          
        for (int i=1;i<n;i++)                      
        printf("%I64d ",sum(i));                   
        printf("%I64d\n",sum(n));                  
    }                                           
    return 0;                                   
}                                                                              

posted on 2014-03-22 03:18  forgot93  阅读(533)  评论(0编辑  收藏  举报

导航