Created
December 5, 2022 00:40
-
-
Save hjroh0315/a0f800bbc0c8484e77a2d4591da0ead5 to your computer and use it in GitHub Desktop.
fenwick in ruby (ig)
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
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