hdu 1166 树状数组 线段树入门
点修改 区间求和
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 200010
#define INF 0x7fffffff
#define inf 10000000
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
using namespace std; int n, m, maxv[maxn*2], a[maxn], p, v, ql, qr; void build(int id, int L, int R)
{
if(L == R) maxv[id] = a[L];
else
{
int M = L + (R-L)/2;
build(id*2, L, M);
build(id*2+1, M+1, R);
maxv[id] = maxv[id] + maxv[id*2] + maxv[id*2+1];
}
} void update(int id, int L, int R)
{
if(L == R) maxv[id] += v;
else
{
int M = L + (R-L)/2;
if(p <= M) update(id*2, L, M);
else update(id*2+1, M+1, R);
maxv[id] += v;
}
} int query(int id, int L, int R)
{
int M = L + (R-L)/2, ans = 0;
if(ql <= L && qr >= R) return maxv[id];
if(ql <= M) ans += query(id*2, L, M);
if(qr > M) ans += query(id*2+1, M+1, R);
return ans;
} int main()
{
//freopen("in.txt", "r", stdin);
int t, ca = 0;
scanf("%d", &t);
while(t --)
{
printf("Case %d:\n", ++ ca);
scanf("%d", &n);
memset(maxv, 0, sizeof(maxv));
for(int i = 1; i <= n; ++ i) scanf("%d", &a[i]);
build(1, 1, n);
char c[10];
while(true)
{
scanf("%s", c);
if(c[0] == 'Q')
{
scanf("%d%d", &ql, &qr);
printf("%d\n", query(1, 1, n));
}
else if(c[0] == 'A')
{
scanf("%d%d", &p, &v);
update(1, 1, n);
}
else if(c[0] == 'S')
{
scanf("%d%d", &p, &v);
v = -v;
update(1, 1, n);
}
else break;
}
}
return 0;
}
hdu 1166 树状数组 线段树入门的更多相关文章
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- hdu 1166 敌兵布阵——(区间和)树状数组/线段树
pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...
- HDU 1166 敌兵布阵 树状数组||线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...
- hdu 1166 树状数组(线段树)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- HDU 3303 Harmony Forever 前缀和+树状数组||线段树
Problem Description We believe that every inhabitant of this universe eventually will find a way to ...
随机推荐
- PropertyGrid排序
解决PropertyGrid对自定义属性排序的问题. 参考了:http://www.cnblogs.com/greatverve/archive/2012/02/08/propergrid-order ...
- (转)hessian源码分析(一)------架构
在计费中心的对外交互这块采用了hessian,有必要对hessian的运行机理和源码做一定的解析. 大致翻了翻源码后,发现hessian的主要结构分客户端与服务端,中间基于http传输.客户端主要做的 ...
- ios固定高度禁止惯性滚动
最近测试pad改H5的项目时,固定高度的div,超出部分滚动,但在ios下滑动特别卡顿,安卓上没问题.搜索找到解决办法 固定高度的div设置超出页面滚动,ios会出现卡顿,非常不爽.通过下面css就可 ...
- iOS 非ARC基本内存管理系列总结6 -设计微博模型
设计简单的微博模型:用User类和Status类来模拟实现 在非ARC机制下有两种方式,两者没有太大的区别之所以写了两种只是为了方便学习和对比两种写法! 第一种:没有使用atuorelease和自动释 ...
- Uncaught SyntaxError: Unexpected token ILLEGAL【js错误】
应该是逗号的中英文状态错了,应该是英文状态的逗号.还有百度应用后面的逗号.college后面的冒号
- CMD规范的函数与普通函数间调用
/* * a.js * 普通的非cmd规范的js文件 */ function fun1(){ console.log("fun1"); //调用seajs模块中的fun1 seaj ...
- struct和class区别
转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...
- Oracle11g数据库安装
一.安装流程截图(没截图部分都默认) 服务器如果不是单独的数据库服务器,内存分配大概1/4即可. 二.安装后,表空间,以及创建账号脚本 sqlplus / as sysdba 登陆数据库,执行 /*第 ...
- UVA10361 -自动作诗机
UVA10361 - Automatic Poetry(自动作诗机) A Schuttelreim seems to be a typical German invention. The funny ...
- 选项切换条--第三方开源--SHSegmentControl
SHSegmentControl在github上的项目主页地址:https://github.com/7heaven/SHSegmentControl SHSegmentControl使用简单,在xm ...