HDU 1166 线段树的单点更新 区间求和
2015-06-02 23:07
295 查看
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define N 50005 using namespace std; int num ; struct Tree { int l; int r; int sum; }tree[N * 4]; void build(int root,int l,int r)///root为节点 此处为根节点 1 l,r为线段的左端点和右端点 { tree[root].l = l; tree[root].r = r; if(tree[root].l == tree[root].r) { tree[root].sum = num[l]; return ; } int mid = (l + r) / 2; build(root << 1,l ,mid); build(root << 1|1,mid+1,r); tree[root].sum = tree[root << 1].sum + tree[root << 1|1].sum;///此处灵活应用根据题意不同 } void update(int root,int pos,int val)///感觉此处类似回溯 先找到最低层 更新所要求的值然后一层一层的递归上来 { if(tree[root].l == tree[root].r)///找到最底层的数值 更新 { tree[root].sum = val; return ; } int mid=(tree[root].l + tree[root].r) / 2; if(pos <= mid) update(root << 1,pos,val); else update(root << 1|1,pos,val); tree[root].sum = tree[root << 1].sum + tree[root << 1|1].sum;///更新 } int query(int root,int L,int R)///求和逐层递归就好 { if(L <= tree[root].l && R >= tree[root].r) return tree[root].sum; int mid = (tree[root].l + tree[root].r) / 2,ret = 0; if(L <= mid ) ret += query(root << 1,L,R); if(R > mid) ret += query(root << 1|1,L,R); return ret; } int main() { int ca,cas = 1,n,Q,a,b; char str[10]; scanf("%d",&ca); while(ca--) { scanf("%d",&n); for(int i = 1;i <= n;i ++) scanf("%d",&num[i]); build(1,1,N); printf("Case %d:\n",cas++); while(scanf("%s",str),strcmp(str,"End")) { scanf("%d%d",&a,&b); if(strcmp(str,"Query") == 0) { if(a > b) swap(a , b); printf("%d\n",query(1,a,b)); } else if(strcmp(str,"Add") == 0) { num[a] = num[a] + b; update(1,a,num[a]); } else if(strcmp(str,"Sub") == 0) { num[a] = num[a] - b; update(1,a,num[a]); } } } return 0; }
相关文章推荐
- TortoiseSVN客户端重新设置用户名和密码
- 序列化
- Struts2原理图
- 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面。
- 请开启您PHP环境的openssl
- 欢迎使用CSDN-markdown编辑器
- HTML5 localStorage本地存储实际应用举例
- HTML5学习笔记 视频
- 课程改进建议
- Ubuntu 系统设置中不见了语言支持这个图标
- Ubuntu14.04建立WiFi热点
- HDOJ 1066 题解
- tcp 粘包
- jQuery css() 方法
- [LeetCode] Next Permutation
- 企业级负载平衡简介
- 缓存穿透与缓存雪崩
- PipedInputStream管道输出流和PipedOutputStream管道输入流
- flatbuffers 在cocos2dx中的应用
- jinfo命令使用