区间sum

描述

有一个长度为n的正整数序列a1--an,candy想知道任意区间[L,R]的和,你能告诉他吗?

输入

第一行一个正整数n(0<n<=1e6),
第二行为长度为n的正整数序列 a1到an(ai<=1e9,sum(a)<=1e18),
第三行一个正整数m(m<=1e6),表示询问次数,
随后m行,每行两个正整数L,R(1<=L<=R<=n)。

输出

m行输出,对应m次询问结果

样例输入1 复制

5
1 4 3 6 3
3
1 3
2 2
1 5
样例输出1

8
4
17

一整数数列a1, a2, ... , an(有正有负),以及另一个整数k,求一个区间i,ji,j,(1 <= i <= j <= n),使得aii + ... + ajj = k。

 

Input第1行:2个数N,K。N为数列的长度。K为需要求的和。(2 <= N <= 10000,-10^9 <= K <= 10^9)
第2 - N + 1行:Aii(-10^9 <= Aii <= 10^9)。Output如果没有这样的序列输出No Solution。 
输出2个数i, j,分别是区间的起始和结束位置。如果存在多个,输出i最小的。如果i相等,输出j最小的。Sample Input

6 10
1
2
3
4
5
6

Sample Output

1 4

前缀和,通常用来求某个区间的和或和为多少的区间,前者时间复杂度由暴力O(n)节省到了O(1),是一个高效的区间和方法。

#include<stdio.h>

int main()
{
int n,m,l,r,i;
long long a[];
scanf("%d",&n);
a[]=;
scanf("%lld",&a[]);
for(i=;i<=n;i++){
scanf("%lld",&a[i]);
a[i]+=a[i-];
}
scanf("%d",&m);
for(i=;i<=m;i++){
scanf("%d%d",&l,&r);
printf("%lld\n",a[r]-a[l-]);
}
return ;
}


#include<stdio.h>
#include<string.h>
int main()
{
long long n,k,f,i,j; long long a[],b[];
scanf("%lld%lld",&n,&k);
memset(b,,sizeof(b));
scanf("%lld",&a[]);
b[]=a[];
for(i=;i<=n;i++){
scanf("%lld",&a[i]);
b[i]+=b[i-]+a[i];
}
f=;
for(i=;i<=n;i++){
for(j=i;j<=n;j++){
if(b[j]-b[i-]==k){
printf("%lld %lld\n",i,j);
f=;
break;
}
}
if(f==) break;
}
if(f==) printf("No Solution\n");
return ;
}

区间sum 和为k的连续区间-前缀和的更多相关文章

  1. 51nod 1094 和为k的连续区间【前缀和/区间差/map】

    1094 和为k的连续区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 一整数数列a1, a2, ... , an(有正有负),以及另一个整数k ...

  2. LeetCode862. Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  3. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  4. [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  5. [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  6. hdu6003 Problem Buyer 贪心 给定n个区间,以及m个数,求从n个区间中任意选k个区间,满足m个数都能在k个区间中找到一个包含它的区间,如果一个区间包含了x,那么 该区间不能再去包含另一个数,即k>=m。求最小的k。如果不存在这样的k,输出“IMPOSSIBLE!”。

    /** 题目:hdu6003 Problem Buyer 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6003 题意:给定n个区间,以及m个数,求从n个区 ...

  7. 862. Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  8. 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...

  9. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

随机推荐

  1. sed系列:行或者模式匹配删除特定行

    “p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its ...

  2. VCC/AVCC/VDD/AVDD区别

    V*与AV*的区别是:数字与模拟的区别CC与DD的区别是:供电电压与工作电压的区别(通常VCC>VDD): 数字电路供电VCC 模拟电路供电AVCCVDD是指工作电压,就是供电进芯片的 AVDD ...

  3. paxos算法之粗浅理解

    paxos出身 paxos出身名门,它爹是没多久前获得图灵奖的在分布式领域大名鼎鼎的LeslieLamport. paxos为何而生 那么Lamport他老人家为什么要搞这个东东呢,不是吃饱了撑的,而 ...

  4. 命令行查看memcached的运行状态(转载)

    很多时候需要监控服务器上的Memcached运行情况,比如缓存的查询次数,命中率之类的.但找到的那个memcached-tool是linux下用perl写的,我也没试过windows能不能用.后来发现 ...

  5. .Net——实现IConfigurationSectionHandler接口定义处理程序处理自定义节点

    除了使用.net里面提供的内置处理程序来处理我们的自定义节点外,我们还可以通过多种方法,来自己定义处理类处理我们的自定义节点,本文主要介绍通过实现IConfigurationSectionHandle ...

  6. CI学习相关地址

    1.CI中国:http://codeigniter.org.cn/ 2.CodeIgniter 2.1.3 for SAE:http://codeigniter.org.cn/forums/forum ...

  7. Android 调用QQ登录

    调用QQ登录        在如今的项目开发.调用第三方登录.差点儿是必须的,而调用QQ登录也是不可缺少的,这里把相关代码分享出来,希望能拿去就能够用,降低项目开发的时间.希望对大家实用. 1,去QQ ...

  8. jquery 通过ajax 提交表单

    1.需要引入以下两个js文件 <script src="Easyui/jquery-1.7.2.min.js"></script>    <scrip ...

  9. Carriage-Return Line-Feed

    Git 提交时报错warning: LF will be replaced by CRLF in - CSDN博客 https://blog.csdn.net/yan_less/article/det ...

  10. npm ERR! Unexpected end of JSON input while parsing near '...inimist":"^1.2.0"}

    简介 在项目中执行npm install安装依赖包的时候.出现npm ERR! Unexpected end of JSON input while parsing near '...inimist& ...