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

#include <bits/stdc++.h>

using namespace std;

struct point {
  int x, y;
  bool operator<(const point &a) const {
    if (x == a.x)
      return y < a.y;
    return x < a.x;
  }
};

struct data1 {
  int ind, val;
  bool operator<(const data1 &a) const { return val > a.val; }
};

set<point> points;

const int NMAX = 300000;
int l[NMAX + 5], c[NMAX + 5];
vector<data1> linie;
vector<data1> coloana;

int main() {
  int h, w, m, i, j, x, y;
  cin >> h >> w >> m;
  int solcol = 0;
  int sollin = 0;
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    point aux;
    aux.x = x;
    aux.y = y;
    l[x]++;
    c[y]++;
    points.insert(aux);
  }
  for (i = 1; i <= h; i++) {
    if (l[i] != 0) {
      data1 a;
      a.ind = i;
      a.val = l[i];
      linie.push_back(a);
    }
  }
  for (i = 1; i <= w; i++) {
    if (c[i] != 0) {
      data1 a;
      a.ind = i;
      a.val = c[i];
      coloana.push_back(a);
    }
  }
  sort(linie.begin(), linie.end());
  sort(coloana.begin(), coloana.end());
  int sol = linie[0].val + coloana[0].val;
  point aux;
  aux.x = linie[0].ind;
  aux.y = coloana[0].ind;

  solcol = coloana[0].val;
  sollin = linie[0].val;
  bool ok = true;
  if (points.find(aux) != points.end()) {
    sol--;
    ok = false;
  }
  if (ok == true) {
    cout << sol << "\\n";
    return 0;
  }
  for (i = 0; i < linie.size(); i++) {
    if (linie[i].val != sollin)
      break;

    for (j = 0; j < coloana.size(); j++) {
      if (coloana[j].val != solcol)
        break;
      int csol = linie[i].val + coloana[j].val;
      if (csol == sol)
        continue;
      point aux;
      aux.x = linie[i].ind;
      aux.y = coloana[j].ind;
      bool ok1 = true;
      if (points.find(aux) != points.end()) {
        csol--;
        ok1 = false;
      }
      if (ok1 == true) {
        cout << csol << "\\n";
        return 0;
      }
      sol = max(sol, csol);
    }
  }

  cout << sol;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct point {
  int x, y;
  bool operator<(const point &a) const {
    if (x == a.x)
      return y < a.y;
    return x < a.x;
  }
};

struct data1 {
  int ind, val;
  bool operator<(const data1 &a) const { return val > a.val; }
};

set<point> points;

const int NMAX = 300000;
int l[NMAX + 5], c[NMAX + 5];
vector<data1> linie;
vector<data1> coloana;

int main() {
  int h, w, m, i, j, x, y;
  cin >> h >> w >> m;
  int solcol = 0;
  int sollin = 0;
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    point aux;
    aux.x = x;
    aux.y = y;
    l[x]++;
    c[y]++;
    points.insert(aux);
  }
  for (i = 1; i <= h; i++) {
    if (l[i] != 0) {
      data1 a;
      a.ind = i;
      a.val = l[i];
      linie.push_back(a);
    }
  }
  for (i = 1; i <= w; i++) {
    if (c[i] != 0) {
      data1 a;
      a.ind = i;
      a.val = c[i];
      coloana.push_back(a);
    }
  }
  sort(linie.begin(), linie.end());
  sort(coloana.begin(), coloana.end());
  int sol = linie[0].val + coloana[0].val;
  point aux;
  aux.x = linie[0].ind;
  aux.y = coloana[0].ind;

  solcol = coloana[0].val;
  sollin = linie[0].val;

  if (points.find(aux) != points.end())
    sol--;
  for (i = 0; i < linie.size(); i++) {
    if (linie[i].val != sollin)
      break;

    for (j = 0; j < coloana.size(); j++) {
      if (coloana[j].val != solcol)
        break;
      int csol = linie[i].val + coloana[j].val;
      if (csol == sol)
        continue;
      point aux;
      aux.x = linie[i].ind;
      aux.y = coloana[j].ind;
      if (points.find(aux) != points.end())
        csol--;
      sol = max(sol, csol);
    }
  }

  cout << sol;
}