Kyopro Library
 
読み取り中…
検索中…
一致する文字列を見つけられません
sorted_multitree.hpp
[詳解]
1#include"../../kyopro_library/template.hpp"
2
3#include<ext/pb_ds/assoc_container.hpp>
4#include<ext/pb_ds/tree_policy.hpp>
5using namespace __gnu_pbds;
6template<typename T>
9 using P=pair<T,int>;
11 SortedMultiTree()=default;
12
13 /// @brief コンストラクタ
14 /// @param not_found 指定の値が見つからなかったときに返す値
15 SortedMultiTree(T not_found=-1) { this->not_found=not_found; }
16
17 /// @brief x を追加する
18 void add(T x) {
19 if(this->size()==0) {
20 this->insert(P{x,0});
21 return;
22 }
23 auto itr=this->lower_bound(P{x+1,0});
24 if(itr==this->begin()) {
25 this->insert(P{x,0});
26 return;
27 }
28 itr--;
29 if(itr->first!=x) {
30 this->insert(P{x,0});
31 return;
32 }
33 this->insert(P{x,itr->second+1});
34 }
35
36 /// @brief 最小値を返す
37 T min() {
38 if(this->empty())return not_found;
39 return*this->begin()->first;
40 }
41
42 /// @brief 最大値を返す
43 T max() {
44 if(this->empty())return not_found;
45 return*this->rbegin()->first;
46 }
47
48 /// @brief 最小値を返し、削除する
49 T pop_min() {
50 if(this->empty())return not_found;
51 auto mn=*this->begin();
52 T ret=mn.first;
53 this->erase(mn);
54 return ret;
55 }
56
57 /// @brief 最大値を返し、削除する
58 T pop_max() {
59 if(this->empty())return not_found;
60 auto mx=*this->rbegin();
61 T ret=mx.first;
62 this->erase(mx);
63 return ret;
64 }
65
66 /// @brief x が含まれているか否かを返す
67 bool contains(T x) {
68 auto itr=this->lower_bound({x,0});
69 if(itr==this->end())return false;
70 return itr->first==x;
71 }
72
73 /// @brief x の個数を返す
74 int count(T x) {
75 if(!contains(x))return 0;
76 auto itr=this->lower_bound({x+1,0});
77 itr--;
78 return itr->second+1;
79 }
80
81 /// @brief x を削除する
82 /// @brief x が含まれていたか否かを返す
83 bool discard(T x) {
84 if(!contains(x))return false;
85 auto itr=prev(this->lower_bound({x+1,0}));
86 if(itr==this->end())return false;
87 this->erase(itr);
88 return true;
89 }
90
91 /// @brief x より大きい最小の値を返す
92 T gt(T x) {
93 auto itr=this->lower_bound({x+1,0});
94 if(itr==this->end())return not_found;
95 return itr->first;
96 }
97
98 /// @brief x 以上最小の値を返す
99 T ge(T x) {
100 auto itr=this->lower_bound({x,0});
101 if(itr==this->end())return not_found;
102 return itr->first;
103 }
104
105 /// @brief x 未満最大の値を返す
106 T lt(T x) {
107 auto itr=this->lower_bound({x,0});
108 if(itr==this->begin())return not_found;
109 return(--itr)->first;
110 }
111
112 /// @brief x 以下最大の値を返す
113 T le(T x) {
114 auto itr=this->lower_bound({x+1,0});
115 if(itr==this->begin())return not_found;
116 return(--itr)->first;
117 }
118
119 /// @brief x 未満の値の個数を返す
120 int count_lt(T x) { return this->order_of_key({x,0}); }
121
122 /// @brief x 以下の値の個数を返す
123 int count_le(T x) { return this->order_of_key({x+1,0}); }
124
125 /// @brief x より大きい値の個数を返す
126 int count_gt(T x) { return this->size()-this->order_of_key({x+1,0}); }
127
128 /// @brief x 以上の値の個数を返す
129 int count_ge(T x) { return this->size()-this->order_of_key({x,0}); }
130
131 /// @brief k(0-indexed) 番目に小さい値の個数を返す
132 T kth_min(int k) { return this->find_by_order(k)->first; }
133
134 /// @brief k(0-indexed) 番目に大きい値の個数を返す
135 T kth_max(int k) { return this->find_by_order(this->size()-k-1)->first; }
136};
T min()
最小値を返す
bool contains(T x)
x が含まれているか否かを返す
T gt(T x)
x より大きい最小の値を返す
T kth_min(int k)
k(0-indexed) 番目に小さい値の個数を返す
T max()
最大値を返す
int count_le(T x)
x 以下の値の個数を返す
int count(T x)
x の個数を返す
T lt(T x)
x 未満最大の値を返す
T pop_min()
最小値を返し、削除する
SortedMultiTree()=default
T le(T x)
x 以下最大の値を返す
T kth_max(int k)
k(0-indexed) 番目に大きい値の個数を返す
int count_lt(T x)
x 未満の値の個数を返す
T pop_max()
最大値を返し、削除する
int count_gt(T x)
x より大きい値の個数を返す
SortedMultiTree(T not_found=-1)
コンストラクタ
T ge(T x)
x 以上最小の値を返す
void add(T x)
x を追加する
bool discard(T x)
x を削除する
int count_ge(T x)
x 以上の値の個数を返す