Skip to content

Instantly share code, notes, and snippets.

@ia7ck
Last active April 1, 2017 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ia7ck/9597e3cff900c9fb8e97cda78eb899a1 to your computer and use it in GitHub Desktop.
Save ia7ck/9597e3cff900c9fb8e97cda78eb899a1 to your computer and use it in GitHub Desktop.
ARC 033-C データ構造
class BinaryIndexedTree
def initialize
@N=200_001
@bit=Array.new(@N){0}
end
def add(k, x)
while k<=@N
@bit[k]+=x
k+=(k&-k)
end
end
def sum(k)
ret=0
while k>0
ret+=@bit[k]
k-=(k&-k)
end
ret
end
end
q=gets.to_i
bt=BinaryIndexedTree.new
q.times do
t, x=gets.split.map(&:to_i)
case t
when 1
bt.add(x, 1)
when 2
l, u=0, 200_000
while u-l>1
m=(u+l)/2
bt.sum(m)<x ? l=m: u=m
end
puts u
bt.add(u, -1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment