Skip to content
Snippets Groups Projects
Commit ac9b7cf4 authored by Fernando K's avatar Fernando K
Browse files

Adiciona k-elements

parent 5cbfb8f3
No related branches found
No related tags found
No related merge requests found
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T, class U = less<T>>
using ord_set = tree<T, null_type, U, rb_tree_tag,
tree_order_statistics_node_update>;
template <class C = less<>>
struct k_elements {
int k; ord_set<pair<ll, int>, C> s; ll sum;
k_elements(int k) : k(k), sum(0) {};
void insert(int key, ll value) {
int l = s.order_of_key(make_pair(value, key));
s.insert(make_pair(value, key));
if (l >= k) { return; }
sum += value;
if (s.size() > k)
sum -= s.find_by_order(k)->first;
}
void remove(int key, ll value) {
if (s.order_of_key(make_pair(value, key)) < k) {
sum -= value;
if (s.size() > k)
sum += s.find_by_order(k)->first;
}
s.erase(s.find(make_pair(value, key)));
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment