Fenwick Tree (Binary Indexed Tree)
Last active
September 11, 2021 04:07
-
-
Save orendon/e484c9cf01b48dfd9fabb7128dd2386b to your computer and use it in GitHub Desktop.
DSA library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Fenwick { | |
int n; | |
vector<int> arr; | |
vector<int> bit; | |
Fenwick(int size): n(size) { | |
arr = vector<int>(n); | |
bit = vector<int>(n); | |
} | |
int qry(int pos) { | |
int sum = 0; | |
while(pos) { | |
sum += bit[pos]; | |
pos -= pos & (-pos); | |
} | |
return sum; | |
} | |
void upd(int i, int val) { | |
int pos = i; | |
while(pos < n) { | |
bit[pos] += val - arr[i]; | |
pos += pos & (-pos); | |
} | |
arr[i] = val; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct TrieNode{ | |
char val; | |
bool end; | |
map<char, TrieNode*> children; | |
TrieNode(char c): val(c), end(false) {} | |
void insert(string word){ | |
auto temp = this; | |
for(char c: word){ | |
if(!temp->children.count(c)){ | |
temp->children[c] = new TrieNode(c); | |
} | |
temp = temp->children[c]; | |
} | |
temp->end = true; | |
} | |
bool match(string word){ | |
auto temp = this; | |
for(char c: word) { | |
if(!temp->children.count(c)) { | |
if(isupper(c)) return false; | |
continue; | |
} | |
temp = temp->children[c]; | |
} | |
return temp->end; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment