Skip to content

Commit c3521d0

Browse files
committed
added fenwick tree
1 parent 91e80f4 commit c3521d0

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Author : Amit Kumar
2+
3+
// #include <ext/pb_ds/assoc_container.hpp>
4+
// #include <ext/pb_ds/tree_policy.hpp>
5+
6+
#include <iostream>
7+
#include <algorithm>
8+
#include <string>
9+
// #include <sstream>
10+
// #include <fstream>
11+
// #include <iomanip>
12+
#include <chrono>
13+
#include <numeric>
14+
#include <utility>
15+
#include <functional>
16+
17+
#include <bitset>
18+
#include <tuple>
19+
#include <queue>
20+
#include <stack>
21+
#include <vector>
22+
#include <array>
23+
// #include <unordered_map>
24+
// #include <unordered_set>
25+
#include <set>
26+
#include <map>
27+
// #include <deque>
28+
29+
#include <climits>
30+
#include <cstring>
31+
#include <cmath>
32+
#include <cassert>
33+
#include <cstdio>
34+
using namespace std;
35+
using namespace std::chrono;
36+
// using namespace placeholders;
37+
// using namespace __gnu_pbds;
38+
// template<typename TypeInfo>
39+
// using new_set = tree< // find_by_order & order_of_key
40+
// TypeInfo ,
41+
// null_type ,
42+
// less<TypeInfo> ,
43+
// rb_tree_tag ,
44+
// tree_order_statistics_node_update
45+
// > ;
46+
47+
void debug_out() { cerr << endl; }
48+
49+
template <typename HEAD, typename... TAIL>
50+
void debug_out(HEAD H, TAIL... T) {
51+
cerr << " " << to_string(H);
52+
debug_out(T...);
53+
}
54+
55+
#ifdef HELL_JUDGE
56+
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
57+
#else
58+
#define debug(...) 0
59+
#endif
60+
61+
62+
const long long MAXM = (long long)1e5+100;
63+
const long long MAXN = 5000100;
64+
const long long MOD = (long long)1e9+7;
65+
66+
vector<long long>ftree(MAXN);
67+
#define MSB(X) ((X)&(-X))
68+
69+
void update(long long a, long long val){
70+
a+=1;
71+
while(a<MAXN){
72+
ftree[a]+=val;
73+
a+=MSB(a);
74+
}
75+
return;
76+
}
77+
78+
long long query(long long a){
79+
long long sum=ftree[0];
80+
while(a>0){
81+
sum+=ftree[a];
82+
a-=MSB(a);
83+
}
84+
return sum;
85+
}
86+
87+
int main(void){
88+
#ifdef HELL_JUDGE
89+
freopen("input","r",stdin);
90+
freopen("output","w",stdout);
91+
freopen("error","w",stderr);
92+
#endif
93+
#ifdef HELL_JUDGE
94+
auto INITIAL_TIME = high_resolution_clock::now();
95+
#endif
96+
ios::sync_with_stdio(false);
97+
cin.tie(0);
98+
long long n,m; cin >> n >> m;
99+
while(m--){
100+
char c; cin >> c;
101+
if(c=='+'){
102+
long long a,b; cin >> a >> b;
103+
update(a , b);
104+
}else{
105+
long long a; cin >> a;
106+
cout << query(a) << '\n';
107+
}
108+
}
109+
#ifdef HELL_JUDGE
110+
auto FINAL_TIME = high_resolution_clock::now();
111+
cout << "Time : "
112+
<< duration_cast<milliseconds>(FINAL_TIME-INITIAL_TIME).count()
113+
<< " ms" << '\n';
114+
#endif
115+
return 0;
116+
}

0 commit comments

Comments
 (0)