51NOD 1081 子段求和
第1行:一个数N,N为数组的长度(2 <= N <= 50000)。
第2 至 N + 1行:数组的N个元素。(-10^9 <= N[i] <= 10^9)
第N + 2行:1个数Q,Q为查询的数量。
第N + 3 至 N + Q + 2行:每行2个数,i,l(1 <= i <= N,i + l <= N)
共Q行,对应Q次查询的计算结果。 前缀和
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
typedef long long ll;
int n,k;
int s[maxn]; ll sum [maxn]; int main (){ scanf("%d ", &n);
for(int i=; i <= n; i++)
scanf("%d",&s[i]);
sum[] = ;
for(int i=;i <= n;i++){
sum [i] = sum[i-] + s[i];
} scanf("%d", &k); for(int i=;i <= k;i++){
int a,b;
scanf("%d %d",&a,&b);
printf("%lld\n",sum[a+b-] - sum[a-]);
}
return ;
}
树状数组
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
typedef long long ll;
int n,k; ll bit[maxn]; void update(int i,int d){
while (i <= n){
bit[i] += d;
i += i & (-i);
}
} ll sum (int i){
ll s = ;
while (i > ){
s += bit[i];
i -= i & (-i);
}
return s;
} int main (){
memset(bit,,sizeof(bit));
scanf("%d", &n); int x;
for(int i=; i <= n; i++){
scanf("%d",&x);
update(i,x);
} scanf("%d", &k);
for(int i=;i <= k;i++){
int a,b;
scanf("%d %d",&a,&b);
printf("%lld\n",sum(a+b-) - sum(a-));
}
return ;
}
51NOD 1081 子段求和的更多相关文章
- 51nod 1081 子段求和(线段树 | 树状数组 | 前缀和)
题目链接:子段求和 题意:n个数字序列,m次询问,每次询问从第p个开始L长度序列的子段和为多少. 题解:线段树区间求和 | 树状数组区间求和 线段树: #include <cstdio> ...
- (前缀和 内存分配)51NOD 1081 子段求和
给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和. 例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 ...
- 【51NOD-0】1081 子段求和
[算法]树状数组(区间和) [题解]记得开long long #include<cstdio> #include<cstring> #include<algorithm& ...
- 51Nod 1081:子段求和(前缀和)
1081 子段求和 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和 ...
- 51nod 1258 序列求和 V4
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1258 1258 序列求和 V4 基准时间限制:8 秒 空间限制:131 ...
- 51NOD 1258 序列求和 V4 [任意模数fft 多项式求逆元 伯努利数]
1258 序列求和 V4 题意:求\(S_m(n) = \sum_{i=1}^n i^m \mod 10^9+7\),多组数据,\(T \le 500, n \le 10^{18}, k \le 50 ...
- 51Nod 1680 区间求和 树状数组
题意: 给出一个长度为\(n\)的数列\(A_i\),定义\(f(k)\)为所有长度大于等于\(k\)的子区间中前\(k\)大数之和的和. 求\(\sum_{k=1}^{n}f(k) \; mod \ ...
- 51nod1081 子段求和
给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和. 例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 = ...
- 51nod 1228 序列求和(伯努利数)
1228 序列求和 题目来源: HackerRank 基准时间限制:3 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 T(n) = n^k,S(n) = T(1 ...
随机推荐
- java连接数据库时的报错
//java连接数据库时的报错 1 package Java数据库编程; import java.sql.DriverManager; import java.sql.SQLException; im ...
- 第五课 JAVA反射获取对象属性和方法
package com.hero; import java.lang.reflect.Field; public class TestReflction5 { public static void m ...
- mysql "order by" "distinct" "group by" "having"
本文用到的表结构 create table stu( stu_id int auto_increment primary key, name ) not null, age smallint, cls ...
- 万恶之源 - Python文件操作
文件操作 初始文件操作 使用Python来读写文件是非常简单的操作,我们使用open()函数来打开一个文件,获取到文件句柄,然后通过文件句柄就可以进行各种各样的操作了 根据打开方式的不同能够执行的操作 ...
- lua的文件管理
lua没有自己的文件管理 只有读取和写入文件,但是可以通过调用lfs(LuaFileSystem),lfs是一个 用于lua进行文件访问的库,支持lua5.1和lua5.2,并且跨平台 lfs的使用: ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- 深度学习Momentum(动量方法)
转自:http://blog.csdn.net/bvl10101111/article/details/72615621 先上结论: 1.动量方法主要是为了解决Hessian矩阵病态条件问题(直观上讲 ...
- MySQL--教程
登入登出 首先启动服务,然后 mysql -u root -p 命令输入密码登入. mysql退出三种方法:mysql > exit;mysql > quit;mysql > \q;
- sql 关于存储过程的查询
--查数据库中所有的存储过程select * from sys.procedures ----------------------查数据库中所有的存储过程select o.name from sysc ...
- apache源码安装
1.apr和apr-util,下载地址: http://apr.apache.org/download.cgi yum install gcc yum install libtool yum inst ...