Skip to content

Instantly share code, notes, and snippets.

View mrchnk's full-sized avatar
☮️
NO WAR, PLEASE!

Nikolay Sukharev mrchnk

☮️
NO WAR, PLEASE!
View GitHub Profile
@mrchnk
mrchnk / segment-tree.lua
Created September 20, 2021 10:44
Segment Tree (update by segment, query by index)
function SegmentTree(size, merge, zero)
local tree = {}
local function get(v, tl, tr, index)
local value = tree[v] or zero
if tl == tr then
return value
end
local tm = math.floor((tl + tr) / 2)
if index <= tm then
@mrchnk
mrchnk / segment-tree.lua
Last active January 26, 2022 19:11
Segment Tree (update by index, query by segment)
function SegmentTree(size, merge, zero)
local tree = {}
local function query(v, tl, tr, l, r)
if l == tl and r == tr then
return tree[v] or zero
end
local tm = math.floor((tl + tr) / 2)
if (r <= tm) then
return query(v * 2, tl, tm, l, r)
@mrchnk
mrchnk / binary-heap.lua
Last active September 20, 2021 13:09
Binary Heap
function BinaryHeap(cmp)
local heap = {}
local function push(el)
table.insert(heap, el)
local i = #heap
while i > 1 do
local parent = math.floor(i / 2)
if cmp(heap[parent], heap[i]) then
break
@mrchnk
mrchnk / binary-search-tree.lua
Last active September 19, 2021 13:34
Binary Search Tree
function BinarySearchTree(cmp)
local root
local size = 0
local function has(v, el)
if not v then
return false
end
if v.value == el then
return true
end
class BinaryHeap<T>
{
public T[] Heap { get; }
public int Count { get; private set; }
public IComparer<T> Comparer { get; }
public BinaryHeap(int size, IComparer<T> comparer = null)
{
Heap = new T[size];
Count = 0;
private static class SegmentTree {
private final long[] tree;
private final int size;
public SegmentTree(int size) {
this.tree = new long[size * 4];
this.size = size;
}
class SegmentTree:
def __init__(self, size, merge=min, default=0):
self.size = size
self.merge = merge
self.tree = [default] * size * 4
pass
def build(self, values):
self._build(0, 0, self.size-1, values)
import time
def timeit(f):
def _f(*args, **nargs):
begin = time.time()
r = f(*args, **nargs)
end = time.time()
print('Time %2.22f ms' % ((end - begin) * 1000))
return r
Common
-Xnovalidate
-XnoAneValidate
-Xruntime RUNTIME_DIR
set
-Xdebug
For IPA-packaging
function SegmentTree(size, merge, zero) {
const tree = [];
return {
tree,
update(index, value) {
update(0, 0, size - 1, index, value)
},
query(l, r) {
return query(0, 0, size - 1, l, r)
},