【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)
【概念】
转载连接:树状数组 讲的挺好。
这两题非常的相似,查询区间的累加和、更新结点。Add(x,d) 与 Query(L,R) 的操作
【题目链接:candy】
唉,也是现在才发现这题用了这个知识,当初A的第一个数据结构的题就是关于树状数组的,忘了忘了。。
Problem C: candy
Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 252 Solved: 63
Description
Kimi has a lot of candies, and divides them into piles, where the ith pile contains Ai candies. Each time Kimi will choose an interval [l,r], and calculate the total amount of Al,Al+1,…,Ar. It's a hard task, and you're required to solve it.
Input
An integer T(T≤10) will exist in the first line of input, indicating the number of test cases. Each test case begins with the number of pilesN(1≤N≤105). The second line contains N integers Ai(1≤Ai≤100), where Ai stands for the number of candies in the ith pile. The next line is the number of queries M(1≤M≤105). The next M lines, each with two integers l,r(1≤l≤r≤N), describe the queried intervals.
Output
For each test case, output the total amount of candies in the queried interval.
Sample Input
Sample Output
#include<cstdio>
#include<cstring>
const int MAXN = ;
int a[MAXN];
int m;
int lowbit(int r){
return r & (-r);
}
int sum(int x){
int ret = ;
while(x > ){
ret += a[x];
x -= lowbit(x);
}
return ret;
}
void add(int i,int x){
while(i <= m){
a[i] += x;
i += lowbit(i);
}
}
int main(){
int n;
scanf("%d",&n);
while(n--){
memset(a,,sizeof(a));
scanf("%d",&m);
int i,j;
for(i = ;i <= m;i++){
int x;
scanf("%d",&x);
add(i,x);
}
int t;
scanf("%d",&t);
while(t--){
int num1,num2;
scanf("%d%d",&num1,&num2);
printf("%d\n",sum(num2) - sum(num1 - ));
}
}
}
【题目链接:NYOJ-士兵杀敌(二)】
相似的简单题链接:士兵杀敌(一) 只涉及查询区间和
士兵杀敌(二)
- 描述
-
南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的。
小工是南将军手下的军师,南将军经常想知道第m号到第n号士兵的总杀敌数,请你帮助小工来回答南将军吧。
南将军的某次询问之后士兵i可能又杀敌q人,之后南将军再询问的时候,需要考虑到新增的杀敌数
- 输入
- 只有一组测试数据
第一行是两个整数N,M,其中N表示士兵的个数(1<N<1000000),M表示指令的条数。(1<M<100000)
随后的一行是N个整数,ai表示第i号士兵杀敌数目。(0<=ai<=100)
随后的M行每行是一条指令,这条指令包含了一个字符串和两个整数,首先是一个字符串,如果是字符串QUERY则表示南将军进行了查询操作,后面的两个整数m,n,表示查询的起始与终止士兵编号;如果是字符串ADD则后面跟的两个整数I,A(1<=I<=N,1<=A<=100),表示第I个士兵新增杀敌数为A. - 输出
- 对于每次查询,输出一个整数R表示第m号士兵到第n号士兵的总杀敌数,每组输出占一行
- 样例输入
-
5 6
1 2 3 4 5
QUERY 1 3
ADD 1 2
QUERY 1 3
ADD 2 3
QUERY 1 2
QUERY 1 5 - 样例输出
-
6
8
8
20
#include<stdio.h>
#define size 1000008 int a[size];
int N;
int lowbit(int r)
{
return r & (-r);
}
int sum(int x)
{
int ret = ;
while(x>)
{
ret+=a[x];x-=lowbit(x);
}
return ret;
}
void add(int i,int x)
{
while(i<=N)
{
a[i]+=x;
i+=lowbit(i);
}
} int main()
{
int M,m,n,x;
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++)
{
scanf("%d",&x);
add(i,x);
}
while(M--)
{
char ac[];
scanf("%s%d%d",ac,&m,&n);
if(ac[]=='Q')
{
printf("%d\n",sum(n)-sum(m-));
}
else
{
add(m,n);
}
}
return ;
}
继续努力吧~
【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)的更多相关文章
- NYOJ 116士兵杀敌(二) 树状数组
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=116 士兵杀敌(一) 数组是固定的,所以可以用一个sum数组来保存每个元素的和就行,但是不 ...
- NYOJ 116 士兵杀敌二
士兵杀敌(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...
- 【转载】区间信息的维护与查询(一)——二叉索引树(Fenwick树、树状数组)
在网上找到一篇非常不错的树状数组的博客,拿来转载,原文地址. 树状数组 最新看了一下区间的查询与修改的知识,最主要看到的是树状数组(BIT),以前感觉好高大上的东西,其实也不过就这么简单而已. 我们有 ...
- 二叉索引树BIT
定义 二叉索引树,binary index tree,又名树状数组,或Fenwick Tree,因为本算法由Fenwick创造. 对于数组A,定义Query(i,j) = Ai +Ai ...
- C++实用数据结构:二叉索引树
看下面这个问题(动态连续和查询): 有一个数组A(长度为n),要求进行两种操作: add(i,x):让Ai增大x: query(a,b):询问Aa+Aa+1+...+Ab的和: 若进行模拟,则每次qu ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- NYOJ 116 士兵杀敌(二)(二叉索引树)
http://acm.nyist.net/JudgeOnline/problem.php?pid=116 题意: 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的 ...
- HDU 1166 敌兵布阵(线段树 or 二叉索引树)
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N<=50000),表示敌人有 ...
- HDU 4325 离散化+树状数组 或者 不使用树状数组
题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...
随机推荐
- PHP之Error与Logging函数讲解
PHP Error 和 Logging 简介 error 和 logging 函数允许你对错误进行处理和记录. error 函数允许用户定义错误处理规则,并修改记录错误的方式. logging 函数允 ...
- 【WCF--初入江湖】07 分布式事务
07 分布式事务 一.前言 [1]理解事务特性 [2]掌握TransactionFlow 特性 [3]掌握WCF中的事务属性 TransactionAutoCompleteOnSessionClose ...
- POJ 3125 Printer Queue(队列,水题)
题意:有多组数据,每组数据给出n,m,n表示需要打印的文件个数,m表示要打印的目标位置(m为0~n-1). 接下来给出n个数,第i个值对应第i-1个位置的优先级大小. 打印规则如下: ...
- java基础知识回顾之javaIO类--File类
File类是对文件系统中文件以及目录(文件夹)进行封装的对象,可以通过面向对象的思想来操作文件和目录(文件夹).File类保存文件或目录的各种元素的信息,包括文件名,文件长度,最后修改日期,是否可读, ...
- POJ 3411 Paid Roads(SPFA || DFS)
题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...
- hdu 4599 Dice 概率DP
思路: 1.求f[n];dp[i]表示i个连续相同时的期望 则 dp[0]=1+dp[1] dp[1]=1+(5dp[1]+dp[2])/6 …… dp[i]=1+(5dp[1 ...
- JSP include标签和include指令
test1.jsp <% int a = 5; out.println(a); %> test2.jsp <jsp:include page="/test1.jsp&quo ...
- Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray(II[BI[BIILjav
在eclipse上运行hadoop报错:Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.ha ...
- LR_问题_在导入wsdl时出现parsing error
问题描述:使用LR录制webservice协议的脚本,在导入wsdl时出现parsing error,详见图 问题解决:在导入wsdl时输入的地址错误,只指定了地址的虚拟目录名称,未指定方法名称,应该 ...
- php动态滚动加载实例
内容涉及:php.分页.jquery.div+css 实例下载:http://download.csdn.net/detail/roro5119/7373905 index.php <? //数 ...