Created
April 21, 2023 15:39
-
-
Save hjroh0315/e62e52e25096340abf741dd67a966622 to your computer and use it in GitHub Desktop.
dynamic sort-fenwick using PBDS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
#include<ext/pb_ds/assoc_container.hpp> | |
#include<ext/pb_ds/tree_policy.hpp> | |
using namespace std; | |
using namespace __gnu_pbds; | |
using ll=long long; | |
template<class T,class U> | |
using ord_map=tree<T,U,less<T>,rb_tree_tag,tree_order_statistics_node_update>; | |
template<class T,class U> | |
using ord_multimap=tree<T,U,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>; | |
template<int sz> | |
struct sf_dynamic | |
{ | |
ord_multimap<ll,null_type>fen[sz]; | |
void add(int i,ll x) | |
{ | |
while(i<sz) | |
{ | |
fen[i].insert(x); | |
i+=i&-i; | |
} | |
} | |
void erase(int i,ll x) | |
{ | |
while(i<sz) | |
{ | |
fen[i].erase(fen[i].find_by_order(fen[i].order_of_key(x))); | |
i+=i&-i; | |
} | |
} | |
ll qry_g(int i,ll x) | |
{ | |
ll ans=0; | |
while(i>0) | |
{ | |
ans+=size(fen[i])-fen[i].order_of_key(x+1); | |
i-=i&-i; | |
} | |
return ans; | |
} | |
ll qry_ge(int i,ll x) | |
{ | |
ll ans=0; | |
while(i>0) | |
{ | |
ans+=size(fen[i])-fen[i].order_of_key(x); | |
i-=i&-i; | |
} | |
return ans; | |
} | |
ll qry_l(int i,ll x) | |
{ | |
ll ans=0; | |
while(i>0) | |
{ | |
ans+=fen[i].order_of_key(x); | |
i-=i&-i; | |
} | |
return ans; | |
} | |
ll qry_le(int i,ll x) | |
{ | |
ll ans=0; | |
while(i>0) | |
{ | |
ans+=fen[i].order_of_key(x+1); | |
i-=i&-i; | |
} | |
return ans; | |
} | |
ll qry_eq(int i,ll x) | |
{ | |
ll ans=0; | |
while(i>0) | |
{ | |
ans+=fen[i].order_of_key(x+1)-fen[i].order_of_key(x); | |
i-=i&-i; | |
} | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment