树状数组学习

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34271269/article/details/52594842

<span style="background-color: rgb(255, 255, 255);">#include <iostream>
#include <algorithm>
#define maxn 50000+50
using namespace std;
int c[maxn],a[maxn];
int n;
int lowbit(int x)
{return x&(-x);}
int sum(int r)
{
    int all=0;
    while(r)
    {
        all+=c[r];
        r-=lowbit(r);
    }
    return all;
}
void update(int pos,int inc)
{
    a[pos]+=inc;
    while(pos<=n)
    {
        c[pos]+=inc;
        pos+=lowbit(pos);
    }
}
void read()
{
    for(int i=1;i<=n;i++)
    {
        int x;
        update(i,x);
    }
    return;
}

</span>

一直不会树状数组 今天学习了一波 发现真的好简单啊。。。。。 很好写 常数又小 真的妙啊

猜你喜欢

转载自blog.csdn.net/qq_34271269/article/details/52594842
今日推荐