hdoj 1166 敌兵布阵(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
思路分析:该问题为动态连续和查询问题,使用数组数组可以解决;也可使用线段树解决该问题;
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = + ;
int c[MAX_N]; inline int Lowbit(int x) { return x & -x; }
inline int Sum(int x)
{
int ret = ;
while (x) {
ret += c[x];
x -= Lowbit(x);
}
return ret;
} inline void Add(int x, int d, int n)
{
while (x <= n) {
c[x] += d;
x += Lowbit(x);
}
} int main()
{
int a = , b = , ans = , temp = ;
int test_case = , case_id = , n = ;
char command[]; scanf("%d", &test_case);
while (test_case--) {
scanf("%d", &n);
memset(c, , sizeof(c));
for (int i = ; i <= n; ++ i) {
scanf("%d", &temp);
Add(i, temp, n);
}
printf("Case %d:\n", ++case_id);
while(scanf("%s", command) != EOF && command[] != 'E') {
scanf("%d %d", &a, &b);
if (command[] == 'Q') {
ans = Sum(b) - Sum(a - );
printf("%d\n", ans);
} else if (command[] == 'A') {
Add(a, b, n);
} else {
Add(a, -b, n);
}
}
}
return ;
}
hdoj 1166 敌兵布阵(树状数组)的更多相关文章
- HDU 1166 敌兵布阵 树状数组||线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...
- HDU 1166 敌兵布阵(树状数组)
之前用过了线段树的做法,树状数组的也补上吧 #include<iostream> #include<cstdio> #include<cstring> using ...
- HDU 1166 敌兵布阵 树状数组小结(更新)
树状数组(Binary Indexed Tree(BIT), Fenwick Tree) 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有 元素之和,但是每次只能修改一 ...
- HDU1166 敌兵布阵(树状数组)
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- hdu1166 敌兵布阵 树状数组/线段树
数列的单点修改.区间求和 树状数组或线段树入门题 #include<stdio.h> #include<string.h> ],N; void add(int x,int a) ...
- HDU 1166 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- hdu-1166 敌兵布阵---树状数组模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 维护动态的区间和,单点更新,就是模板题 #include<iostream& ...
- HDU-1166 敌兵布阵 (树状数组模板题——单点更新,区间求和)
题目链接 AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵
题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...
随机推荐
- ssh生成密钥(供git使用)
我们在使用git远程更新时候,需要设置好远程密钥,以使我们能够远程更新代码到代码库中.现在我们就来做一下这件事情(ssh模式下) ssh-keygen -t rsa -c “hcu5555@hotm ...
- SGU 101.Domino( 欧拉路径 )
求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...
- Hibernate学习之hibernate执行顺序
Hibernate 执行的顺序如下: (1) 生成一个事务的对象,并标记当前的 Session 处于事务状态(注:此时并未启动数据库级事务). (2) 应用使用 s.save 保存对象,这个时候 ...
- 64bits Python2.7.5安装numpy包
由于数值分析需要numpy计算包,我找了很多numpy-cp27的下载地址,下了最新版的.whl文件,但总是安装不成功,后来找到一个.exe文件 直接下载安装即可使用:下面是网址链接http://do ...
- ThinkPHP中的CURD操作
<?php //查询多条记录,返回二维数组 $result = M("admin")->select(); $result = M("admin") ...
- 尼康D5100使用设置及技巧,同样也适用尼康D5200
尼康D5100使用设置及技巧,同样也适用尼康D5200,希望对新手能有点帮助. 一.设置 1.优化校准:可以在menu菜单中找到它,一般使用"标准"就可以,建议将"标准& ...
- fafu 1100 线段树
题目链接 单点更新, 区间查询. 这题空间好小.... #include <iostream> #include <vector> #include <cstdio> ...
- 编程习题——Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- QListWidget的QComboBox下拉列表添加复选框及消息处理
要在QComboBox下拉列表项中添加复选框,并进行消息处理,在网上搜索了很久没有找到太多有用的信息和实际的例子,但从中还是找到了一些提示性的资料,根据这些简短的介绍,最终实现了这个功能. QComb ...
- HDU 5818 Joint Stacks(左偏树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5818 [题目大意] 给出两个栈A B(初始时为空),有三种操作: push.pop.merge. ...