forked from Vishalrai2002/CP_Math_CB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path008PrefixSum.cpp
More file actions
46 lines (40 loc) · 872 Bytes
/
008PrefixSum.cpp
File metadata and controls
46 lines (40 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Given array a of n integers. Given Q queries and in each
query given L and R print sum of array elements from index L to R
(L, R included)
constraints
1<=N<=10^5
1<=a[i]<=10^9
1<=Q<=10^5
1<=L, R<=N
*/
#include<iostream>
using namespace std;
const int N=1e5+10;
int a[N];
int pf[N]; // prfix array declared
int main(){
int n;
cin>>n;
int arr[n];
for(int i=1;i<=n;i++){
cin>>arr[i];
// below i filled my prefix array and pf[i] store the sum till i
pf[i]=pf[i-1]+arr[i];
}
int q;
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
// long long sum=0;
// // Time Complexity -> O(N)+O(Q*N)
// for(int i=l;i<=r;i++){
// sum+=arr[i];
// }
// cout<<sum<<endl;
// Time complexity -> O(N)+O(Q)
cout<<pf[r]-pf[l-1]<<endl;
}
return 0;
}