forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_index_tree.cpp
57 lines (56 loc) · 1.09 KB
/
binary_index_tree.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>
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;
}