-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Most Humayra Khanom
authored
Nov 20, 2022
1 parent
8bd9624
commit 7a9026f
Showing
12 changed files
with
691 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
Description : BIT range update range query | ||
Time Complexity : all log(n) | ||
*/ | ||
|
||
/// Remember to use 1 based indexing | ||
//const int MX = 100005; | ||
|
||
ll query(ll *bit, int indx) | ||
{ | ||
ll sum = 0; | ||
while (indx) { | ||
sum += bit[indx]; | ||
indx -= (indx & -indx); | ||
} | ||
return sum; | ||
} | ||
|
||
void update(ll *bit, int indx, ll x) | ||
{ | ||
while (indx < MX) { | ||
bit[indx] += x; | ||
indx += (indx & -indx); | ||
} | ||
} | ||
ll B1[MX],B2[MX];//set 0 | ||
|
||
void Rupdate(int l, int r, ll v){ | ||
update(B1, l, v); | ||
update(B1, r+1, -v); | ||
update(B2, l, -((l-1)*v)); | ||
update(B2, r+1, r*v); | ||
} | ||
ll Rquery1(int p){ | ||
ll b1,b2; | ||
b1 = query(B1, p); | ||
b2 = query(B2, p); | ||
return b1 * p + b2; | ||
} | ||
ll Requery(int l,int r){ | ||
return Rquery1(r)-Rquery1(l-1); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Name : Dijoint Set with undo | ||
* Description : DisjointSet (Makes a set of sets, merge sets, set membership, no. of sets, undo last operation,size of each component) | ||
* Time Complexity : parent O(lg(N)), setUp O(lg(N)), undo O(1), | ||
*/ | ||
|
||
#define MX 10000 | ||
int rp[MX],sz[MX]; | ||
int compo; | ||
int pts[MX*2],in=0; | ||
|
||
int parent(int n){ | ||
if(rp[n]==n)return n; | ||
return rp[n]=parent(rp[n]); | ||
} | ||
|
||
// additionally storing parent which is connected to another parents | ||
void setUp(int a,int b){ | ||
a = parent(a); | ||
b = parent(b); | ||
if(a==b){ | ||
pts[++in]=-1; | ||
return; | ||
} | ||
if(sz[a]<sz[b]){ | ||
rp[a] = rp[b]; | ||
sz[b] += sz[a]; | ||
pts[++in]=a; | ||
} | ||
else{ | ||
rp[b] = rp[a]; | ||
sz[a] += sz[b]; | ||
pts[++in] = b;; | ||
} | ||
compo--; | ||
} | ||
|
||
void undo(){ | ||
if(!in) return; | ||
int n = pts[in--]; | ||
if(n!=-1) { | ||
sz[parent(rp[n])] -= sz[n]; | ||
rp[n]=n; | ||
compo++; | ||
} | ||
} | ||
|
||
void init(int n){ | ||
in=0; | ||
for(int i=0;i<=MX;i++){ | ||
rp[i]=i; | ||
sz[i]=1; | ||
} | ||
compo=n; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
Name : Sparse table(RMQ) | ||
Description : Find min/max | ||
Time Complexity : Build O(nlogn) Query O(1) | ||
*/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
//0 Indexed | ||
#define MX 10000 | ||
int spt[MX][22]; | ||
int n,ar[MX]={ 7, 2, 3, 0, 5, 10, 3, 12, 18 }; | ||
void buildST() | ||
{ | ||
for (int i = 0; i < n; i++) spt[i][0] = ar[i]; | ||
|
||
for (int j = 1; (1 << j) <= n; j++) { | ||
for (int i = 0; (i + (1 << j) - 1) < n; i++) { | ||
spt[i][j] = min(spt[i + (1 << (j - 1))][j - 1] , spt[i][j - 1]); | ||
} | ||
} | ||
} | ||
|
||
int query(int l, int r) | ||
{ | ||
if(l>r) return INT_MAX; | ||
int j = (int)log2(r - l + 1); | ||
///j = 31 - __builtin_clz(r - l+1); | ||
return min (spt[l][j], spt[r - (1 << j) + 1][j]); | ||
} | ||
|
||
// Driver program | ||
int main() | ||
{ | ||
|
||
n = 9; | ||
buildST(); | ||
cout << query(4, 7) << endl; | ||
cout << query(7, 8) << endl; | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
Name : Trie with Dynamic memory | ||
Time complexity : o((number of words)*(maximum lenght)) | ||
*/ | ||
|
||
|
||
/// Trie form shafaetsplanet | ||
struct node { | ||
bool endmark; | ||
node* next[26 + 1]; | ||
node() | ||
{ | ||
endmark = false; | ||
for (int i = 0; i < 26; i++) | ||
next[i] = NULL; | ||
} | ||
} * root; | ||
void insert(char* str, int len) | ||
{ | ||
node* curr = root; | ||
for (int i = 0; i < len; i++) { | ||
int id = str[i] - 'a'; | ||
if (curr->next[id] == NULL) | ||
curr->next[id] = new node(); | ||
curr = curr->next[id]; | ||
} | ||
curr->endmark = true; | ||
} | ||
bool search(char* str, int len) | ||
{ | ||
node* curr = root; | ||
for (int i = 0; i < len; i++) { | ||
int id = str[i] - 'a'; | ||
if (curr->next[id] == NULL) | ||
return false; | ||
curr = curr->next[id]; | ||
} | ||
return curr->endmark; | ||
} | ||
void del(node* cur) | ||
{ | ||
for (int i = 0; i < 26; i++) | ||
if (cur->next[i]) | ||
del(cur->next[i]); | ||
|
||
delete (cur); | ||
} | ||
int main() | ||
{ | ||
puts("ENTER NUMBER OF WORDS"); | ||
root = new node(); | ||
int num_word; | ||
cin >> num_word; | ||
for (int i = 1; i <= num_word; i++) { | ||
char str[50]; | ||
scanf("%s", str); | ||
insert(str, strlen(str)); | ||
} | ||
puts("ENTER NUMBER OF QUERY";); | ||
int query; | ||
cin >> query; | ||
for (int i = 1; i <= query; i++) { | ||
char str[50]; | ||
scanf("%s", str); | ||
if (search(str, strlen(str))) | ||
puts("FOUND"); | ||
else | ||
puts("NOT FOUND"); | ||
} | ||
del(root); | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
/// *** BIT O(Log(n)) space O(n) | ||
|
||
/** 1 based index | ||
* which functions has inverse function that can be solve bye BIT | ||
* it works like consucative sums but in log(n) | ||
*/ | ||
|
||
int n=SIZE; //of space; | ||
void update(int idx,int val)//adding value val to idx index | ||
{ | ||
while(idx<=n){ | ||
bitree[idx]+=val; | ||
idx+=idx&(-idx); // Last set of digit | ||
} | ||
} | ||
int query(int idx){// returns sum of [1,idx] index | ||
int sum=0; | ||
while(idx>0){ | ||
sum+=bitree[idx]; | ||
idx-=idx&(-idx); | ||
} | ||
return sum; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
/** | ||
Name : Disjoint set union find | ||
Description : Always check parent for size or anything | ||
Complexity : O(n) ~ O(log n) ~ O(1) | ||
*/ | ||
|
||
#define MX 10000 | ||
int rp[MX],sz[MX]; | ||
|
||
int parent(int n){ | ||
if(rp[n]==n)return n; | ||
return rp[n]=parent(rp[n]); | ||
} | ||
|
||
void setUp(int a,int b){ | ||
a = parent(a); | ||
b = parent(b); | ||
if(a==b) return; | ||
if(sz[a]<sz[b]){ | ||
rp[a] = rp[b]; | ||
sz[b] += sz[a]; | ||
} | ||
else{ | ||
rp[b] = rp[a]; | ||
sz[a] += sz[b]; | ||
} | ||
} | ||
|
||
void init(){ | ||
for(int i=0;i<=MX;i++) | ||
rp[i]=i,sz[i]=1; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
Description : Efficient and easy segment trees , Range [l, r) | ||
Time Complexity : O(logn) | ||
from: https://codeforces.com/blog/entry/18051 | ||
*/ | ||
|
||
const int N = 1e5; // limit for array size | ||
int n; // array size | ||
int tr[2 * N]; | ||
void build() { // build the tree | ||
for (int i = n - 1; i > 0; --i) tr[i] = tr[i<<1] + tr[i<<1|1]; | ||
} | ||
|
||
void modify(int p, int value) { // set value at position p | ||
for (tr[p += n] = value; p > 1; p >>= 1) tr[p>>1] = tr[p] + tr[p^1]; | ||
} | ||
|
||
int query(int l, int r) { // sum on interval [l, r) | ||
int res = 0; | ||
for (l += n, r += n; l < r; l >>= 1, r >>= 1) { | ||
if (l&1) res += tr[l++]; | ||
if (r&1) res += tr[--r]; | ||
} | ||
return res; | ||
} | ||
|
||
int main() { | ||
//scanf("%d", &n); | ||
int ar[]={1,3,4,5,4,5,6,4,6,4,5,6,6,5}; | ||
n=5; | ||
for (int i = 0; i < n; ++i) { | ||
tr[n+i]=ar[i]; | ||
} | ||
build(); | ||
printf("%d\n", query(0, 4));//that means [0,3] | ||
printf("%d\n", query(2, 3)); | ||
modify(0, 5); | ||
printf("%d\n", query(0, 4)); | ||
return 0; | ||
} |
Oops, something went wrong.