From 79ef71926c7f43b898b253c13079ef0ffe26f01e Mon Sep 17 00:00:00 2001 From: fmkiotheka <fmkiotheka@inf.ufpr.br> Date: Fri, 20 Jan 2023 01:06:20 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20busca=20bin=C3=A1ria=20na=20=C3=A1rv?= =?UTF-8?q?ore=20de=20Fenwick?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fontes/fenwick-tree.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fontes/fenwick-tree.h b/fontes/fenwick-tree.h index bebaeef..5b11196 100644 --- a/fontes/fenwick-tree.h +++ b/fontes/fenwick-tree.h @@ -18,3 +18,17 @@ void add_inclusive(int a, int b, ll d) { add(a, d); add(b + 1, -d); } + +int lower_bound(ll t) { + ll sum = 0; int pos = 0; + for (int p = log2(N); p >= 0; p--) { + if ( + pos + (1 << p) < N + && sum + bit[pos + (1 << p)] < t + ) { + sum += bit[pos + (1 << p)]; + pos += (1 << p); + } + } + return pos; +} -- GitLab