-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfenwicktree.cpp
More file actions
67 lines (62 loc) · 892 Bytes
/
fenwicktree.cpp
File metadata and controls
67 lines (62 loc) · 892 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
int ft[int(1e5+5)];
int zeroes(int n)
{
int count=0;
while(n)
{
if(n%10==0)
count++;
n/=10;
}
return count;
}
int getsum(int index)
{
int sum=0;
index++;
while(index)
{
sum += ft[index];
index -= index & (-index);
}
return sum;
}
void update(int n, int index, int val)
{
index++;
while(index <= n)
{
ft[index] += val;
index += index & (-index);
}
}
int build(int arr[], int n)
{
// int ft[n+1];
memset(ft, 0, sizeof ft);
for(int i=0; i<n; i++)
update(n, i, arr[i]);
for(int i=1; i<=n; i++)
cout<<ft[i]<<' ';
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; cin>>arr[i++]);
build(arr, n);
cout<<'\n';
int q;
cin>>q;
while(q--)
{
int l, r;
cin>>l>>r;
--l; --r;
int ans = getsum(r)-getsum(l);
cout<<ans;
}
}