• 51nod 1081 子段求和


    基准时间限制: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 <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    typedef long long LL;
    int N,i,Q,a;
    LL sum[50001];
    void read(int &x)
    {
        x=0;int f=1;
        char ch=getchar();
        while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+(int)ch-48;ch=getchar();}
        x*=f; 
    }
    int main()
    {
        ios::sync_with_stdio(false);
        read(N);
        for(i=1;i<=N;++i)
        {
            read(a);
            sum[i]=sum[i-1]+a;
        }
        read(Q);
        int a,b;
        while(Q--)
        {
            read(a);read(b);
            b=a+b-1;
            cout<<sum[b]-sum[a-1]<<endl;
        }
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    oracle修改字符编码
    oracle修改约束列
    oracle非空约束
    Linux修改字符集
    修改oracle字符集合
    word问题禁止宏
    增加修改表列
    oracle增加sequence
    增加 修改oracle约束条件
    oracle用户 密码永不过期
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6413148.html
Copyright © 2020-2023  润新知