-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
executable file
·57 lines (42 loc) · 1.29 KB
/
solution.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
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void construct(vector<ll> &tree, vector<ll> &arr, int start, int end, int node) {
if (start == end) {
tree[node] = arr[start];
return;
}
int mid = (start+end) >> 1;
int leftNode = node << 1;
int rightNode = leftNode + 1;
construct(tree, arr, start, mid, leftNode);
construct(tree, arr, mid+1, end, rightNode);
tree[node] = tree[leftNode] + tree[rightNode];
}
ll query(vector<ll> &tree, vector<ll> &arr, int start, int end, int l, int r, int node) {
if (r < start || end < l) return 0;
if (l <= start && end <= r) return tree[node];
int mid = (start+end) >> 1;
int leftNode = node << 1;
int rightNode = leftNode + 1;
ll leftResult = query(tree, arr, start, mid, l, r, leftNode);
ll rightResult = query(tree, arr, mid + 1, end, l, r, rightNode);
return leftResult + rightResult;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q, l, r;
cin >> n >> q;
vector<ll> arr(n), tree(4*n), lazy(4*n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
construct(tree, arr, 0, n-1, 1);
while (q--) {
cin >> l >> r;
cout << query(tree, arr, 0, n-1, l-1, r-1, 1) << '\n';
}
}