zoukankan      html  css  js  c++  java
  • 1081 子段求和(前缀和)

    1081 子段求和(前缀和)(51NOD基础题)

    基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
     
    给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和。
     
    例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1。3 + 7 + 9 = 19,输出19。
    Input
    第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)
    Output
    共Q行,对应Q次查询的计算结果。
    Input示例
    5
    1
    3
    7
    9
    -1
    4
    1 2
    2 2
    3 2
    1 5
    Output示例
    4
    10
    16
    19
    #include <cstdio>
    #include <cstring>
    #define LL long long
    #define maxn 50000+500
    
    LL n ; 
    LL num[maxn] ; 
    LL sum[maxn] ; 
    LL Q ; 
    LL left , len; 
    
    int main(){
        while(~scanf("%lld" , &n)){
            memset(sum , 0 , sizeof(sum)) ; 
            
            for(int i=1 ; i<=n ; i++){//累加前缀和 
                scanf("%lld" , &num[i]) ; 
                sum[i] = sum[i-1] + num[i] ;  
            }
            
            scanf("%lld" , &Q) ; 
            for(int i=0 ; i<Q ; i++){//前缀和相减 求出起点left长度len数段的和 
                scanf("%lld%lld" , &left , &len) ; 
                printf("%lld
    " , sum[left+len-1] - sum[left-1]) ; 
            }
        }
        return 0 ; 
    } 
  • 相关阅读:
    搜索框用定时器限制发送请求
    vue的生命周期,钩子函数
    事件委托的实现流程
    在vscode中快速生成vue模板
    JS继承
    各种宽高
    ES6新特性
    python入门学习一
    字符编码
    npm install --save 与 npm install --save-dev 的区别
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7553813.html
Copyright © 2011-2022 走看看