树状数组

来源:互联网 发布:c语言心型示爱代码 编辑:程序博客网 时间:2024/05/02 01:51

ACM模版

一维

/* *  INIT: ar[]置为0; *  CALL: add(i, v): 将i点的值加v; sum(i): 求[1, i]的和; */const int N = 1010;#define typev int   //  type of restypev ar[N];        //  index: 1 ~ Nint lowb(int t){    return t & (-t);}void add(int i, typev v){    for ( ; i < N; ar[i] += v, i += lowb(i));    return ;}typev sum(int i){    typev s = 0;    for (; i > 0; s += ar[i], i -= lowb(i));    return s;}

二维

/* *  INIT: c[][]置为0; Row,Col要赋初值  */const int N = 10000;int c[N][N];int Row, Col;inline int Lowbit(const int &x){   //  x > 0    return x & (-x);}int Sum(int i, int j){    int tempj, sum = 0;    while (i > 0)    {        tempj = j;        while (tempj > 0)        {            sum += c[i][tempj];            tempj -= Lowbit(tempj);        }        i -= Lowbit(i);    }    return sum;}void Update(int i, int j, int num){    int tempj;    while (i <= Row)    {        tempj = j;        while (tempj <= Col)        {            c[i][tempj] += num;            tempj += Lowbit(tempj);        }        i += Lowbit(i);    }    return ;}
0 0
原创粉丝点击