題目連結:https://www.acwing.com/problem/content/description/797/
時/空限制:2s / 64MB
題目描述
輸入一個長度為n的整數序列。
接下來再輸入m個詢問,每個詢問輸入一對l, r。
對于每個詢問,輸出原序列中從第l個數到第r個數的和。
輸入格式
第一行包含兩個整數n和m。
第二行包含n個整數,表示整數數列。
接下來m行,每行包含兩個整數l和r,表示一個詢問的區間範圍。
輸出格式
共m行,每行輸出一個詢問的結果。
資料範圍
1≤l≤r≤n,
1≤n,m≤100000,
−1000≤數列中元素的值≤1000
輸入樣例
5 3
2 1 3 6 4
1 2
1 3
2 4
輸出樣例
解題思路
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int bits[MAXN];
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
bits[i] = bits[i - 1] + x;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", bits[r] - bits[l - 1]);
}
return 0;
}