HDU 1166 (segment tree)
来源:互联网 发布:matlab调用python脚本 编辑:程序博客网 时间:2024/06/06 20:43
An easy problem aiming at practicing the implementation of the segment tree. Apparently, we should use segment tree to solve it. And within the tree structure is the sum.
Note that the number of the nodes is much larger than the size of the integer array because of the "tree" structure. Otherwise, it may cause Runtime Error (ACCESS_VIOLATION).
AC Code:
#include <iostream>#include <cstdio>#include <cstring>#define INF 0x3f3f3f3f#define N 50010#define MST(a,b) memset(a,b,sizeof(a))#define FOR(i,a,b) for(int i=a;i<=b;i++)using namespace std;struct TREE { int left,right; int suml;} tree[900000];//*** Large Enough? ***void Built(int root,int l,int r){ tree[root].left = l; tree[root].right = r; tree[root].suml=0; if(l == r)return; int mid = (l+r) >> 1; Built ( root<<1 , l , mid ); Built ( root<<1|1 , 1+mid , r);}void Fix(int root,int left,int right,int del) { if (left == tree[root].left && tree[root].right == right) { tree[root].suml += del; return; } if (left < tree[root].left || right > tree[root].right) return; Fix(root << 1, left, right, del); Fix(root << 1 | 1, left, right, del); tree[root].suml += del;}int Query_sum(int root,int left,int right) { if (left == tree[root].left && tree[root].right == right) return tree[root].suml; if (tree[root << 1 | 1].left > right)//left only return Query_sum(root << 1, left, right); if (left > tree[root << 1].right)//right only return Query_sum(root << 1 | 1, left, right); int s1 = 0, s2 = 0; s1 = Query_sum(root << 1, left, tree[root << 1].right); s2 = Query_sum(root << 1 | 1, tree[root << 1 | 1].left, right); return s1 + s2; if (left < tree[root].left || right > tree[root].right) return -INF;}int main(){ int t,n,a,b; cin>>t; FOR(ncase,1,t) { printf("Case %d:\n", ncase); cin >> n; Built(1, 1, n); FOR(i, 1, n) { cin >> a; Fix(1, i, i, a); } char s[10]; cin >> s; while (s[0] != 'E') { cin >> a >> b; if (s[0] == 'A') { Fix(1, a, a, b); } else if (s[0] == 'S') { Fix(1, a, a, -b); } else printf("%d\n", Query_sum(1, a, b)); cin>>s; } } return 0;}
0 0
- HDU 1166 (segment tree)
- HDU 1166 - 敌兵布阵 - Segment Tree
- HDU 1166 敌兵布阵 Segment Tree题解
- HDU 1754 - I Hate It - Segment Tree
- HDU 4107 Gangster Segment Tree线段树
- Revenge of Segment Tree (hdu 5086)
- hdu 5086 Revenge of Segment Tree
- HDU 5086 Revenge of Segment Tree
- hdu 5086 Revenge of Segment Tree(水题)
- HDU 5086 Revenge of Segment Tree
- HDU 1754 I Hate It (Splay tree和Segment tree)
- Segment tree
- Segment Tree
- Segment Tree
- segment Tree
- Segment Tree
- Segment Tree
- hdu 1166 敌兵布阵 单点更新+区间查询 segment tree or BIT
- Power of Three
- Move Zeroes
- 开始新的博客生活
- 【2008】 求数列最大累加和的子序列
- mybatis注解案例
- HDU 1166 (segment tree)
- Android自带抽屉布局中NavigationView的动态生成
- Libcurl的编译_HTTP/HTTPS客户端源码示例
- Decode Ways
- tomcat启动与Server.xml文件的解析
- mybatis日志输出
- 陶哲轩实分析-第18章 Lebesgue测度
- SparkML之回归(一)线性回归
- 【2037】利用字符串处理,输出x+y的结果