例题当模板系列---树状数组

敌兵布阵:
单点更新,区间查询

const int shuzu=50000+10;

int tree[shuzu];
int n;
char s[10];
void add(int x,int j)
{
    while(x<=n)
    {
        tree[x]+=j;
        x+=x&-x;
    }
}
int Query(int x)
{
    int sum = 0;
    while(x)
    {
        sum+=tree[x];
        x-=x&-x;
    }
    return sum;
}
int main()
{
    int T;
    cin>>T;
    for(int t=1;t<=T;t++)
    {
        memset(tree,0,sizeof(tree));
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            int a;scanf("%d",&a);
            add(i,a);
        }
        printf("Case %d:\n",t);
        while(1)
        {
            int a,b;
            scanf("%s",s);if(strcmp(s,"End")==0) break;
            scanf("%d %d",&a,&b);

            if(strcmp(s,"Add")==0)
                add(a,b);
            else
                if(strcmp(s,"Sub")==0)
                    add(a,-b);
                else
                    if(strcmp(s,"Query")==0)
                    {
                        //printf("Query(b)=%d\n",Query(b));
                        //printf("Query(a-1)=%d\n",Query(a-1));
                        cout << Query(b)-Query(a-1) << endl;
                    }

        }
    }
}

洛谷:P3374 【模板】树状数组 1
单点更新,区间查询

const int shuzu=500000+10;

int tree[shuzu];
int n,m;
void add(int x,int j)
{
    while(x<=n)
    {
        tree[x]+=j;
        x+=x&-x;
    }
}
int Query(int x)
{
    int sum = 0;
    while(x)
    {
        sum+=tree[x];
        x-=x&-x;
    }
    return sum;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        int a;
        cin>>a;
        add(i,a);
    }
    for(int i=1;i<=m;i++)
    {
        int ope,a,b;
        cin>>ope>>a>>b;
        if(ope==1)
        {
            add(a,b);
        }
        else if(ope==2)
        {
            cout << Query(b)-Query(a-1) << endl;
        }
    }
    return 0;
}

P3368 【模板】树状数组 2:
区间更新,单点查询

const int shuzu=500000+10;

int a[shuzu];
int tree[shuzu];
int n,m;
void add(int x,int j)
{
    while(x<=n)
    {
        tree[x]+=j;
        x+=x&-x;
    }
}
int Query(int x)
{
    int sum = 0;
    while(x)
    {
        sum+=tree[x];
        x-=x&-x;
    }
    return sum;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        add(i,a[i]-a[i-1]);
    }
    for(int i=1;i<=m;i++)
    {
        int ope,l,r,j;
        cin>>ope;
        if(ope==1)
        {
            cin>>l>>r>>j;
            add(l,j);
            add(r+1,-j);
        }
        else if(ope==2)
        {
            cin>>l;
            cout << Query(l) << endl;
        }
    }
    return 0;
}

A Simple Problem with Integers POJ - 3468
区间更新,区间查询
此题推荐scanf(),printf()
cin,cout,TLE了

const int shuzu=500000+10;

ll a[shuzu];
ll d[shuzu];
ll nd[shuzu];
ll n,m;

void add(ll i,ll j)
{
    ll x = i;
    while(i<=n)
    {
        d[i]+=j;
        nd[i]+=j*(x-1);
        i+=i&-i;
    }
}
ll query(ll i)
{
    ll sum=0,x=i;
    while(i)
    {
        sum+=x*d[i]-nd[i];
        i-=i&-i;
    }
    return sum;
}

int main()
{
    scanf("%lld %lld",&n,&m);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        add(i,a[i]-a[i-1]);
    }
    for(ll i=1;i<=m;i++)
    {
        char ope;
        ll l,r,j;
        
        cin>>ope;
        if(ope=='C')
        {
            scanf("%lld %lld %lld",&l,&r,&j);
            add(l,j);
            add(r+1,-j);
        }
        else if(ope=='Q')
        {
            scanf("%lld %lld",&l,&r);
            printf("%lld\n",query(r)-query(l-1));
        }
    }
    return 0;
}

大佬的讲解:https://www.cnblogs.com/xenny/p/9739600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值