HDOJ 1166 敌兵布阵 (线段树)
2012-04-07 19:41
323 查看
题目链接:~( ̄▽ ̄~)(~ ̄▽ ̄)~
哎~ 不会树状数组郁闷........╮(╯Д╰)╭
code:
哎~ 不会树状数组郁闷........╮(╯Д╰)╭
code:
#include <stdio.h> typedef struct { int l, r, count; }node; node tree[4*50005]; void Init(int root, int left, int right) { int m = (left+right)/2; if(left == right)//叶节点为单个的数 { tree[root].l = tree[root].r = left; scanf("%d",&tree[root].count); return ; } tree[root].l = left; tree[root].r = right; Init(2*root, left, m); Init(2*root+1, m+1, right); tree[root].count = tree[2*root].count+tree[2*root+1].count; } void modify(int root, int a, int b)//a为第几个营地, b为要修改的数 { int m = (tree[root].l+tree[root].r)/2; if(tree[root].l == tree[root].r && tree[root].l == a)//找到叶节点 { tree[root].count += b; return ; } if(a>m) modify(2*root+1, a, b); else modify(2*root, a, b); tree[root].count = tree[2*root].count+tree[2*root+1].count; } int search(int root, int left, int right) { int l = tree[root].l ,r = tree[root].r, m = (tree[root].l+tree[root].r)/2, sum = 0; if(left<=l && right>=r)//当前区间属于查询区间 return sum += tree[root].count; if(left>m)//查询区间属于树的右区间 return sum = search(2*root+1, left, right); else if(right<=m)//查询区见属于树的左区间 return sum = search(2*root, left, right); else return sum = search(2*root+1, left, right)+search(2*root, left, right); } int main() { int t = 0, n = 0, count = 0, a = 0, b = 0; char ch[10]; scanf("%d",&t); while(t--) { scanf("%d",&n); Init(1, 1, n); printf("Case %d:\n", ++count); while(1) { scanf("%s",ch); if(ch[0] == 'E') break; if(ch[0] == 'A') { scanf("%d %d",&a,&b); modify(1,a,b); } else if(ch[0] == 'S') { scanf("%d %d",&a,&b); modify(1,a,-b); } else { scanf("%d %d",&a,&b); printf("%d\n",search(1,a,b)); } } } return 0; }
相关文章推荐
- HDOJ 1166 敌兵布阵(线段树,裸)
- 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵
- [HDU]HDOJ1166 敌兵布阵 基础线段树
- hdoj--1166--敌兵布阵(线段树)
- hdoj 1166 敌兵布阵(线段树||树状数组)
- hdoj 1166 敌兵布阵【线段树求区间最大值+单点更新】
- HDOJ 题目1166 敌兵布阵(线段树单点更新)
- HDOJ 题目 1166 敌兵布阵(线段树,模板)
- hdoj 1166 敌兵布阵 【单点线段树】
- HDOJ 1166 敌兵布阵-------------树状数组 线段树
- HDOJ 1166 敌兵布阵 线段树: 单点更新 成段求和
- HDOJ 1166 敌兵布阵(线段树)
- 线段树——HDOJ 1166 敌兵布阵 解题报告
- hdoj 1166 敌兵布阵(线段树)
- hdoj 敌兵布阵 1166 (线段树 模板)
- HDOJ 1166 敌兵布阵 第一次用线段树AC题目
- hdoj 1166 敌兵布阵 【单点更新+区间求和】 【线段树】 【树状数组】
- hdoj1754-I Hate It && 1166-敌兵布阵(线段树)
- [HDOJ] problem_1166 【敌兵布阵】 第一道线段树 纪念一下
- HDOJ 1166 敌兵布阵(线段树基本操作)