Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53cdc17

Browse files
committed
implemented fenwick tree in C++
1 parent f680c87 commit 53cdc17

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
45 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Fenwick Tree implemented as a solution of a problem "http://codeforces.com/contest/1066/problem/C"
2+
3+
4+
#include <bits/stdc++.h>
5+
6+
using namespace std;
7+
8+
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
9+
#define endl '\n'
10+
#define ll long long
11+
12+
const int N = 2e5 + 5;
13+
14+
class Fenwick{
15+
public:
16+
vector<int> bit = vector<int> (N, 0);
17+
18+
void add(int idx, int val){
19+
for(; idx < N && idx >= 0; idx = idx | (idx + 1))
20+
bit[idx] += val;
21+
}
22+
23+
int sum(int r){
24+
int res = 0;
25+
for(;r >= 0;r = (r & (r + 1)) -1)
26+
res += bit[r];
27+
return res;
28+
}
29+
}front, back;
30+
31+
int main(){
32+
IOS;
33+
int q;cin>>q;
34+
int cnt = 0; map<int, int> m;
35+
while(q--){
36+
char c;int id;
37+
cin>>c>>id;
38+
if(c == 'L'){
39+
m[id] = cnt++;
40+
front.add(0, 1); front.add(cnt - 1, -1);
41+
back.add(cnt - 1, cnt - 1); back.add(cnt, -1*(cnt - 1));
42+
}
43+
else if(c == 'R'){
44+
m[id] = cnt++;
45+
back.add(0, 1); back.add(cnt -1, -1);
46+
front.add(cnt - 1, cnt - 1); front.add(cnt, - 1*(cnt - 1));
47+
}
48+
else{
49+
int idx = m[id];
50+
int p1 = front.sum(idx);
51+
int p2 = back.sum(idx);
52+
cout<<min(p1, p2)<<endl;
53+
}
54+
}
55+
return 0;
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /