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

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

int main() {
  int t = 1;
  //	cin >> t;
  while (t--) {
    int r, c;
    cin >> r >> c;
    int m;
    cin >> m;
    int a, b;
    map<int, int> row;
    map<int, int> col;
    map<pair<int, int>, int> temp;
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      a = a - 1;
      b = b - 1;
      row[a]++;
      col[b]++;
      pair<int, int> p;
      p.first = a;
      p.second = b;
      temp[p] = 1;
    }
    pair<int, int> p;
    p.first = -1;
    p.second = -1;
    pair<int, int> mrow = p;
    pair<int, int> mcol = p;
    vector<int> vr;
    vector<int> vc;
    for (auto i : row) {
      if (i.second > mrow.second) {
        vr.clear();
        vr.push_back(i.first);
        mrow.first = i.first;
        mrow.second = i.second;
      }
      if (i.second == mrow.second) {
        vr.push_back(i.first);
      }
    }
    for (auto i : col) {
      if (i.second > mcol.second) {
        vc.clear();
        vc.push_back(i.first);
        mcol.first = i.first;
        mcol.second = i.second;
      }
      if (i.second == mcol.second) {
        vc.push_back(i.first);
      }
    }
    int ans = 0;
    for (int i = 0; i < vr.size(); i++) {
      int flag = 0;
      for (int j = 0; j < vc.size(); j++) {
        pair<int, int> p;
        p.first = vr[i];
        p.second = vc[j];
        int tem = mrow.second + mcol.second;
        if (temp[p] == 1) {
          flag = 1;
          tem--;
        }
        ans = max(ans, tem);
        if (flag == 0)
          break;
      }
      if (flag == 0)
        break;
    }

    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int t = 1;
  //	cin >> t;
  while (t--) {
    int r, c;
    cin >> r >> c;
    int m;
    cin >> m;
    int a, b;
    map<int, int> row;
    map<int, int> col;
    map<pair<int, int>, int> temp;
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      a = a - 1;
      b = b - 1;
      row[a]++;
      col[b]++;
      pair<int, int> p;
      p.first = a;
      p.second = b;
      temp[p] = 1;
    }
    pair<int, int> p;
    p.first = -1;
    p.second = -1;
    pair<int, int> mrow = p;
    pair<int, int> mcol = p;
    vector<int> vr;
    vector<int> vc;
    for (auto i : row) {
      if (i.second > mrow.second) {
        vr.clear();
        vr.push_back(i.first);
        mrow.first = i.first;
        mrow.second = i.second;
      }
      if (i.second == mrow.second) {
        vr.push_back(i.first);
      }
    }
    for (auto i : col) {
      if (i.second > mcol.second) {
        vc.clear();
        vc.push_back(i.first);
        mcol.first = i.first;
        mcol.second = i.second;
      }
      if (i.second == mcol.second) {
        vc.push_back(i.first);
      }
    }
    int ans = 0;
    for (int i = 0; i < vr.size(); i++) {
      for (int j = 0; j < vc.size(); j++) {
        pair<int, int> p;
        p.first = vr[i];
        p.second = vc[j];
        int tem = mrow.second + mcol.second;
        if (temp[p] == 1)
          tem--;
        ans = max(ans, tem);
      }
    }

    cout << ans << endl;
  }
}