zoukankan      html  css  js  c++  java
  • 树状数组模板

     1 /*
     2 树状数组基本模板    
     3 2014.4.20
     4 
     5 初始:设a[i]为要读入的数组,共n个元素,则 
     6     for i:=1 to n do add(i,a[i]); 
     7 
     8 add(x,d):在x的位置加上d
     9 sum(x):求a[1]~a[x]的和 
    10 */
    11  
    12 #include <iostream>
    13 #include <cstring>
    14 using namespace std;
    15 int c[1000];
    16 int n;
    17 
    18 int lowbit(int x)
    19 {
    20      return x&(-x);    
    21 }
    22 
    23 int sum(int x)
    24 {
    25     int ret=0;
    26     while (x>0)
    27     {
    28         ret=ret+c[x];
    29         x=x-lowbit(x);
    30     }
    31     return ret;
    32 }
    33 
    34 void add(int x,int d)
    35 {
    36     while (x<=n)
    37     {
    38         c[x]=c[x]+d;
    39         x=x+lowbit(x);
    40     }
    41 }
    42 
    43 int main()
    44 {
    45     memset(c,0,sizeof(c));
    46     
    47     
    48         
    49     return 0;
    50 }
  • 相关阅读:
    php1
    c# out参数
    c#冒泡算法
    c#方法 最大值我最小值
    方法
    OUT参数
    芮年
    PHP博客
    数组习题
    从郑和下西洋 到华人爱燕窝
  • 原文地址:https://www.cnblogs.com/pdev/p/4175973.html
Copyright © 2011-2022 走看看