博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)
阅读量:4607 次
发布时间:2019-06-09

本文共 3355 字,大约阅读时间需要 11 分钟。

【概念】

  转载连接:  讲的挺好。

这两题非常的相似,查询区间的累加和、更新结点。Add(x,d) 与 Query(L,R) 的操作

【题目链接:】

  唉,也是现在才发现这题用了这个知识,当初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≤lrN), describe the queried intervals.

Output

For each test case, output the total amount of candies in the queried interval.

Sample Input

1
5
1 2 4 5 9
3
1 2
2 4
4 5

Sample Output

3
11
14
1 #include
2 #include
3 const int MAXN = 1000008; 4 int a[MAXN]; 5 int m; 6 int lowbit(int r){ 7 return r & (-r); 8 } 9 int sum(int x){10 int ret = 0;11 while(x > 0){12 ret += a[x];13 x -= lowbit(x);14 }15 return ret;16 }17 void add(int i,int x){18 while(i <= m){19 a[i] += x;20 i += lowbit(i);21 }22 }23 int main(){24 int n;25 scanf("%d",&n);26 while(n--){27 memset(a,0,sizeof(a)); 28 scanf("%d",&m);29 int i,j; 30 for(i = 1;i <= m;i++){31 int x;32 scanf("%d",&x);33 add(i,x);34 }35 int t;36 scanf("%d",&t);37 while(t--){38 int num1,num2;39 scanf("%d%d",&num1,&num2); 40 printf("%d\n",sum(num2) - sum(num1 - 1));41 }42 }43 }

 

【题目链接:】

相似的简单题链接: 只涉及查询区间和

士兵杀敌(二)

时间限制:
1000 ms  |  内存限制:65535 KB
难度:
5
描述

南将军手下有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 61 2 3 4 5QUERY 1 3ADD 1 2QUERY 1 3ADD 2 3QUERY 1 2QUERY 1 5
样例输出
68820
1   2 #include
3 #define size 1000008 4 5 int a[size]; 6 int N; 7 int lowbit(int r) 8 { 9 return r & (-r);10 }11 int sum(int x)12 {13 int ret = 0;14 while(x>0)15 {16 ret+=a[x];x-=lowbit(x);17 }18 return ret;19 }20 void add(int i,int x)21 {22 while(i<=N)23 {24 a[i]+=x;25 i+=lowbit(i);26 }27 }28 29 int main()30 {31 int M,m,n,x;32 scanf("%d%d",&N,&M);33 for(int i=1;i<=N;i++)34 {35 scanf("%d",&x);36 add(i,x);37 }38 while(M--)39 {40 char ac[5];41 scanf("%s%d%d",ac,&m,&n);42 if(ac[0]=='Q')43 {44 printf("%d\n",sum(n)-sum(m-1));45 }46 else47 {48 add(m,n);49 }50 }51 return 0;52 }

 

继续努力吧~

转载于:https://www.cnblogs.com/zhengbin/p/4462942.html

你可能感兴趣的文章
队列实现霍夫曼树
查看>>
【Java】图片高质量缩放类
查看>>
详解定位与定位应用
查看>>
【前端开发】 5分钟创建 Mock Server
查看>>
java 从键盘录入的三种方法
查看>>
使用jQuery和YQL,以Ajax方式加载外部内容
查看>>
pyspider 示例
查看>>
JAVA 笔记(一)
查看>>
c# 范型Dictionary实用例子
查看>>
C#实现动态页面静态化
查看>>
可选参数、命名参数、.NET的特殊类型、特性
查看>>
利用CGLib实现动态代理实现Spring的AOP
查看>>
面试之SQL(1)--选出选课数量>=2的学号
查看>>
IIS处理并发请求时出现的问题
查看>>
优先队列小结
查看>>
线程安全与可重入函数之间的区别与联系
查看>>
{Nodejs} request URL 中文乱码
查看>>
异常及日志使用与项目打包
查看>>
努力,时间,坚持,自律
查看>>
Hadoop2.6.0 动态增加节点
查看>>