那些更神秘的STL
- ordered_multiset
由于set和multiset不支持随机访问,所以二分查询的速度是\(O(n)\)的
(你不可以使用st.lower_bound(value) -st.begin()这样的查询方法而是需要调用distance方法)
ordered_multiset提供了方法order_of_key(value),可以快速查找一个multiset里面的value的排名
using namespace __gnu_pbds;
struct pair_compare {
bool operator()(const pair<int,int> &a, const pair<int,int> &b) const {
if(a.first != b.first) return a.first < b.first;
return a.second < b.second;
}
};
template<typename T,typename Compare = pair_compare>
using ordered_multiset = tree<T, null_type, Compare, rb_tree_tag,
tree_order_statistics_node_update>;
[https://www.luogu.com.cn/problem/P7910]
using namespace __gnu_pbds;
struct pair_compare {
bool operator()(const pair<int,int> &a, const pair<int,int> &b) const {
if(a.first != b.first) return a.first < b.first;
return a.second < b.second;
}
};
template<typename T,typename Compare = pair_compare>
using ordered_multiset = tree<T, null_type, Compare, rb_tree_tag,
tree_order_statistics_node_update>;
typedef long long ll;
typedef __int128 lll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll llmax=LLONG_MAX;
const int M=1e5+5;
const int mod =1e9+7;
const int inf =1e18;
std::mt19937_64 rnd(std::random_device{}());
int n,q;
//3 2 2
//2 3 2
//2 2 3
void solve(){
cin>>n>>q;
vector<int>a(n+1);
rep(i,1,n)cin>>a[i];
ordered_multiset<pii>st;
rep(i,1,n){
st.insert({a[i],i});
}
while(q--){
int opt;cin>>opt;
int x;cin>>x;
if(opt==1){
int v;cin>>v;
//a[x]=v
auto it = st.find({a[x],x});
st.erase(it);
st.insert({v,x});
a[x]=v;
}else{
int p = st.order_of_key({a[x],x});
cout<<p+1<<endl;
}
}
}

浙公网安备 33010602011771号