【51Nod】1081 子段求和
来源:互联网 发布:全民奇迹服务端源码 编辑:程序博客网 时间:2024/06/06 13:56
题意
给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和。
例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1。3 + 7 + 9 = 19,输出19。
解题思路
线段树求区间和
参考代码
#include <iostream>using namespace std;#define MAXN 50000+1typedef long long ll;struct Node{ int l,r; ll x;}tree[4*MAXN];void build(int node,int l,int r){ tree[node].l=l,tree[node].r=r; if (l==r){ cin>>tree[node].x; return; } int mid=(l+r)/2; build(node*2,l,mid); build(node*2+1,mid+1,r); tree[node].x=tree[node*2].x+tree[node*2+1].x;}ll query(int node,int l,int r){ if (l<=tree[node].l && tree[node].r<=r) return tree[node].x; int mid=(tree[node].l+tree[node].r)/2; if (r<=mid) return query(node*2,l,r); else if (l>mid) return query(node*2+1,l,r); else return query(node*2,l,mid)+query(node*2+1,mid+1,r);}int main(){ int n,q,i,l; while (cin>>n){ build(1,1,n); cin>>q; while (q--){ cin>>i>>l; cout<<query(1,i,i+l-1)<<endl; } } return 0;}
阅读全文
0 0
- 51nod 1081 子段求和
- 51nod-1081子段求和
- 51nod 1081 子段求和
- 51nod 1081 子段求和
- 51Nod 1081 子段求和
- 【51Nod】1081 子段求和
- 51Nod-1081-子段求和
- 51Nod-1081 子段求和
- 51nod 1081 子段求和
- 51Nod-1081 子段求和
- 51Nod-1081-子段求和
- 51nod:1081 子段求和(树状数组)
- 51Nod-1081 子段求和【前缀和】
- 51Nod 1081 子段求和(数组/树状数组)
- 51nod-1081[前缀和]子段求和
- H 子段求和(51Nod 1081)
- 1081 子段求和
- 51nod 1081子串求和
- Python文件操作
- Linux中的sed常用技巧
- Vue基本开发环境搭建
- 深入理解Java类加载器(2):线程上下文类加载器
- 将项目上传至github
- 【51Nod】1081 子段求和
- 《数据结构学习与实验指导》3-4:一元多项式的乘法与加法运算
- c++ error records
- centos安装twemproxy实现分布式缓存
- C++深拷贝和浅拷贝
- 淘淘商城项目系列-1.Eclipse开发环境配置
- Your system is running in low-graphics mode
- github push 出错:fatal: Authentication failed for 'https://github.com/ ..的解决
- C语言几种输入函数的比较