Language: C++

#include<bits/stdc++.h>
using namespace std ;

// Template START
#define endl "\n"
#define async ios_base::sync_with_stdio(false); cin.tie(NULL);
#define Int long long int
#define all(y) y.begin(), y.end()
#define present( x, y ) (x.find( y ) != x.end())
const int mod = (int)1e9 + 7 ;
template<class T>
auto matrix( int r, int c, T v ){
    return vector<vector<T>>( r, vector<T>( c, v ) ) ;
}
template<class T>
auto matrix( int o1, int o2, int o3, T v ){ 
    return vector<vector<vector<T>>>( o1, vector<vector<T>>( o2, vector<T>( o3, v ) ) ) ;
}
#define v vector
int power( int x, int n ){
    if( n == 0 ) return 1 ;
    else if( n & 1 ) return ( x * power( (x*x) % mod, n / 2 ) ) % mod ;
    return power( (x*x) % mod, n / 2 ) % mod ;
}
template<class T> void __print( v<T> &arr ){
    int n = arr.size() ;
    cerr << "[ " ;
    for( int i = 0 ; i < n ; i++ ) {
        cerr << arr[i] ;
        if( i != n - 1 ) cerr << ", " ;
    }
    cerr << " ]" << endl ;
}
template<class T> void __print( T arr[], int n ){ 
    cerr << "[ " ;
    for( int i = 0 ; i < n ; i++ ) {
        cerr << arr[i] ;
        if( i != n - 1 ) cerr << ", " ;
    }
    cerr << " ]" << endl ;
}
#define print(x) cerr << "{ " << #x << " : " << x << " }" ;
#define println(x) cerr << "{ " << #x << " : " << x << " }" ;
// Template END
const int mx = (int)2e5 + 1 ;
class compare {
    // multiset<dataType, compare>
    // priority_queue<dataType, vector<dataType>, compare>
    public :
    // first - ratings, second - number
    bool operator()( const pair<int,int> &a, const pair<int,int> &b ){
        return a.first > b.first ;
    }
} ;

signed main(){
    int n, q ; cin >> n >> q ;
    vector< pair<int,int> > ratings( n + 1 ) ;  // first > rating, second > kindergarten
    for( int i = 1 ; i <= n ; i++ ){
        cin >> ratings[i].first >> ratings[i].second ;
    }
    v<  multiset< pair<int,int>, compare >  > ms( mx ) ;
    for( int i = 1 ; i <= n ; i++ ){
        ms[ratings[i].second].insert( { ratings[i].first, i } ) ;
    }
    multiset<pair<int,int>> overall ;
    for( int i = 1 ; i < mx ; i++ ){
        if( !ms[i].empty() ){
            overall.insert( *ms[i].begin() ) ;
        }
    }
    int a, b ;
    for( int i = 0 ; i < q ; i++ ){
        cin >> a >> b ;
        // infant a move to kg b
        int init_kg = ratings[a].second ;
        int init_rt = ratings[a].first ;
        auto oldKing1 = *ms[init_kg].begin() ;
        ms[init_kg].erase( { init_rt, a } ) ;
        auto newKing1 = make_pair( -1, -1 ) ;
        if( !ms[init_kg].empty() ) 
            newKing1 = *ms[init_kg].begin() ;
        if( newKing1.second == -1 && newKing1.first == -1 ){
            overall.erase( oldKing1 ) ;
        }
        else if( oldKing1 == newKing1 ){
            // a was not king
        }
        else{
            overall.erase( oldKing1 ) ;
            overall.insert( newKing1 ) ;
        }
        auto oldKing2 = make_pair( -1, -1 ) ;
        if( !ms[b].empty() )
            oldKing2 = *ms[b].begin() ;
        ms[b].insert( { init_rt, a } ) ;
        auto newKing2 = *ms[b].begin() ;
        if( oldKing2.first == -1 && oldKing2.second == -1 ){
            overall.insert( newKing2 ) ;
        }
        else if( oldKing2 == newKing2 ){
            // do nothing
        }
        else{
            overall.erase( oldKing2 ) ;
            overall.insert( newKing2 ) ;
        }
        ratings[a].second = b ;
        cout << (*overall.begin()).first << endl ;
    }
    return 0 ;   
}          

Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = compare; _Alloc = std::allocator<std::pair<int, int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<int, int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2147:44:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_equal_pos(const key_type&) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = compare; _Alloc = std::allocator<std::pair<int, int> >; key_type = std::pair<int, int>]'
 2147 |           __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
      |                                             ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2196:4:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_Arg&&) [with _Arg = std::pair<int, int>; _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = compare; _Alloc = std::allocator<std::pair<int, int> >; iterator = std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, compare, std::allocator<std::pair<int, int> > >::iterator]'
 2196 |         = _M_get_insert_equal_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:510:36:   required from 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = std::pair<int, int>; _Compare = compare; _Alloc = std::allocator<std::pair<int, int> >; iterator = std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, compare, std::allocator<std::pair<int, int> > >::const_iterator; value_type = std::pair<int, int>]'
  510 |       { return _M_t._M_insert_equal(std::move(__x)); }
      |                ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:64:37:   required from here
   64 |         ms[ratings[i].second].insert( { ratings[i].first, i } ) ;
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const compare&, const std::pair<int, int>&, const std::pair<int, int>&>' evaluates to false