Language: C++

#include<bits/stdc++.h>

using namespace std;
using   ll=long long;

#define fast        	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define endl        	"\n"
#define pb          	push_back
#define mp          	make_pair
#define F           	first
#define S           	second
#define int             long long int
#define pll				pair<int , int>

#define ALL(v)      	v.begin(),v.end()
#define ALLR(v)     	v.rbegin(),v.rend()
#define pii         	3.14159265358979323
#define inf     		LLONG_MAX
#define ones(x)     	__builtin_popcount(x)
#define fill(a,b)   	memset(a,b,sizeof(a))
#define mod 			1000000007
#define hell            998244353

ll mod_pow(ll a,ll b,ll m)
{
    ll res = 1;
    while(b)
    {
        if(b&1)
        {
        	res=(res*a) % m;
        }
        a=(a*a) % m;
        b>>=1;
    }
    return res;
}

ll mod_inverse(ll a)
{
	return mod_pow(a , mod-2 , mod);
}

struct CustomCompare
{
    bool operator()(const pair<int,int>& l, const pair<int,int>& r)
	{
	    return  l.S < r.S;
	}
};

void solve() 
{
	int n , m , p;
	
	cin >> n >> m >> p;
	
	vector<pair<int,int>> a(p);
	
	for(int i = 0; i < p; ++i) {
		cin >> a[i].F >> a[i].S;
	}
	
	sort(ALL(a));
	
	map<int,int> f;
	
	multiset<pair<int,int> , CustomCompare> mt;
	
	for(int i = 0; i < p; ++i) {
		f[a[i].S]++;
	}
	
	for(auto u : f) {
		mt.insert({u.F , u.S});
	}
	
	int mx = 0;
	
	for(int i = 0; i < p; ++i) {
		int j = i;
		
		while(j < p && a[i].F == a[j].F) {
			mt.erase(mt.find({a[j].S , f[a[j].S]}));
			mt.insert({a[j].S , f[a[j].S]-1});
			j++;
		}
		
		auto tm = mt.end();
		tm--;
		
		mx = max(mx , j - i + (*tm).S);
		
		for(int z = i; z < j; ++z) {
			mt.erase(mt.find({a[z].S , f[a[z].S]-1}));
			mt.insert({a[z].S , f[a[z].S]});
		}
		
		i = j-1;
	}
	
	cout << mx;
}

signed main() {
    fast;

    int t = 1;

	//cin >> t;

    while(t--) {
        solve();
    }

    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<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = CustomCompare; _Alloc = std::allocator<std::pair<long long int, long long int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<long long int, long long 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<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = CustomCompare; _Alloc = std::allocator<std::pair<long long int, long long int> >; key_type = std::pair<long long int, long long 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<long long int, long long int>; _Key = std::pair<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = CustomCompare; _Alloc = std::allocator<std::pair<long long int, long long int> >; iterator = std::_Rb_tree<std::pair<long long int, long long int>, std::pair<long long int, long long int>, std::_Identity<std::pair<long long int, long long int> >, CustomCompare, std::allocator<std::pair<long long int, long long 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<long long int, long long int>; _Compare = CustomCompare; _Alloc = std::allocator<std::pair<long long int, long long int> >; iterator = std::_Rb_tree<std::pair<long long int, long long int>, std::pair<long long int, long long int>, std::_Identity<std::pair<long long int, long long int> >, CustomCompare, std::allocator<std::pair<long long int, long long int> > >::const_iterator; value_type = std::pair<long long int, long long int>]'
  510 |       { return _M_t._M_insert_equal(std::move(__x)); }
      |                ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:76:12:   required from here
   76 |                 mt.insert({u.F , u.S});
      |                 ~~~~~~~~~^~~~~~~~~~~~~
/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 CustomCompare&, const std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&>' evaluates to false