区间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. 《Android Studio有用指南》7.1 AndroidStudio代码检查工具概述

    本文节选自<Android Studio有用指南> 作者: 毕小朋 博客: http://blog.csdn.net/wirelessqa 眼下本书已上传到百度阅读, 在百度中搜索[Anr ...

  2. C语言预处理条件语句的 与或运算

    1.#ifdef 与或运算 #ifdef  (MIN)  && (MAX)  ----------------------------错误使用 #if  defined(MIN)  & ...

  3. MVC——分页

    添加类PageBar.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. php 数组转xml 数组转json xml转数组 json转数组

    array->xml <?php function array2xml($array, $tag) { function ia2xml($array) { $xml="" ...

  5. 【iOS开发】---- UIView动画

      iOS 动画UIView动画 原文:http://www.cocoachina.com/bbs/read.php?tid=110168 1.概述 UIKit直接将动画集成到UIView类中,实现简 ...

  6. [Phoenix] 六、MR在Ali-Phoenix上的使用

    摘要: 在云HBASE上利用MR BULKLOAD入库PHOENIX表或通过MR构建PHOENIX索引表. 一.MR在Phoenix上的用途 利用MR对Phoenix表(可带有二级索引表)进行Bulk ...

  7. yum 工作原理

    MySQL :: A Quick Guide to Using the MySQL Yum Repository https://dev.mysql.com/doc/mysql-yum-repo-qu ...

  8. 可以执行全文搜索的原因 Elasticsearch full-text search Kibana RESTful API with JSON over HTTP elasticsearch_action es 模糊查询

    https://www.elastic.co/guide/en/elasticsearch/guide/current/getting-started.html Elasticsearch is a ...

  9. Delphi的类方法不是静态方法

    Delphi中,类方法不是你理解的静态方法 Delphi中的类方法与C++类中的static方法完全没有可比性.Delphi中的类方法是有Self的,而Self就是类本身(注意不是对象),而这个Sel ...

  10. 【C++基础学习】引用和指针、重载、函数、内存管理

    第一部分:引用VS指针 引用的含义:变量的别名 注意:变量不能只有别名,必须有一个真实的变量与之相对应 基本数据类型的引用 对别名本身的操作和它的实体的操作是一样的 1.基本数据类型的引用 类型 &a ...