敌兵布阵---hud1166(线段树或者树状数组模板)
2015-07-30 18:27
381 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
线段树中对某一点的值进行改变;
树状数组
线段树中对某一点的值进行改变;
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<iostream> using namespace std; #define N 50500 #define met(a, b) memset(a, b, sizeof(a)) int Tree , n; int lowbit(int x) { return x&(-x); } void Update(int pos, int num) { while(pos<=n) { Tree[pos]+=num; pos += lowbit(pos); } } int GetSum(int pos) { int s=0; while(pos) { s += Tree[pos]; pos -= lowbit(pos); } return s; } int main() { int T, t=1; scanf("%d", &T); while(T--) { met(Tree, 0); int num, pos, x, y; scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%d", &num); Update(i, num); } printf("Case %d:\n", t++); char s[110]; while(scanf("%s", s), s[0]!='E') { if(s[0]=='A') { scanf("%d %d", &pos, &num); Update(pos, num); } if(s[0]=='S') { scanf("%d %d", &pos, &num); Update(pos, -num); } if(s[0]=='Q') { scanf("%d %d", &x, &y); int ans = GetSum(y)-GetSum(x-1); printf("%d\n", ans); } } } return 0; }
树状数组
相关文章推荐
- 程序设计题目参考
- Sublime Text 3 学习及使用
- zone_watermark_ok()的理解
- 一个简单的Android图片处理Demo
- Ubuntu镜像源和pypi镜像源
- 十五.电商鸡巴,最年轻的也能生存
- js中bind,call,apply方法的应用
- character_set_database和character_set_server
- 泛型委托Func
- Android工程项目没有错却出现红叉—解决办法
- sudo: sorry, you must have a tty to run sudo
- Axis2学习的第一天
- 求两个数的最大公约数(辗转相除法)
- 泛型委托
- 脚本实现exe的运行过程中参数的输入
- 开发常用软件
- 【linux高级程序设计】(第十二章)Linux多线程编程 2
- MYSQL/REDIS/MONGODB 服务器间数据迁移
- Linux系统下HTML5使用SQLite后数据库文件的存放位置
- 宏定义和函数的区别