HDU 1166 敌兵布阵

来源:互联网 发布:相场模拟软件 编辑:程序博客网 时间:2024/05/16 09:45

Tree Array.

The portal:http://acm.hdu.edu.cn/showproblem.php?pid=1166

#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;const int M = 50005;int Bit[M << 1] ;void Insert(int x,int value) {for(int i = x ; i <= M ; i += i & (-i)) {Bit[i] += value;}}int Query(int x){int ret = 0;if(x == 0) return 0;for(int i = x ; i ; i -= i & (-i)){ret += Bit[i]; }return ret;}void Deal_with(){int T,n,t=1;scanf("%d",&T);while(T--) {printf("Case %d:\n",t++);memset(Bit,0,sizeof(Bit));scanf("%d",&n);int tempx,tempvalue,tempa,Left,Right;char temps[10];for(int i=1; i<=n; i++) {scanf("%d",&tempa);Insert(i,tempa);}while(1) {scanf("%s",temps);if(strcmp(temps,"End") == 0) break;if(strcmp(temps,"Add") == 0) {scanf("%d %d",&tempx,&tempvalue);Insert(tempx,tempvalue);}if(strcmp(temps,"Sub") == 0) {scanf("%d %d",&tempx,&tempvalue);Insert(tempx,-tempvalue);}if(strcmp(temps,"Query") == 0) {scanf("%d %d",&Left,&Right);printf("%d\n",Query(Right) - Query(Left-1));}}}}int main(void){//freopen("a.in","r",stdin);Deal_with();return 0;}
Segment Tree:

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <iostream>const int MAXN = 50005;struct Node {int value;int Left;int Right;}Segment_Tree[MAXN<<2];int a[MAXN];int cnt ;void Build_Tree(int Left,int Right,int Now) {Segment_Tree[Now].Left = Left;Segment_Tree[Now].Right = Right;if(Left == Right) {Segment_Tree[Now].value = a[cnt++];return ;}int Mid = (Left + Right) >> 1;Build_Tree(Left,Mid,Now<<1);Build_Tree(Mid+1,Right,Now<<1|1);Segment_Tree[Now].value = Segment_Tree[Now<<1].value + Segment_Tree[Now<<1|1].value;}void Update_Tree(int Left,int Right,int Now,int position,int add_value) {if(Left == Right) {Segment_Tree[Now].value += add_value;return;}int Mid = (Left + Right) >> 1;if(position > Mid) {Update_Tree(Mid+1,Right,Now<<1|1,position,add_value);}else {Update_Tree(Left,Mid,Now<<1,position,add_value);}Segment_Tree[Now].value = Segment_Tree[Now<<1].value + Segment_Tree[Now<<1|1].value;}int Query_Tree(int Left,int Right,int Now,int Left_Edge,int Right_Edge) {int Query_Ans = 0;if(Left >= Left_Edge && Right <= Right_Edge) {return Segment_Tree[Now].value;}int Mid = (Left + Right) >> 1;if(Mid >= Left_Edge) {Query_Ans += Query_Tree(Left,Mid,Now<<1,Left_Edge,Right_Edge);}if(Mid < Right_Edge) {Query_Ans += Query_Tree(Mid+1,Right,Now<<1|1,Left_Edge,Right_Edge);}return Query_Ans;}void Deal_with() {int n,T,t=1;scanf("%d",&T);while(T--) {printf("Case %d:\n",t++);scanf("%d",&n);char tempc[10];cnt = 1;for(int i = 1 ; i <= n ; i ++) {scanf("%d",a+i);}Build_Tree(1,n,1);int Left,Right,position,value;while(1) {scanf("%s",tempc);if(strcmp(tempc,"End") == 0) {break;}else if(strcmp(tempc,"Query") == 0) {scanf("%d %d",&Left,&Right);printf("%d\n",Query_Tree(1,n,1,Left,Right));}else if(strcmp(tempc,"Add") == 0) {scanf("%d %d",&position,&value);Update_Tree(1,n,1,position,value);}else {scanf("%d %d",&position,&value);Update_Tree(1,n,1,position,-value);}}}}int main(void) {//freopen("a.in","r",stdin);Deal_with();return 0;}


0 0
原创粉丝点击