https://www.acwing.com/problem/content/797/

#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int a[N], s[N];
int main(void)
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        s[i] = s[i - 1] + a[i];
    }
    while (m--)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        printf("%d\n", s[r] - s[l - 1]);
    }
    return 0;
}

下面是
自己写的屎山代码。。。

#include <iostream>
using namespace std;
// S(r) - S(l-1) = S(r-l)
const int N = 100010;
int arr[N];
int s[N];
int front_sum(int arr[], int l, int r)
{
    int sr = 0;
    for (int i = 0; i < r; i++)
    {
        sr += arr[i];
    }
    int sl = 0;
    for (int i = 0; i < l - 1; i++)
    {
        sl += arr[i];
    }
    int res = sr - sl;
    return res;
}
int main(void)
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    for (int i = 0; i < m*2; i++)
    {
        cin >> s[i];
    }
    int t1 = 0, t2 = 1;
    for (int i = 0; i < m; i++)
    {
        int l = s[t1];
        t1 = t1 + 2;
        int r = s[t2];
        t2 = t2 + 2;
        cout << front_sum(arr, l, r);
        cout << endl;
    }
}