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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lowbit(a) ((a) & -(a))
#define clean(a, b) memset(a, b, sizeof(a))
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5 + 9;

int _;

//========================================================================
struct node {
  int num, val;
} x[maxn], y[maxn];
int cmp(node a, node b) { return a.val > b.val; }
// map<map<int,int>,int>mp;
map<int, map<int, int>> mp;
vector<int> vx, vy;
//========================================================================
int main() {
  int h, w, k, v, u;
  scanf("%d%d", &h, &w);
  scanf("%d", &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &u, &v);
    x[u].val++;
    y[v].val++;
    x[u].num = u;
    y[v].num = v;
    mp[u][v]++;
  }
  node maxx, maxy;
  maxx.val = 0;
  maxx.num = 0;
  maxy.val = 0;
  maxy.num = 0;
  for (int i = 1; i <= h; i++) {
    if (x[i].val > maxx.val) {
      maxx.val = x[i].val;
      maxx.num = x[i].num;
    }
  }
  for (int i = 1; i <= w; i++) {
    if (y[i].val > maxy.val) {
      maxy.val = y[i].val;
      maxy.num = y[i].num;
    }
  }
  for (int i = 1; i <= h; i++) {
    if (x[i].val == maxx.val) {
      vx.push_back(i);
    }
  }
  for (int i = 1; i <= w; i++) {
    if (y[i].val == maxy.val) {
      vy.push_back(i);
    }
  }
  int lenx = vx.size();
  int leny = vy.size();
  int flag = 0;
  for (int i = 0; i < lenx; i++) {
    for (int j = 0; j < leny; j++) {
      if (mp[vx[i]][vy[j]] == 0) {
        flag = 1;
        break;
      }
    }
  }
  int ans = maxx.val + maxy.val;
  if (!flag)
    ans--;
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lowbit(a) ((a) & -(a))
#define clean(a, b) memset(a, b, sizeof(a))
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5 + 9;

int _;

//========================================================================
struct node {
  int num, val;
} x[maxn], y[maxn];
int cmp(node a, node b) { return a.val > b.val; }
// map<map<int,int>,int>mp;
map<int, map<int, int>> mp;
vector<int> vx, vy;
//========================================================================
int main() {
  int h, w, k, v, u;
  scanf("%d%d", &h, &w);
  scanf("%d", &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &u, &v);
    x[u].val++;
    y[v].val++;
    x[u].num = u;
    y[v].num = v;
    // mp[make_pair(u,v)]++;
    mp[u][v]++;
  }
  // sort(x+1,x+1+h);
  // sort(y+1,y+1+w);
  node maxx, maxy;
  maxx.val = 0;
  maxx.num = 0;
  maxy.val = 0;
  maxy.num = 0;

  for (int i = 1; i <= h; i++) {
    if (x[i].val > maxx.val) {
      maxx.val = x[i].val;
      maxx.num = x[i].num;
    }
  }
  for (int i = 1; i <= w; i++) {
    if (y[i].val > maxy.val) {
      maxy.val = y[i].val;
      maxy.num = y[i].num;
    }
  }
  for (int i = 1; i <= h; i++) {
    if (x[i].val == maxx.val) {
      vx.push_back(i);
    }
  }
  for (int i = 1; i <= w; i++) {
    if (y[i].val == maxy.val) {
      vy.push_back(i);
    }
  }
  int lenx = vx.size();
  int leny = vy.size();
  int flag = 0;
  for (int i = 0; i < lenx; i++) {
    for (int j = 0; j < leny; j++) {
      if (mp[vx[i]][vy[j]] == 0)
        flag = 1;
    }
  }
  int ans = maxx.val + maxy.val;
  if (!flag)
    ans--;
  // printf("%d %d | %d %d\\n",maxx.val,maxx.num,maxy.val,maxy.num);

  // // if(mp[make_pair(maxx.num,maxy.num)]) ans--;
  // if(mp[maxx.num][maxy.num]) ans--;
  printf("%d\\n", ans);

  return 0;
}