题解 | #【模板】前缀和# 动态规划
【模板】前缀和
https://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf
#include <iostream> #include <vector> using namespace std; int main() { int n, q; cin >> n >> q; vector<int> arr1(n); //n个整数 for(int i = 0; i < n; ++i) cin >> arr1[i]; //记录前n项和的数组 vector<long long> arr2(n + 1, 0); for(int i = 0; i < n; ++i) arr2[i + 1] = arr2[i] + arr1[i]; while(q--) { int l,r; cin >> l >> r; cout << arr2[r] - arr2[l - 1] << endl; } return 0; }