Skip to content

Instantly share code, notes, and snippets.

@hjroh0315
Created December 5, 2022 00:40
Show Gist options
  • Save hjroh0315/a0f800bbc0c8484e77a2d4591da0ead5 to your computer and use it in GitHub Desktop.
Save hjroh0315/a0f800bbc0c8484e77a2d4591da0ead5 to your computer and use it in GitHub Desktop.
fenwick in ruby (ig)
class Fenwick
def initialize(n)
@size=n
@data=[0]*(n+1)
end
def add(idx,v)
while idx<=@size
@data[idx]+=v
idx+=idx&-idx
end
end
def sum(idx)
res=0
while idx>0
res+=@data[idx]
idx-=idx&-idx
end
res
end
def size()
@size
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment