是一道树状数组的裸题,也可以说是线段树的对于单点维护的裸题。多做这种题目可以提高自己对基础知识的理解程度,很经典。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <string>
using namespace std; const int N = ;
int C[N]; struct peo {
int x, v;
}P[N]; int lowbit (int x) {
return x & -x;
} int sum (int x) {
int ret = ;
while (x > ) {
ret += C[x]; x -= lowbit(x);
}
return ret;
} void add (int x, int d) {
while (x <= ) {
C[x] += d; x += lowbit(x);
}
} int _sum (int d, int u) {
return sum(u) - sum(d - );
} int main () {
int T, n, cur = ; scanf("%d", &T);
char op[];
while (T --) {
memset(C, , sizeof(C));
memset(P, , sizeof(P));
scanf("%d", &n);
for (int i = ; i < n; ++ i) {
scanf("%d", &P[i].v);
P[i].x = i + ;
add (P[i].x, P[i].v);
}
//cout << sum(3) << endl;
printf("Case %d:\n", ++ cur);
while (scanf("%s", op)) {
if (op[] == 'E') {
break;
} else if (op[] == 'Q') {
int x, y;
scanf("%d %d", &x, &y);
cout << _sum(x, y) << endl;
} else if (op[] == 'S') {
int x, y;
scanf("%d %d", &x, &y);
add (x, -y);
} else if (op[] == 'A') {
int x, y;
scanf("%d %d", &x, &y);
add (x, y);
}
}
}
return ;
}
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <numeric> using namespace std; struct P{
int sum, l, r;
} node[]; int n, num[] = {}; void build (int k, int l, int r) {
node[k].l = l, node[k].r = r;
if (l == r) {
node[k].sum = num[l];
} else {
build( * k, l, (l + r) / );
build( * k + , ((l + r) / ) + , r);
node[k].sum = node[ * k].sum + node[ * k + ].sum;
}
} void update(int k, int i, int x) {
if (node[k].l <= i && node[k].r >= i) {
node[k].sum += x;
}
if (node[k].l == node[k].r) {
return ;
}
if (i <= node[ * k].r) {
update( * k, i, x);
}
else update( * k + , i, x);
} int query (int k, int l, int r) {
if (l == node[k].l && r == node[k].r) {
return node[k].sum;
}
if (r <= node[ * k].r) {
return query( * k, l, r);
}
if (l >= node[ * k + ].l) {
return query( * k + , l, r);
} else {
return query( * k, l, node[ * k].r) + query( * k + , node[ * k + ].l, r);
}
} int main(){
int T;
scanf("%d",&T);
for (int z = ; z <= T; ++ z) {
memset(num, , sizeof(num));
scanf("%d", &n);
for (int i = ; i <= n; ++ i) {
scanf("%d",&num[i]);
}
build(, , n);
char s[];
printf("Case %d:\n",z);
while (scanf("%s", s)){
if (s[] == 'E') break;
if (s[] == 'Q'){
int a, b;
scanf("%d%d" ,&a ,&b);
printf("%d\n",query(, a, b));
}
if (s[] == 'A'){
int a,x;
scanf("%d%d", &a, &x);
update(, a, x);
}
if (s[] == 'S') {
int a,x;
scanf("%d%d", &a, &x);
update(, a, -x);
}
}
}
return ;
}

【HDU1166】敌兵布阵(树状数组或线段树)的更多相关文章

  1. bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1384  Solved: 629[Submit][Stat ...

  2. [BZOJ 3196] 213平衡树 【线段树套set + 树状数组套线段树】

    题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过 ...

  3. [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】

    题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...

  4. POJ 1195 Mobile phones (二维树状数组或线段树)

    偶然发现这题还没A掉............速速解决了............. 树状数组和线段树比较下,线段树是在是太冗余了,以后能用树状数组还是尽量用......... #include < ...

  5. 【BZOJ3196】二逼平衡树(树状数组,线段树)

    [BZOJ3196]二逼平衡树(树状数组,线段树) 题面 BZOJ题面 题解 如果不存在区间修改操作: 搞一个权值线段树 区间第K大--->直接在线段树上二分 某个数第几大--->查询一下 ...

  6. BZOJ.4553.[HEOI2016&TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)

    题目链接:BZOJ 洛谷 \(O(n^2)\)DP很好写,对于当前的i从之前满足条件的j中选一个最大值,\(dp[i]=d[j]+1\) for(int j=1; j<i; ++j) if(a[ ...

  7. P3157 [CQOI2011]动态逆序对(树状数组套线段树)

    P3157 [CQOI2011]动态逆序对 树状数组套线段树 静态逆序对咋做?树状数组(别管归并QWQ) 然鹅动态的咋做? 我们考虑每次删除一个元素. 减去的就是与这个元素有关的逆序对数,介个可以预处 ...

  8. HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

    Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  9. BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树

    题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...

  10. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota ...

随机推荐

  1. [置顶] Oracle 11g R2 ASM:了解 Oracle ASM 基本概念

    About Oracle ASM Instances About Oracle ASM Disk Groups About Mirroring and Failure Groups About Ora ...

  2. JSON 基本语法

    JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...

  3. C语言--返回局部变量的地址

    我们可能会经常写出这样的代码: int  add(int  a , int  b) { return  a + b; } 当然,这是合理的写法,使函数的返回值为 int ,所以,调用函数后会返回一个i ...

  4. Git实现从本地加入项目到远程仓库

    Git是如今最流行的版本号控制系统之中的一个了,今天也试试了.成功了上传了远程仓库,接下来看看我是怎么做的. 1.首先,要有git的账号.点击查看怎么注冊? 2.注冊成功之后.登陆GitHub.然后, ...

  5. asp.net实现将网页内容输出到word并下载到本地

    个人觉得要实现这个功能如果没有类库提供的几个关键函数,还是比较繁琐的.所以首先介绍几个将要在代码中使用的关键函数和参数,然后再说函数实现.注意问题等. 关键函数: 1.函数原型:Response.Ap ...

  6. (转)Windows Server 2008 默认"照片库查看器" 无法打开图片, 只能用画图程序打开

    1.解决[启用Win2008照片查看器] Win2008 中放了一些图片,本来以为可以象Win7那样直接用“照片查看器”打开,可是Win2008默认竟然是用“画图”打开的,非常不方便. 再仔细一看,“ ...

  7. ADO.Net总结

    ADO.NET简介 一.    ADO.NET的组成(ADO.NET是什么?能干什么)客户体验      ADO.NET就是一组类库可以让我们通过程序的方式访问数据库 ADO.NET主要包括5个对象, ...

  8. 图文混排——用表情代替"[文字]"

    1.简单设置带属性的字符串 定义一个NSMutableAttributedString带属性的字符串 NSMutableAttributedString *str = [[NSMutableAttri ...

  9. C#三个平台上的文件选择方法

    wpf: Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = &qu ...

  10. hdu4497 正整数唯一分解定理应用

    C - (例题)整数分解,计数 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65535KB    ...