Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Hacktoberfest beginning!!


3
123 changes: 123 additions & 0 deletions c++/persistent_segtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <bits/stdc++.h>
#define MAXN 500050

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

struct node
{
int val;
node *child[2];
node()
{
val = 0;
for(int i = 0; i < 2; i++)
child[i] = NULL;
}
};

node *version[MAXN];

void initialize()
{
version[0] = new node();
version[0]->child[0] = version[0];
version[0]->child[1] = version[0];
}

node* insert(node *prev, unsigned val, int bitcount)
{
node *root = new node();
node *temp = root;
for(int i = bitcount; i >= 0; i--)
{
root->val = prev->val + 1;
int loc = (val >> i) & 1;
int opposite = 1 - loc;
root->child[opposite] = prev->child[opposite];
root->child[loc] = new node();
root = root->child[loc];
prev = prev->child[loc];
}
root->val = prev->val + 1;
return temp;
}

int maxXOR(node *curr, node *prev, int x, int bitcount)
{
int result = 0;
for(int i = bitcount; i >= 0; i--)
{
int loc = (x >> i) & 1;
int opposite = 1 - loc;
int count = (curr->child[opposite]->val) - (prev->child[opposite]->val);
if(count == 0)
{
curr = curr->child[loc];
prev = prev->child[loc];
}
else
{
curr = curr->child[opposite];
prev = prev->child[opposite];
result |= (1 << i);
}
}
return result ^ x;
}

int kthSmallest(node *curr, node *prev, int k, int bitcount)
{
int result = 0;
for(int i = bitcount; i >= 0; i--)
{
int count = curr->child[0]->val - prev->child[0]->val;
if(k > count)
{
curr = curr->child[1];
prev = prev->child[1];
result |= (1 << i);
k -= count;
}
else
{
curr = curr->child[0];
prev = prev->child[0];
}
}
return result;
}

int smallerThanX(node *curr, node *prev, int x, int bitcount)
{
int result = 0;
for(int i = bitcount; i >= 0; i--)
{
int loc = (x >> i) & 1;
int opposite = 1 - loc;
if(loc == 1)
{
int left = curr->child[0]->val - prev->child[0]->val;
result += left;
curr = curr->child[1];
prev = prev->child[1];
}
else
{
curr = curr->child[0];
prev = prev->child[0];
}
}
int temp = curr->val - prev->val;
return result + temp;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
initialize();
return 0;
}
81 changes: 81 additions & 0 deletions c++/rangeupdate_segtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <bits/stdc++.h>
#define MAXN 500050

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int segtree[MAXN], lazy[MAXN];

void build_tree(vector<int> &a, int loc, int low, int high)
{
if(low == high)
{
segtree[loc] = a[low];
return;
}
int mid = (low + high) >> 1;
int left = loc << 1, right = left + 1;
build_tree(a, left, low, mid);
build_tree(a, right, mid + 1, high);
segtree[loc] = segtree[left] + segtree[right];
}

void update_tree(int loc, int low, int high, int left, int right, int val)
{
if(lazy[loc] != 0)
{
int l = loc << 1, r = l + 1;
segtree[loc] += (high - low + 1) * lazy[loc];
if(low != high)
{
lazy[l] += lazy[loc];
lazy[r] += lazy[loc];
}
lazy[loc] = 0;
}
if(left > high || right < low)
return;

if(left <= low && right >= high)
{
int l = loc << 1, r = l + 1;
segtree[loc] += (high - low + 1) * val;
if(low != high)
{
lazy[l] += val;
lazy[r] += val;
}
return;
}
int mid = (low + high) >> 1, l = loc << 1, r = l + 1;
update_tree(l, low, mid, left, right, val);
update_tree(r, mid + 1, high, left, right, val);
segtree[loc] = segtree[l] + segtree[r];
}

int query_tree(int loc, int low, int high, int left, int right)
{
if(lazy[loc] != 0)
{
int l = loc << 1, r = l + 1;
segtree[loc] += (high - low + 1) * lazy[loc];
if(low != high)
{
lazy[l] += lazy[loc];
lazy[r] += lazy[loc];
}
lazy[loc] = 0;
}
if(left > high || right < low)
return 0;
if(left <= low && right >= high)
return segtree[loc];
int mid = (low + high) >> 1, l = loc << 1, r = l + 1;
return query_tree(l, low, mid, left, right) + query_tree(r, mid + 1, high, left, right);
}

int main()
{
return 0;
}