#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define INF 0x3f3f3f3f3f
const int N = 1000010;
using namespace std;
int arr[N],pre[N];
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,q;
int l,r;
cin>>n>>q;
for(int i = 1; i<= n; i++){
cin>>arr[i];
//记录前缀和
pre[i] = pre[i-1] + arr[i];
}
while(q--){
cin>>l>>r;
cout<<pre[r] - pre[l-1]<<endl;
}
return 0;
}
前缀和算法模板
2024-03-24 09:50:03 33 阅读