目錄
一:題目
二:算法原理
三:代碼實現
一:題目
題目鏈接:【模板】前綴和_牛客題霸_牛客網?
二:算法原理
三:代碼實現
#include <iostream>
#include <vector>
using namespace std;int main()
{//1.讀入數據int n = 0,q = 0;cin >> n >> q;vector<int> arr(n+1);for(int i = 1 ; i <= n; i++)cin >>arr[i];//2.預處理一個前綴和數組vector<long long> dp(n+1);//防止溢出for(int i = 1 ;i <= n; i++)dp[i] = dp[i-1] + arr[i];//3.使用前綴和數組 int l = 0,r = 0;while(q--){cin >> l >> r;cout << dp[r] - dp[l-1]<<endl;}return 0;
}
// 64 位輸出請用 printf("%lld")