题解 | #【模板】前缀和#
【模板】前缀和
https://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf
#include <iostream> #include<vector> using namespace std; int main() { int n, q; cin >> n >> q; //原数组 vector<int> arr(n + 1); //前缀和数组 vector<long long> dp(n + 1); //输入数组值并计算前缀和数组 for(int i = 1; i <= n; i++) { cin >> arr[i]; dp[i] = dp[i - 1] + arr[i]; } //按q组输出 while (q--) { int l = 0, r = 0; cin >> l >> r; cout << dp[r] - dp[l - 1] << endl; } return 0; }