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 ...
随机推荐
- Git tricks: Unstaging files
NOTE: Following content is directly reprinted from http://andrewberls.com/blog/post/git-tricks-unsta ...
- [Guava源码分析] Preconditions 前置条件
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3874170.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- python 实现梯度下降
在多元线性回归中会用到梯度下降来计算参数值.这里我用python实现一个梯度下降版本. 这里多元线性方程为 y = A0+A1*x1+...+An* xn 数据输入格式,y表示 y \t x1 \t ...
- iOS程序的生命周期
任何程序的生命周期都是指程序加载到程序结束这一段时间. 在iOS应用程序中,通过单击主页面上的图标的方式可以启动一个程序.单击后,系统会显示一个过渡界面,然后调用main()函数来加载程序.从这一刻开 ...
- 转:JAVA中this用法小结
转:http://blog.sina.com.cn/s/blog_6a6badc90100t8hm.html#SinaEditor_Temp_FontName Java关键字this只能用于方法方法体 ...
- select标签用法
<select name="type" class="textarea" onchange='bbbb(this.value)' > <opt ...
- 安装360后,visual studio 经常报各种莫名其妙的错误的解决方案
安装360后,visual studio 经常报各种莫名其妙的错误,每次都要查找错误的解决方案 而且网上关于这个的好少,以后只要碰到了这种情况我就记录下吧 今天碰到的情况是打开WCF服务时出现 ...
- 【转载】Powershell设置世纪互联Office365嵌套组发送权限
Start-Transcript ".\Set-GroupSendPermisionLog.txt" -Force function Get-DLMemberRecurse { $ ...
- Basic Operation about Linux
1. 永久开启/关闭防火墙 在linux中防火墙是一个名叫iptables的工具 开启: chkconfig iptables on 关闭: chkconfig iptables off 即时生效,重 ...
- 【Go】 http webserver
示例1: package main import ( "fmt" "net/http" "encoding/json" ) var i in ...