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

#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
// #include <functional>
#include <cmath>
#include <stack>
#include <string>
#define fi first
#define se second
#define re register
#define ls i << 1
#define rs i << 1 | 1
#define pb push_back
#define pii pair<int, int>
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007

#define int long long

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);

inline int rd() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

void out(int a) {
  if (a < 0)
    putchar('-'), a = -a;
  if (a >= 10)
    out(a / 10);
  putchar(a % 10 + '0');
}
int H, W, M;
const int maxn = 3e5 + 10;
int h[maxn], w[maxn];
int hh[maxn], ww[maxn];
// int aa[maxn], bb[maxn];
vector<int> hpos, wpos;
// struct node{
//   int x, y;
//   node(int X, int Y){
//     x = X, y = Y;
//   }
//   bool friend operator<(const node &a, const node &b) {
//     return a.x == b.x && a.y == b.y;
//   }
// };
// map<node, int> mp;
set<int> st[maxn];
signed main() {
  // int main() {
  H = rd(), W = rd(), M = rd();
  int maxh = 0, maxw = 0;
  for (int i = 1; i <= M; i++) {
    h[i] = rd(), hh[h[i]]++, w[i] = rd(), ww[w[i]]++;
    //    printf("h[i] = %lld w[i] = %lld\\n",h[i],w[i]);
    //    mp[node(h[i],w[i])] = 1;
    st[h[i]].insert(w[i]);
    maxh = max(maxh, hh[h[i]]);
    maxw = max(maxw, ww[w[i]]);
  }
  for (int i = 1; i <= H; i++) {
    if (hh[i] == maxh)
      hpos.push_back(i);
  }
  for (int i = 1; i <= W; i++) {
    if (ww[i] == maxw)
      wpos.push_back(i);
  }
  //  cout << mp[node(1,5)] << " debug\\n";
  int ans = 0;
  int flag = 0;
  for (auto i : hpos) {
    if (flag)
      break;
    for (auto j : wpos) {
      //      cout << "i = " << i << " j = " << j << '\\n';
      //      printf("hh[i] = %lld ww[j] = %lld\\n",hh[i],ww[j]);
      //      printf("mp[node(%lld,%lld)] = ",i,j);
      //      cout << mp[node(i,j)] << '\\n';
      int now = hh[i] + ww[j] - (st[i].find(j) != st[i].end());
      if (ans <= now) {
        ans = now;
      } else {
        flag = 1;
        break;
      }
      //      ans = max(ans, hh[i] + ww[j]);
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
// #include <functional>
#include <cmath>
#include <stack>
#include <string>
#define fi first
#define se second
#define re register
#define ls i << 1
#define rs i << 1 | 1
#define pb push_back
#define pii pair<int, int>
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007

#define int long long

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);

inline int rd() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

void out(int a) {
  if (a < 0)
    putchar('-'), a = -a;
  if (a >= 10)
    out(a / 10);
  putchar(a % 10 + '0');
}
int H, W, M;
const int maxn = 3e5 + 10;
int h[maxn], w[maxn];
int hh[maxn], ww[maxn];
// int aa[maxn], bb[maxn];
vector<int> hpos, wpos;
// struct node{
//   int x, y;
//   node(int X, int Y){
//     x = X, y = Y;
//   }
//   bool friend operator<(const node &a, const node &b) {
//     return a.x == b.x && a.y == b.y;
//   }
// };
// map<node, int> mp;
set<int> st[maxn];
signed main() {
  // int main() {
  H = rd(), W = rd(), M = rd();
  int maxh = 0, maxw = 0;
  for (int i = 1; i <= M; i++) {
    h[i] = rd(), hh[h[i]]++, w[i] = rd(), ww[w[i]]++;
    //    printf("h[i] = %lld w[i] = %lld\\n",h[i],w[i]);
    //    mp[node(h[i],w[i])] = 1;
    st[h[i]].insert(w[i]);
    maxh = max(maxh, hh[h[i]]);
    maxw = max(maxw, ww[w[i]]);
  }
  for (int i = 1; i <= H; i++) {
    if (hh[i] == maxh)
      hpos.push_back(i);
  }
  for (int i = 1; i <= W; i++) {
    if (ww[i] == maxw)
      wpos.push_back(i);
  }
  //  cout << mp[node(1,5)] << " debug\\n";
  int ans = 0;
  for (auto i : hpos) {
    for (auto j : wpos) {
      //      cout << "i = " << i << " j = " << j << '\\n';
      //      printf("hh[i] = %lld ww[j] = %lld\\n",hh[i],ww[j]);
      //      printf("mp[node(%lld,%lld)] = ",i,j);
      //      cout << mp[node(i,j)] << '\\n';
      ans = max(ans, hh[i] + ww[j] - (st[i].find(j) != st[i].end()));
      //      ans = max(ans, hh[i] + ww[j]);
    }
  }
  cout << ans << '\\n';
  return 0;
}