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

 #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. 使用python监听、模拟鼠标键盘事件

    最近守望职业选手疑似开挂事件挺热闹的,在下小菜一枚,并不能从视频中看出端倪.看了一些关于外挂的讨论,自动点射和压枪只需在鼠标驱动上做些改动即可,自瞄或其他高级功能则需要读内存或修改游戏文件,检测也更容 ...

  2. PHP设计模式笔记四:适配器模式 -- Rango韩老师 http://www.imooc.com/learn/236

    适配器模式 1.适配器模式,可以将截然不同的函数接口封装成统一的API 2.实际应用举例,PHP的数据库操作有mysql.mysqli.pdo三种,可以用适配器模式统一成一致,类似的场景还有cache ...

  3. web前端代码规范——css代码规范

    Bootstrap CSS编码规范 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 为选择器分组时,将单独的选择器单独放在一行. 为了代码的易读性,在每个 ...

  4. LNMP、LAMP、LANMP一键安装脚本(定期更新)[转]

    这个脚本是使用shell编写,为了快速在生产环境上部署LNMP/LAMP/LANMP(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS/ ...

  5. [Node.js] Using ES6 and beyond with Node.js

    If you're used to using all the latest ES6+ hotness on the front end via Babel, working in Node.js c ...

  6. 程序员的绘图利器 — Gnuplot

      介绍 Gnuplot is a command-line program that can generate two- and three-dimensional plots. It is fre ...

  7. 常见HTTP状态码的含义

    200 请求已成功,请求所希望的响应头或数据体将随此响应返回. 301 被请求的资源已永久移动到新位置. 302 请求的资源现在临时从不同的 URI 响应请求. 400 1.语义有误,当前请求无法被服 ...

  8. C# KeyValuePair<TKey,TValue>与Container

    KeyValuePair<TKey,TValue>  KeyValuePair<TKey,TValue>是一个结构体,相当于C#一个Map类型的对象,可以通过它记录一个键/值对 ...

  9. QQ在线联系代码

    添加图文模块,标题地址:tencent://message/?uin=你的QQ号&Site=myqq&Menu=yes "你的QQ号"就写您自己的Q号 图片地址写: ...

  10. 安卓状态栏通知Status Bar Notification

    安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...