很简单,求数组前缀和数组,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")