树状数组

原文链接:https://www.cnblogs.com/xenny/p/9739600.html

这篇写的很详细
HDU1166

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#include<cmath>
#define mem(a,b) memset(a,b,sizeof(a))
const int Max=5e4+10;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int mod=1e7+10;
int m,n;
int a[Max],c[Max];
int lowbit(int i)
{
    return i&(-i);
}
void update(int pos,int val)
{
    for(int i=pos; i<=n; i+=lowbit(i))
        c[i]+=val;
}
int getsum(int pos) //用前缀和 1-pos
{
    int ans=0;
    for(int i=pos; i>0; i-=lowbit(i))
        ans+=c[i];
    return ans;
}
int main()
{
    scanf("%d",&m);
    int k=0;
    while(m--)
    {
        scanf("%d",&n);
        mem(a,0);
        mem(c,0);
        printf("Case %d:\n",++k);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            update(i,a[i]);
        }
        char s[20]={0};
        int x,y;
        while(scanf("%s",s)&&s[0]!='E')
        {
            scanf("%d%d",&x,&y);
            if(s[0]=='Q')
            {
                printf("%d\n",getsum(y)-getsum(x-1));
            }
            else if(s[0]=='A')
                update(x,y);
            else if(s[0]=='S')
                update(x,-y);
        }
    }
    return 0;
}

上一篇:(原)pytorch中使用TensorRT


下一篇:3518EV300修改内存分配(linux和MMZ)