Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define _                                                                      \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define ll long long int
#define eb emplace_back
#define F first
#define S second

set<pair<int, int>> se;
/*multiset<int> mtsex,mtsey;
vector<int> ansx,ansy;*/
map<int, set<int>> mpx, mpy;
map<int, int> ctx, cty;

int main() {
  _ int n, m, k, a, b;
  while (cin >> n >> m >> k) {
    int maxx = 0, maxy = 0;
    while (k--) {
      cin >> a >> b;
      se.insert({a, b});
      /*mtsex.insert(a);
      mtsey.insert(b);*/
      ctx[a]++;
      cty[b]++;
      if (ctx[a] == 1) {
        mpx[1].insert(a);
        maxx = max(maxx, 1);
      } else {
        int tmp = ctx[a];
        mpx[tmp - 1].erase(a);
        mpx[tmp].insert(a);
        maxx = max(maxx, tmp);
      }
      if (cty[b] == 1) {
        mpy[1].insert(b);
        maxy = max(maxy, 1);
      } else {
        int tmp = cty[b];
        mpy[tmp - 1].erase(b);
        mpy[tmp].insert(b);
        maxy = max(maxy, tmp);
      }
    }
    /*
    for(int i=1;i<=n;i++)
    {
            if(mtsex.count(i)>maxx)
            {
                    ansx.clear();
                    ansx.eb(i);
                    maxx=mtsex.count(i);
            }
            else if(mtsex.count(i)==maxx)
            {
                    ansx.eb(i);
            }
    }
    for(int i=1;i<=m;i++)
    {
            if(mtsey.count(i)>maxy)
            {
                    ansy.clear();
                    ansy.eb(i);
                    maxy=mtsey.count(i);
            }
            else if(mtsey.count(i)==maxy)
            {
                    ansy.eb(i);
            }
    }
    */
    int ans = 0;
    /*
    for(int i:ansx)
    {
            for(int j:ansy)
            {
                    int tmpx=mtsex.count(i);
                    int tmpy=mtsey.count(j);
                    //cout<<i<<" "<<j<<" "<<mtsex.count(i)<<"
    "<<mtsey.count(j)<<"*\\n"; int cur=tmpx+tmpy; if(se.count({i,j})) cur--;
                    anss=max(anss,cur);
            }
    }
    */
    bool minus = 1;
    for (auto it = mpx[maxx].begin(); it != mpx[maxx].end(); it++) {
      for (auto jt = mpy[maxy].begin(); jt != mpy[maxy].end(); jt++) {
        if (se.count({*it, *jt}) == 0) {
          minus = 0;
          break;
        }
      }
      if (!minus)
        break;
    }
    cout << (minus ? maxx + maxy - 1 : maxx + maxy) << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _                                                                      \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define ll long long int
#define eb emplace_back
#define F first
#define S second

set<pair<int, int>> se;
multiset<int> mtsex, mtsey;
vector<int> ansx, ansy;

int main() {
  _ int n, m, k, a, b;
  while (cin >> n >> m >> k) {
    while (k--) {
      cin >> a >> b;
      se.insert({a, b});
      mtsex.insert(a);
      mtsey.insert(b);
    }
    int maxx = 0, maxy = 0;
    for (int i = 1; i <= n; i++) {
      if (mtsex.count(i) > maxx) {
        ansx.clear();
        ansx.eb(i);
        maxx = mtsex.count(i);
      } else if (mtsex.count(i) == maxx) {
        ansx.eb(i);
      }
    }
    for (int i = 1; i <= m; i++) {
      if (mtsey.count(i) > maxy) {
        ansy.clear();
        ansy.eb(i);
        maxy = mtsey.count(i);
      } else if (mtsey.count(i) == maxy) {
        ansy.eb(i);
      }
    }
    int anss = 0;
    for (int i : ansx) {
      for (int j : ansy) {
        int tmpx = mtsex.count(i);
        int tmpy = mtsey.count(j);
        // cout<<i<<" "<<j<<" "<<mtsex.count(i)<<" "<<mtsey.count(j)<<"*\\n";
        int cur = tmpx + tmpy;
        if (se.count({i, j}))
          cur--;
        anss = max(anss, cur);
      }
    }
    cout << anss << "\\n";
  }

  return 0;
}