Skip to content

Commit d4a8940

Browse files
authored
Fenwick Tree
1 parent f573fec commit d4a8940

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

Fenwick_Tree.cpp

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
#include <ext/pb_ds/assoc_container.hpp>
7+
#include <ext/pb_ds/tree_policy.hpp>
8+
using namespace std;
9+
using namespace __gnu_pbds;
10+
typedef long long ll ;
11+
typedef vector<ll> vl;
12+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
13+
/* Abbrevations */
14+
#define ff first
15+
#define ss second
16+
#define mp make_pair
17+
#define line cout<<endl;
18+
#define pb push_back
19+
#define Endl "\n"
20+
// loops
21+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
22+
// Some print
23+
#define no cout<<"NO"<<endl;
24+
#define yes cout<<"YES"<<endl;
25+
// sort
26+
#define all(V) (V).begin(),(V).end()
27+
#define srt(V) sort(all(V))
28+
#define srtGreat(V) sort(all(V),greater<ll>())
29+
// some extra
30+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
31+
#define sz(V) ll(V.size())
32+
// template
33+
template <typename T>
34+
T mymax(T x,T y)
35+
{
36+
return (x>y)?x:y;
37+
}
38+
// function
39+
ll power(ll x,ll y,ll mod)
40+
{
41+
ll res=1;
42+
// x=x%mod;
43+
while(y>0)
44+
{
45+
if(y%2==1)
46+
{
47+
res*=x;
48+
// res=res%mod;
49+
}
50+
y/=2; x*=x; // x=x%mod;
51+
}
52+
return res;
53+
}
54+
ll str_to_num(string s)
55+
{
56+
return stoi(s);
57+
}
58+
59+
string num_to_str(ll num)
60+
{
61+
return to_string(num);
62+
}
63+
// datatype definination
64+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
65+
class Point
66+
{
67+
public:
68+
ll x;
69+
ll y;
70+
ll z;
71+
ll getsum()
72+
{
73+
return x+y+z;
74+
}
75+
};
76+
/* ascii value
77+
A=65,Z=90,a=97,z=122
78+
*/
79+
/* -----------------------------------------------------------------------------------*/
80+
// to run ctrl+b
81+
ll getsum(vector<ll> v,vector<ll>& FenwickTree,ll index)
82+
{
83+
ll sum=0;
84+
while(index>0)
85+
{
86+
cout<<"index is "<<index<<endl;
87+
sum+=FenwickTree[index];
88+
index-=(index&(-index));
89+
}
90+
return sum;
91+
}
92+
93+
void update(vector<ll>& FenwickTree,ll index,ll n,ll value)
94+
{
95+
while(index<=n)
96+
{
97+
FenwickTree[index]+=value;
98+
index+=(index&(-index));
99+
}
100+
}
101+
ll solve()
102+
{
103+
cout<<"\tuse zero based indexing for query"<<endl;
104+
cout<<"size of array "<<endl;
105+
ll n,q,l,a,b;
106+
cin>>n;
107+
vector<ll> v(n,0);
108+
vector<ll> FenwickTree(n+1,0);
109+
cout<<"Enter element of array "<<endl;
110+
for(ll i=0;i<n;i++)
111+
{
112+
cin>>v[i];
113+
update(FenwickTree,i+1,n,v[i]);
114+
}
115+
cout<<"No of query "<<endl;
116+
cin>>q;
117+
while(q--)
118+
{
119+
cout<<"1.For update and 2. for sum"<<endl;
120+
cin>>l;
121+
if(l==1)
122+
{
123+
cout<<"Enter position and value "<<endl;
124+
ll pos=0,value;
125+
cin>>pos>>value;
126+
pos++;
127+
update(FenwickTree,pos,n,value);
128+
}
129+
else
130+
{
131+
cout<<"Sum upto:"<<endl;
132+
cin>>a;
133+
a++;
134+
ll sum=getsum(v,FenwickTree,a);
135+
cout<<"Required sum is "<<sum<<endl;
136+
}
137+
}
138+
return 0;
139+
}
140+
141+
int main()
142+
{
143+
speed;
144+
/* #ifndef ONLINE_JUDGE
145+
freopen("input.txt","r",stdin);
146+
freopen("output.txt","w",stdout);
147+
#endif */
148+
ll TestCase=1;
149+
// cin>>TestCase;
150+
while(TestCase--)
151+
{
152+
solve();
153+
}
154+
}
155+
/* stuff you should look before submission
156+
* int overflow
157+
* special test case (n=0||n=1||n=2)
158+
* don't get stuck on one approach if you get wrong answer
159+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy