算法:前缀和
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], s[N]; //s[N]前缀和数组 s[i] = a[1] + a[2] + a[3] + ...+ a[i]
int main(){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){ //前缀和公式:s[n] = s[n-1] + a[n]
s[i] = s[i - 1] + a[i];
}
while(m--){
int l, r;
cin >> l >> r;
cout << s[r] - s[l - 1] << endl; //[l~r]的前缀和公式:s[r] - s[l-1]
}
return 0;
}
标签:std,10,前缀,int,基础,算法,main
From: https://www.cnblogs.com/csai-H/p/16924476.html