51nod 1081 子段求和(线段树 | 树状数组 | 前缀和)
题目链接:子段求和
题意:n个数字序列,m次询问,每次询问从第p个开始L长度序列的子段和为多少。
题解:线段树区间求和 | 树状数组区间求和
线段树:
#include <cstdio>
#define LC(a) ((a<<1))
#define RC(a) ((a<<1)+1)
#define MID(a,b) ((a+b)>>1)
using namespace std; typedef long long ll;
const int N=5e4*;
ll ans=; struct node{
ll l,r,sum;
}tree[N]; void pushup(ll p){
tree[p].sum=tree[LC(p)].sum+tree[RC(p)].sum;
} void build(ll p,ll l,ll r){
tree[p].l=l;
tree[p].r=r;
tree[p].sum=;
if(l==r){
scanf("%lld",&tree[p].sum);
return;
}
build(LC(p),l,MID(l,r));
build(RC(p),MID(l,r)+,r);
pushup(p);
} void query(ll p,ll l, ll r){
if(r<tree[p].l||l>tree[p].r) return;
if(l<=tree[p].l&&r>=tree[p].r){
ans+=tree[p].sum;
return;
}
query(LC(p),l,r);
query(RC(p),l,r);
} int main(){ ll n,q;
scanf("%lld",&n);
build(,,n);
scanf("%lld",&q);
while(q--){
ans=;
ll st,len;
scanf("%lld%lld",&st,&len);
query(,st,st+len-);
printf("%lld\n",ans);
} return ;
}
树状数组:
#include <cstdio>
#define low(i) ((i)&(-i))
using namespace std; const int N=5e4+;
typedef long long ll;
ll a[N];
int n,q; struct TreeArray {
ll c[N];
void add(int pos,ll v) {
for(int i=pos;i<=n;i+=low(i)) c[i]+=v;
}
ll query(int pos) {
ll res=;
for(int i=pos;i;i-=low(i)) res+=c[i];
return res;
}
}p; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&a[i]);
for(int i=;i<=n;i++) p.add(i,a[i]);
scanf("%d",&q);
while(q--){
int st,len;
scanf("%d%d",&st,&len);
printf("%lld\n",p.query(st+len-)-p.query(st-));
}
return ;
}
前缀和:
#include <cstdio>
#define low(i) ((i)&(-i))
using namespace std; const int N=5e4+;
typedef long long ll;
ll sum[N];
int n,q; int main(){
sum[]=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld",&sum[i]);
sum[i]+=sum[i-];
}
scanf("%d",&q);
while(q--){
int st,len;
scanf("%d%d",&st,&len);
printf("%lld\n",sum[st+len-]-sum[st-]);
}
return ;
}
51nod 1081 子段求和(线段树 | 树状数组 | 前缀和)的更多相关文章
- 51NOD 1081 子段求和
1081 子段求和 给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和. 例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} ...
- (前缀和 内存分配)51NOD 1081 子段求和
给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和. 例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 ...
- 51nod 1680区间求和 (dp+树状数组/线段树)
不妨考虑已知一个区间[l,r]的k=1.k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间) 那么如果加入一个新的数字a[i](i = r+1) 则新区间[l, i]的答案为 ...
- FZU2013 A short problem —— 线段树/树状数组 + 前缀和
题目链接:https://vjudge.net/problem/FZU-2013 Problem 2013 A short problem Accept: 356 Submit: 1083Ti ...
- CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- 【51NOD-0】1081 子段求和
[算法]树状数组(区间和) [题解]记得开long long #include<cstdio> #include<cstring> #include<algorithm& ...
- ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...
- MooFest 树状数组 + 前缀和
比较友好的数据结构题 建议读者好好思考思考--. 可以分析出与前缀和的关系, 之后就愉快的套起树状数组辣 #include <cstdio> #include<queue> # ...
随机推荐
- Microsoft Visual Studio Ultimate 2013密钥
Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9Visual Studio Premium 2013 KEY(密钥): ...
- python爬虫之git的使用
一.简单认识: 1.初始化文件夹为版本控制文件夹,首先建立一个文件夹,进入这个文件夹以后输入git init初始化这个文件夹. 2.Git几种位置概念 1.本地代码:本地更改完代码以后,虽然是存放在g ...
- 为linux主机增加file description
在benchmarked写的服务器的时候就遇到了too many file open 这个报错. 由于遇到过很多次了,所以知道应该是单机fd打满了. 首先来看看 机器最多支持多少fd cat /pro ...
- drf开发中常见问题
开发常见问题及解决 问题: 一.本地系统不能重现的bug 二.api接口出错不能及时的发现或难找到错误栈 三.api文档管理问题 四.大量的url配置造成url配置越来越多难以维护 五.接口不及时去更 ...
- linux audit审计(2)--audit启动
参考:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/sec- ...
- 一个加载时带动画效果的ListBoxItem
今天我们来谈一下ListBoxItem这个控件,ListBoxItem是直接从ContentControl继承而来的,所以可以添加到任何具有Content属性的控件中去,常见的ListBoxItem可 ...
- html5 服務器發送事件
html5允許頁面獲得來自服務器的更新. 單項消息傳送: 頁面獲得服務器的更新. 以前頁面也可以獲得服務器的更新,但必須詢問服務器是否有可用的更新,而服務器發送事件是單向自動發送. 使用服務器發送事件 ...
- SQL Server 数据库try catch 存储过程
SQL Server 在生产环境中这样写存储过程的坑都避免了吗? 原文链接: http://www.cnblogs.com/chenmh/p/7856777.html 概述 最近因为业务的需求写了一段 ...
- matlab颜色映射colormap() pcolor()
http://blog.csdn.net/qq_20823641/article/details/51711618
- Multiple websites on single instance of IIS
序幕 通常需要在单个IIS实例上托管多个网站,主要在开发环境中,而不是在生产服务器上.我相信它在生产服务器上不是一个首选解决方案,但这至少是一个可能的实现. Web服务器单实例上的多个网站的好处是: ...