题目链接

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define mp make_pair
#define pi acos(-1)
#define pii pair<int, int>
#define pll pair<long long , long long>
#define ll long long
#define ld long double
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
#define lowbit(x) x&(-x)
const int inf = 0x3f3f3f3f;
const int maxn = 50001;
using namespace std;
int T, N, c[maxn];
char order[maxn];
void update(int x, int y)
{
for(int i = x; i <= N; i += lowbit(i))
c[i] += y;
}
int getsum(int x)
{
int ans = 0;
while(x)
{
ans += c[x];
x -= lowbit(x);
}
return ans;
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d", &T);
for(int t = 1; t <= T; t++)
{
scanf("%d", &N);
memset(c, 0, sizeof(c));
for(int i = 1; i <= N; i++)
{
int a;
scanf("%d", &a);
update(i, a);
}
printf("Case %d:\n", t);
while(scanf("%s", order) != EOF)
{
if(!strcmp(order, "End"))
break;
if(!strcmp(order, "Add"))
{
int i, j;
scanf("%d %d", &i, &j);
update(i, j);
}
else if(!strcmp(order, "Sub"))
{
int i, j;
scanf("%d %d", &i, &j);
update(i, -j);
}
else if(!strcmp(order, "Query"))
{
int i, j;
scanf("%d %d", &i, &j);
printf("%d\n", getsum(j) - getsum(i - 1));
}
}
} }

HDU-1166 敌兵布阵 (树状数组模板题——单点更新,区间求和)的更多相关文章

  1. HDU 1166 敌兵布阵 树状数组小结(更新)

    树状数组(Binary Indexed Tree(BIT), Fenwick Tree) 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有 元素之和,但是每次只能修改一 ...

  2. HDU 1166 敌兵布阵 树状数组||线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...

  3. HDU 1166 敌兵布阵(树状数组)

    之前用过了线段树的做法,树状数组的也补上吧 #include<iostream> #include<cstdio> #include<cstring> using ...

  4. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  5. hdu-1166 敌兵布阵---树状数组模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 维护动态的区间和,单点更新,就是模板题 #include<iostream& ...

  6. hdoj 1166 敌兵布阵(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...

  7. HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)

    题目链接 ac代码(注意字符读入前需要注意回车的影响) #include<iostream> #include<cstdio> #include<cstring> ...

  8. HDU1166 敌兵布阵(树状数组)

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...

  9. hdu1166 敌兵布阵 树状数组/线段树

    数列的单点修改.区间求和 树状数组或线段树入门题 #include<stdio.h> #include<string.h> ],N; void add(int x,int a) ...

随机推荐

  1. Spring注解开发系列Ⅰ--- 组件注册(上)

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  2. 1、通过CP数据文件的方式恢复MySQL 从库 启动后报错:Last_IO_Errno: 1236:A slave with the same server_uuid/server_id as this slave has connected to the master;

    1.问题: MySQL从库中查看主从状态: show slave status\G,发现出现IO的报错: Last_IO_Errno: Last_IO_Error: Got fatal error f ...

  3. javaIO编码详解

    原创 输出流 有哪些构造方法可以在参数上设置编码格式 PrintStream(File file, String csn) PrintStream(String fileName, String cs ...

  4. python学习--curl

    PyCurl是一个C语言写的libcurl的python绑定库.libcurl 是一个自由的,并且容易使用的用在客户端的 URL 传输库.它的功能很强大,PycURL 是一个非常快速(参考多并发操作) ...

  5. GitHub注册与Git安装

    注册GitHub GitHub官网 ☞☞☞☞[戳我] 一.打开,如下图所示, 按照要求进行注册. 安装Git Git官网下载☞☞☞☞ [戳我] 一.打开,根据自己的电脑系统选择不同的版本,这里主要是以 ...

  6. Jenkins report 打开样式失败解决方案

    1.临时解决方案(重启Jenkins失效):在系统管理->脚本命令行: 执行: System.setProperty("hudson.model.DirectoryBrowserSup ...

  7. sqlserver install on linux chapter two

    The previous chapter is tell us how to install sqlerver on linuix Today, we will see how to make it ...

  8. javascript Math对象 常用数字操作方法

    var t='1.2'; parseInt(t) parseFloat(t)//1.2 Number(1.2)//1.2强制转换为数字 2.向上取整,有小数就整数部分加1 js: Math.ceil( ...

  9. k8s系列---故障

    kubectl get namespace prom Terminating 45h namespace 出现Terminating 状态,一直删不掉 解决: 1:导出namespace的json文件 ...

  10. centos6.x下使用xinetd管理rsync服务

    系统环境说明:centos6.x,centos7.x下rsync直接可由systemd管理(无需使用xinetd). [root@meinv01 ~]# rpm -qa|grep xinetd [ro ...