Skip to content

Instantly share code, notes, and snippets.

@orendon
Last active September 11, 2021 04:07
Show Gist options
  • Save orendon/e484c9cf01b48dfd9fabb7128dd2386b to your computer and use it in GitHub Desktop.
Save orendon/e484c9cf01b48dfd9fabb7128dd2386b to your computer and use it in GitHub Desktop.
DSA library
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;
}
};
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