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 d6024ca

Browse files
authored
Merge pull request fnplus#604 from krritik/ritik_fenwick
Impelmented Fenwick Tree in C++
2 parents 954e69a + 53cdc17 commit d6024ca

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
-12.9 KB
Binary file not shown.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
const int S = 2;
6+
const int MOD = 1000000000+7;
7+
const int MODQ = 1000000000+7-1;
8+
9+
struct mat_t {
10+
int m[S][S];
11+
};
12+
13+
// Function to calculate matrix multiplication
14+
mat_t mat_mult(mat_t& a, mat_t& b) {
15+
mat_t c;
16+
17+
for(int i=0; i<S; i++)
18+
for(int j=0; j<S; j++)
19+
c.m[i][j] = 0;
20+
21+
for(int i=0; i<S; i++) {
22+
for(int k=0; k<S; k++) {
23+
for(int j=0; j<S; j++) {
24+
c.m[i][j] = (c.m[i][j]+ ll(a.m[i][k])*b.m[k][j] % MODQ) % MODQ;
25+
}
26+
}
27+
}
28+
29+
return c;
30+
}
31+
32+
33+
// Calculation Matrix Power in O(log(n)) ie A^n(where A is matrix)
34+
// same as exponentiation power
35+
mat_t mat_pow(mat_t a, int n) {
36+
mat_t w;
37+
38+
for(int i=0; i<S; i++) {
39+
for(int j=0; j<S; j++)
40+
w.m[i][j] = i==j;
41+
}
42+
43+
44+
while(n) {
45+
if(n&1) w = mat_mult(w, a);
46+
a = mat_mult(a, a);
47+
n/=2;
48+
}
49+
50+
return w;
51+
}
52+
53+
54+
int main() {
55+
int n; cin >> n;
56+
57+
58+
mat_t m;
59+
m.m[0][0] = m.m[0][1] = m.m[1][0] = 1;
60+
m.m[1][1] = 0;
61+
62+
m = mat_pow(m, n);
63+
cout << m.m[0][1] << endl;
64+
}
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 によって変換されたページ (->オリジナル) /