File tree 1 file changed +57
-0
lines changed
Data Structures/Fenwick_tree
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ typedef pair<int ,int > pii;
4
+ typedef vector< pii> vii;
5
+ typedef vector<int > vi;
6
+ typedef vector< vi > vvi;
7
+ typedef long long int LL;
8
+ #define pb push_back
9
+ #define mp make_pair
10
+ #define scan (n ) scanf(" %d" ,&n)
11
+ #define print (n ) printf(" %d\n " ,n)
12
+ #define longscan (n ) scanf(" %lld" ,&n)
13
+ #define longprint (n ) printf(" %lld\n " ,n)
14
+ #define fast_io ios_base::sync_with_stdio (false );cin.tie(NULL )
15
+ /* void sieve(int n)
16
+ {
17
+ for(int i=2;i*i<=100000;i++) for(int j=2*i;j<=100000;j+=i) if(!sie[j]) sie[j]=1;
18
+ }*/
19
+ int n;
20
+ int bit[10000 ];
21
+ void update (int x, int val)
22
+ {
23
+ for (; x <= n; x += x & -x)
24
+ {
25
+ bit[x] += val;
26
+ }
27
+ }
28
+
29
+ int query (int x)
30
+ {
31
+ int sum = 0 ;
32
+ for (; x > 0 ; x-= x&-x)
33
+ {
34
+ sum+= bit[x];
35
+ }
36
+ return sum;
37
+ }
38
+ int main ()
39
+ {
40
+ int q;
41
+ cin >> n >> q;
42
+ int a[n+9 ], quer[q+9 ];
43
+ for (int j=1 ;j<=n;j++)
44
+ {
45
+ cin >> a[j];
46
+ update (j,a[j]);
47
+ }
48
+ for (int j=1 ;j<=q;j++)
49
+ {
50
+ cin >> quer[j];
51
+ }
52
+ for (int j=1 ;j<=q;j++)
53
+ {
54
+ cout << query (quer[j]) << " \n " ;
55
+ }
56
+ return 0 ;
57
+ }
You can’t perform that action at this time.
0 commit comments