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


    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 ",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 ",sum(n));                  
        }                                           
        return 0;                                   
    }                                                                              
  • 相关阅读:
    Angular 项目打包之后,部署到服务器,刷新访问404解决方法
    C# yield return; yield break;
    C#获取枚举描述
    21、uwp UI自动化测试(WinAppDriver)
    20、uwp打包失败(All app package manifests in a bundle must declare the same values under the XPath *[local-name()='Package']/*[local-name()='Dependencies'])
    19、UWP 新特性(Creator Update)
    18、利用 Windows Device Portal 获取用户闪退 dump
    17、uwp 打包失败记录
    爱奇艺招聘uwp开发
    16、C++获取磁盘空间的方法
  • 原文地址:https://www.cnblogs.com/forgot93/p/3617069.html
Copyright © 2020-2023  润新知