diff --git a/structure/policy_tree.cpp b/structure/policy_tree.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d93aca40959759050e131f8712062bf2d29d4a18 --- /dev/null +++ b/structure/policy_tree.cpp @@ -0,0 +1,33 @@ +/** + * Policy Tree + * + * Complexity (Time): + * insert -> O(log n) + * erase -> O(log n) + * find_by_order -> O(log n) + * order_of_key -> O(log n) + * Complexity (Space): O(n) + */ + +#include <ext/pb_ds/assoc_container.hpp> +#include <ext/pb_ds/tree_policy.hpp> + +using namespace __gnu_pbds; + +typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> set_t; + +void operations() { + set_t S; + + // Insert element in S + S.insert(x); + + // Remove element from S + S.erase(x); + + // Returns iterator to the k-th largest element (counting from zero) + int pos = *S.find_by_order(k); + + // Returns the number of items strictly smaller than x + int ord = S.order_of_key(x) +}