c++题解 | #【模板】前缀和#

【模板】前缀和

https://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf

很简单,求数组前缀和数组,ans = pres[r]-pres[l-1],下标从1开始,很方便。

#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
int n,q;
LL pres[N];
int main() {
    scanf("%d%d", &n,&q);
    LL tmp = 0;
    int a;
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a);
        pres[i] = pres[i-1]+a;
    }
    int l,r;
    for(int i = 1;i<=q;i++){
        scanf("%d%d", &l, &r);
        printf("%lld\n", pres[r]-pres[l-1]);
    }

    return 0;
}
// 64 位输出请用 printf("%lld")
全部评论

相关推荐

02-15 22:29
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务