-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbinaryIndexedTree.cpp
71 lines (64 loc) · 1.15 KB
/
binaryIndexedTree.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Using Binary Indexed Tree, we can do both upgrade and query tasks in O(Logn) time.
The advantages of Binary Indexed Tree over Segment are, requires less space and very easy to implement.
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<stdlib.h>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<string>
using namespace std;
#define lli long long int
#define fr(a,b,c) for(a=b;a<c;a++)
#define vi vector<int>
#define vlli vector<long long int >
#define vpii vector<pair<int,int>>
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define f first
#define s second
#define si(a) scanf("%d",&a)
#define slli(a) scanf("%lld",&a)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
lli mod=1000000007;
int bit[1001];
int v[10001];
void update(int n,int val ,int ind)
{
int i=ind;
while(i<=n)
{
bit[i]+=val;
i+=i&-i;
}
}
int query(int i)
{
int j;
int sum=0;
for(j=i;j>=1;j-=j&-j)
{
sum+=bit[j];
}
return sum;
}
int main()
{
int i,n;
cin>>n;
fr(i,1,n+1)
{
cin>>v[i];
update(n,v[i],i);
}
fr(i,0,5)
{
cout<<query(i+1)<<endl;
}
}