HDU 1166 敌兵布阵 (树状数组--单点更新,区间求值)

来源:互联网 发布:淘宝几十块的近视眼镜 编辑:程序博客网 时间:2024/06/10 12:11

OJ题目 : click here ~~~

中文的,大概题意就不说了。树状数组的水题。

忘记清空数组,导致WA,真可恨啊~~~~~~~哭

AC_CODE

int n;int num[50002];int lowbit(int x){    return x&(-x);}int sum(int x){    int ret = 0;    while(x > 0)    {        ret += num[x];        x -= lowbit(x);    }    return ret;}void add(int x , int d){    while(x <= n)    {        num[x] += d;        x += lowbit(x);    }}int main(){    int Case;    cin >> Case;    for(int cas = 1;cas <= Case;cas++)    {        printf("Case %d:\n",cas);        memset(num , 0 , sizeof(num));//为什么这个总是会忘记。。        scanf("%d",&n);        int i , j , a , b;        for(i = 1;i <= n;i++)        {            scanf("%d",&a);            add(i , a);        }        string command;        while(cin >> command)        {            if(command == "Query")            {   scanf("%d%d",&a,&b);                printf("%d\n",sum(b) - sum(a - 1));            }            else if(command == "Add")            {                scanf("%d%d",&a,&b);                add(a , b);            }            else if(command == "Sub")            {                scanf("%d%d",&a,&b);                add(a , -b);            }            else if(command == "End")                break;                    }    }    return 0;}


0 0
原创粉丝点击