hdu-1166 敌兵布阵---树状数组模板
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1166
题目大意:
维护动态的区间和,单点更新,就是模板题
#include<iostream>
#include<algorithm>
#include<set>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = + ;
typedef long long ll;
int tree[maxn];
int n;
int lowbit(int x)
{
return x & (-x);
}
void add(int x, int d)
{
while(x <= n)
{
tree[x] += d;
x += lowbit(x);
}
}
ll sum(int x)
{
ll ret = ;
while(x > )//此处等于0会导致无限循环
{
ret += tree[x];
x -= lowbit(x);
}
return ret;
}
int main()
{
int T, cases = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
memset(tree, , sizeof(tree));
int x, y;
for(int i = ; i <= n; i++)
{
scanf("%d", &x);
add(i, x);
}
char s[];
printf("Case %d:\n", ++cases);
while(scanf("%s", s) && s[] != 'E')
{
scanf("%d%d", &x, &y);
if(s[] == 'A')
{
add(x, y);
}
else if(s[] == 'S')
{
add(x, -y);
}
else if(s[] == 'Q')
{
ll ans = sum(y) - sum(x - );
printf("%lld\n", ans);
}
}
}
}
hdu-1166 敌兵布阵---树状数组模板的更多相关文章
- HDU 1166 敌兵布阵 树状数组小结(更新)
树状数组(Binary Indexed Tree(BIT), Fenwick Tree) 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有 元素之和,但是每次只能修改一 ...
- 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 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- hdoj 1166 敌兵布阵(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...
- HDU-1166 敌兵布阵 (树状数组模板题——单点更新,区间求和)
题目链接 AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- HDU1166 敌兵布阵(树状数组)
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- hdu1166 敌兵布阵 树状数组/线段树
数列的单点修改.区间求和 树状数组或线段树入门题 #include<stdio.h> #include<string.h> ],N; void add(int x,int a) ...
- 【线段树I:母题】hdu 1166 敌兵布阵
[线段树I:母题]hdu 1166 敌兵布阵 题目链接:hdu 1166 敌兵布阵 题目大意 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又開始忙乎了.A国在海 ...
随机推荐
- 不重新编译安装php模块的方法
如果你有下面两种经历: 如果php通过源码安装(php7),如果后来需要开启某个自带模块(例如ldap,snmp等),通常需要重新编译. 另外一些安装php模块的经历,例如redis,swoole,y ...
- 强联通分量之kosaraju算法
首先定义:强联通分量是有向图G=(V, E)的最大结点集合,满足该集合中的任意一对结点v和u,路径vu和uv同时存在. kosaraju算法用来寻找强联通分量.对于图G,它首先随便找个结点dfs,求出 ...
- Cogs 2856. [洛谷U14475]部落冲突
2856. [洛谷U14475]部落冲突 ★★★ 输入文件:lct.in 输出文件:lct.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 在一个叫做Travi ...
- 清北刷题冲刺 10-31 a.m
集合 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ], ...
- Start and Stop Bitbucket Server
Starting and stopping Bitbucket Server This page describes the various ways you can start or stop Bi ...
- JavaScript专题(二)闭包
前言 - ES6 之前,JS没有块级作用域,只有全局作用域和函数作用域 用了许久ES6,春招在即,重写下博文. 还是讲讲闭包.我们要知其然,知其所以然. 仿佛大众情人一般,很多前端面试官都会问一问,说 ...
- Java面向对象_常用类库api
StringBuffer 例: public class StringBufferDemo { /** * @param args */ public static void main(String[ ...
- windows用一键安装包安装(推荐)
为了简化大家在windows下面的安装,我们在xampp基础上做了禅道的windows一键安装包.xampp是业内非常著名的AMP集成运行环境.禅道的一键安装包主要在它基础上做了大量的精简,并集成了我 ...
- MVC中 Remote的用法
一.web.config加入 <appSettings> <add key="ClientValidationEnabled" value="t ...
- SpringBoot2.0之三 优雅整合Spring Data JPA
在我们的实际开发的过程中,无论多复杂的业务逻辑到达持久层都回归到了"增删改查"的基本操作,可能会存在关联多张表的复杂sql,但是对于单表的"增删改查"也是不 ...