题目链接:子段求和

题意:n个数字序列,m次询问,每次询问从第p个开始L长度序列的子段和为多少。

题解:线段树区间求和 | 树状数组区间求和

线段树:

  1. #include <cstdio>
  2. #define LC(a) ((a<<1))
  3. #define RC(a) ((a<<1)+1)
  4. #define MID(a,b) ((a+b)>>1)
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. const int N=5e4*;
  9. ll ans=;
  10.  
  11. struct node{
  12. ll l,r,sum;
  13. }tree[N];
  14.  
  15. void pushup(ll p){
  16. tree[p].sum=tree[LC(p)].sum+tree[RC(p)].sum;
  17. }
  18.  
  19. void build(ll p,ll l,ll r){
  20. tree[p].l=l;
  21. tree[p].r=r;
  22. tree[p].sum=;
  23. if(l==r){
  24. scanf("%lld",&tree[p].sum);
  25. return;
  26. }
  27. build(LC(p),l,MID(l,r));
  28. build(RC(p),MID(l,r)+,r);
  29. pushup(p);
  30. }
  31.  
  32. void query(ll p,ll l, ll r){
  33. if(r<tree[p].l||l>tree[p].r) return;
  34. if(l<=tree[p].l&&r>=tree[p].r){
  35. ans+=tree[p].sum;
  36. return;
  37. }
  38. query(LC(p),l,r);
  39. query(RC(p),l,r);
  40. }
  41.  
  42. int main(){
  43.  
  44. ll n,q;
  45. scanf("%lld",&n);
  46. build(,,n);
  47. scanf("%lld",&q);
  48. while(q--){
  49. ans=;
  50. ll st,len;
  51. scanf("%lld%lld",&st,&len);
  52. query(,st,st+len-);
  53. printf("%lld\n",ans);
  54. }
  55.  
  56. return ;
  57. }

树状数组:

  1. #include <cstdio>
  2. #define low(i) ((i)&(-i))
  3. using namespace std;
  4.  
  5. const int N=5e4+;
  6. typedef long long ll;
  7. ll a[N];
  8. int n,q;
  9.  
  10. struct TreeArray {
  11. ll c[N];
  12. void add(int pos,ll v) {
  13. for(int i=pos;i<=n;i+=low(i)) c[i]+=v;
  14. }
  15. ll query(int pos) {
  16. ll res=;
  17. for(int i=pos;i;i-=low(i)) res+=c[i];
  18. return res;
  19. }
  20. }p;
  21.  
  22. int main(){
  23. scanf("%d",&n);
  24. for(int i=;i<=n;i++) scanf("%lld",&a[i]);
  25. for(int i=;i<=n;i++) p.add(i,a[i]);
  26. scanf("%d",&q);
  27. while(q--){
  28. int st,len;
  29. scanf("%d%d",&st,&len);
  30. printf("%lld\n",p.query(st+len-)-p.query(st-));
  31. }
  32. return ;
  33. }

前缀和:

  1. #include <cstdio>
  2. #define low(i) ((i)&(-i))
  3. using namespace std;
  4.  
  5. const int N=5e4+;
  6. typedef long long ll;
  7. ll sum[N];
  8. int n,q;
  9.  
  10. int main(){
  11. sum[]=;
  12. scanf("%d",&n);
  13. for(int i=;i<=n;i++){
  14. scanf("%lld",&sum[i]);
  15. sum[i]+=sum[i-];
  16. }
  17. scanf("%d",&q);
  18. while(q--){
  19. int st,len;
  20. scanf("%d%d",&st,&len);
  21. printf("%lld\n",sum[st+len-]-sum[st-]);
  22. }
  23. return ;
  24. }

51nod 1081 子段求和(线段树 | 树状数组 | 前缀和)的更多相关文章

  1. 51NOD 1081 子段求和

    1081 子段求和   给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和.   例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} ...

  2. (前缀和 内存分配)51NOD 1081 子段求和

    给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和.   例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 ...

  3. 51nod 1680区间求和 (dp+树状数组/线段树)

    不妨考虑已知一个区间[l,r]的k=1.k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间) 那么如果加入一个新的数字a[i](i = r+1) 则新区间[l, i]的答案为 ...

  4. FZU2013 A short problem —— 线段树/树状数组 + 前缀和

    题目链接:https://vjudge.net/problem/FZU-2013  Problem 2013 A short problem Accept: 356    Submit: 1083Ti ...

  5. CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...

  6. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  7. 【51NOD-0】1081 子段求和

    [算法]树状数组(区间和) [题解]记得开long long #include<cstdio> #include<cstring> #include<algorithm& ...

  8. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  9. MooFest 树状数组 + 前缀和

    比较友好的数据结构题 建议读者好好思考思考--. 可以分析出与前缀和的关系, 之后就愉快的套起树状数组辣 #include <cstdio> #include<queue> # ...

随机推荐

  1. python 获取列表中次大的数值.

    需求: 1.写个函数,把一组数字传到函数中,然后取出最大值和次大值. 2.不能使用排序函数. 分析: Q: list = [100,50,60,70,30,45] 怎么从这个列表中取出最大值? A: ...

  2. 剑指offer(12)

    来两道关于链表链接的题目: 题目一: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 本题要考虑到其中一条链表是空或者两个都是空的情况. 在每个链表安上一 ...

  3. 通用模块设计UMD

    https://leohxj.gitbooks.io/front-end-database/content/javascript-modules/about-umd.html UMD(universa ...

  4. git遇到的问题 .Git: There is no tracking information for the current branch.

    1.Git: There is no tracking information for the current branch. 在执行git pull的时候,提示当前branch没有跟踪信息: git ...

  5. windos下完全卸载MySQL

    1.停止mysql服务(win+R,输入:services.msc回车) 2.控制面板卸载MySQL 3.cmd下删除MySQL服务:sc delete MySQL 4.删除目录 (1) C:\Pro ...

  6. Linux基础学习(16)--备份与恢复

    第十六章——备份与恢复 一.备份概述 1.Linux系统需要备份的数据: 2.备份策略: 二.dump和restore命令 1.dump命令: 2.restore命令:

  7. Linux基础学习(14)--日志管理

    第十四章——日志管理 一.日志管理简介 1.日志服务: 2.常见日志的作用: 二. rsyslogd日志服务 1.日志文件格式: 2./etc/rsyslog.conf配置文件: 三.日志轮替 1.日 ...

  8. idea创建maven项目的一点关键

    maven中的一些概念: POM:项目对象模型(Project Object Model),是项目的一些关键元信息的集合.主要包含项目管理信息.具体的项目描述.开发小组的构 成.源代码库(如CVS)和 ...

  9. git 提交的步骤

    1. git init //初始化仓库   2. git add .(文件name) //添加文件到本地仓库   3. git commit -m "first commit" / ...

  10. Python——进程队列

    队列 先进先出 from multiprocessing import Queue q = Queue(5) #队列的大小 q.put(1) #放入内容 q.put(2) #放入内容 q.put(3) ...