《一维前缀和》

54 阅读1分钟
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int a[N], S[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m;
    cin >> n >> m;
    
    for (int i = 1; i <= n; i ++) cin >> a[i];
    
    for (int i = 1; i <= n; i ++) S[i] = S[i - 1] + a[i];
    
    while (m --)
    {
        int l, r;
        cin >> l >> r;
        
        int res = S[r] - S[l - 1];
        cout << res << endl;
    }
    
    return 0;
}