本文主要是介绍HDU 1166 敌兵布阵 (树状数组--单点更新,区间求值),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;
}
这篇关于HDU 1166 敌兵布阵 (树状数组--单点更新,区间求值)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!