forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab96d96
commit afdd917
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
data_structures/Tree/Binary-tree/c++/binary_index_tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
typedef pair<int,int> pii; | ||
typedef vector< pii> vii; | ||
typedef vector<int> vi; | ||
typedef vector< vi > vvi; | ||
typedef long long int LL; | ||
#define pb push_back | ||
#define mp make_pair | ||
#define scan(n) scanf("%d",&n) | ||
#define print(n) printf("%d\n",n) | ||
#define longscan(n) scanf("%lld",&n) | ||
#define longprint(n) printf("%lld\n",n) | ||
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) | ||
/*void sieve(int n) | ||
{ | ||
for(int i=2;i*i<=100000;i++) for(int j=2*i;j<=100000;j+=i) if(!sie[j]) sie[j]=1; | ||
}*/ | ||
int n; | ||
int bit[10000]; | ||
void update(int x, int val) | ||
{ | ||
for(; x <= n; x += x & -x) | ||
{ | ||
bit[x] += val; | ||
} | ||
} | ||
|
||
int query(int x) | ||
{ | ||
int sum = 0; | ||
for(; x > 0; x-= x&-x) | ||
{ | ||
sum+= bit[x]; | ||
} | ||
return sum; | ||
} | ||
int main() | ||
{ | ||
int q; | ||
cin >> n >> q; | ||
int a[n+9], quer[q+9]; | ||
for(int j=1;j<=n;j++) | ||
{ | ||
cin >> a[j]; | ||
update(j,a[j]); | ||
} | ||
for(int j=1;j<=q;j++) | ||
{ | ||
cin >> quer[j]; | ||
} | ||
for(int j=1;j<=q;j++) | ||
{ | ||
cout << query(quer[j]) << "\n"; | ||
} | ||
return 0; | ||
} |