差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
次のリビジョン両方とも次のリビジョン
programming_algorithm:data_structure:balancing_binary_search_tree:tree_free [2020/06/29] – [代替方法一覧] ikatakosprogramming_algorithm:data_structure:balancing_binary_search_tree:tree_free [2021/09/04] ikatakos
行 50: 行 50:
   * 値 $x$ を追加 → $bit.add(x, 1)$   * 値 $x$ を追加 → $bit.add(x, 1)$
   * 値 $x$ を削除 → $bit.add(x, -1)$   * 値 $x$ を削除 → $bit.add(x, -1)$
-  * $k$ 番目の値を取得 → 累積和が $k$ 以上になる最小のindexを取得+  * $k$ 番目の値を取得 → $bit.lower\_bound(k)$(累積和が $k$ 以上になる最小のindexを取得) 
 +  * $x$ 以上の最小の値を取得 $bit.lower\_bound(bit.sum(x - 1) + 1)$
  
 ^機能  ^計算量      ^ ^機能  ^計算量      ^
行 56: 行 57:
 |erase |$O(\log{N})$| |erase |$O(\log{N})$|
 |upper_bound \\ lower_bound|$O(\log{N})$| |upper_bound \\ lower_bound|$O(\log{N})$|
 +
 +=== 例 ===
 +
 +            累積和               std::set
 +  初期状態   1 2 3 4 5 6 7 8
 +            [0 0 0 0 0 0 0 0]    {}
 +  5を追加
 +            [0 0 0 0 1 1 1 1]    {5}
 +  3を追加
 +            [0 0 1 1 2 2 2 2]    {3, 5}
 +  小さい方から2番目を取得
 +                                                累積和で"2"が始まるiは「5」
 +            [0 0 1 1 2 2 2 2]    {3, 5}
 +  2,3,7を追加
 +            [0 1 3 3 4 4 5 5]    {2, 3, 3, 5, 7}
 +  6以上の最小の値を取得
 +            [0 1 3 3 4 4 5 5]    {2, 3, 3, 5, 7}
 +                                                累積和で 6-1=5 の箇所は"4"
 +                                                累積和で 4+1=5 が始まるiは「7」
 +  7を削除
 +            [0 1 3 3 4 4 4 4]    {2, 3, 3, 5}
 +  6以上の最小の値を取得
 +            [0 1 3 3 4 4 4 4]    {2, 3, 3, 5}
 +                                                この場合、「9」が返る点には注意
 +                                                  (BITの実装にも依るが)
 +
  
 === バリエーション === === バリエーション ===
行 147: 行 174:
 ++++ ++++
  
-=== std::set解 ===+=== std::set解(想定解) ===
  
 std::setであれば、値の小さい順に「要素のindex」をinsertしていく方法が考えられる。 std::setであれば、値の小さい順に「要素のindex」をinsertしていく方法が考えられる。
programming_algorithm/data_structure/balancing_binary_search_tree/tree_free.txt · 最終更新: 2024/04/30 by ikatakos
CC Attribution 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0