-
Notifications
You must be signed in to change notification settings - Fork 70
/
Sum_of_Query_II.cpp
46 lines (44 loc) · 977 Bytes
/
Sum_of_Query_II.cpp
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
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
vector<int> querySum(int n, int arr[], int q, int queries[])
{
// code here
vector<int> v;
int i=0;
int temp1=0,temp2=1;
while(q--){
int sum=0;
int l=queries[temp1],r=queries[temp2];
for(int i=l-1;i<r;i++){
sum+=arr[i];
}
temp1+=2;
temp2+=2;
v.push_back(sum);
}
return v;
}
};
int main(){
int t;
cin>>t;
while(t--){
int n, q;
cin>>n;
int arr[n];
for(int i = 0;i < n;i++)
cin>>arr[i];
cin>>q;
int queries[2*q];
for(int i = 0;i < 2*q;i += 2)
cin>>queries[i]>>queries[i+1];
Solution ob;
vector<int> ans = ob.querySum(n, arr, q, queries);
for(int u: ans)
cout<<u<<" ";
cout<<endl;
}
return 0;
}