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



#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\\n"
typedef long long LL;
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define mod2 998244353
#define INFINITE 1000000000000000001;
#define all(c) c.begin(), c.end()
#define sz(v) ((ll)(v).size())
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define dbg(a) cout << #a << " --> " << (a) << "\\n";
#define forn(i, n) for (ll i = 0; i < (n); ++i)
#define fornd(i, n) for (ll i = (n)-1; i >= 0; --i)
#define forab(i, a, b) for (ll i = (a); i <= (b); ++i)
#define read(a)                                                                \\
  ll a;                                                                        \\
  cin >> a;
#define reads(s)                                                               \\
  string s;                                                                    \\
  cin >> s;
#define readb(a, b)                                                            \\
  ll a, b;                                                                     \\
  cin >> a >> b;
#define readc(a, b, c)                                                         \\
  ll a, b, c;                                                                  \\
  cin >> a >> b >> c;
#define readarr(a, n)                                                          \\
  int a[(n) + 1] = {};                                                         \\
  forab(i, 0, (n - 1)) { cin >> a[i]; }
#define readmat(a, n, m)                                                       \\
  int a[n + 1][m + 1] = {};                                                    \\
  forab(i, 1, n) { forab(j, 1, m) cin >> a[i][j]; }
#define readv(A, n)                                                            \\
  vll A(n, 0);                                                                 \\
  forn(i, n) cin >> A[i];
#define print(a) cout << a << endl;
#define printarr(a, n)                                                         \\
  forab(i, 1, n) cout << a[i] << " ";                                          \\
  cout << endl;
#define printv(v)                                                              \\
  for (int i : v)                                                              \\
    cout << i << " ";                                                          \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  forab(i, 1, n) {                                                             \\
    forab(j, 1, m) cout << a[i][j] << " ";                                     \\
    cout << endl;                                                              \\
  }

#define INF (1e18 + 5)
#define inf (1e9 + 5)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
using namespace std;
bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); }
int takemod(int x) { return (x % mod + mod) % mod; }

int fpow(int base, int power, int MOD) {
  int result = 1;
  base = base % MOD;
  while (power) {
    if (power & 1)
      result = (result * base) % MOD;
    base = (base * base) % MOD;
    power = power >> 1;
  }
  return result;
}

int modinv(int x) { return fpow(x, mod - 2, mod); }
void solve() {
  ll n, m;
  cin >> n >> m;
  ll b;
  cin >> b;
  vll row(300010, 0);
  vll col(300010, 0);
  vector<pll> tars(b);
  forn(i, b) {
    cin >> tars[i].f >> tars[i].s;
    row[tars[i].f]++;
    col[tars[i].s]++;
    // pq.insert({tars[i].f,tars[i].s});
  }
  ll ans = 0;
  sort(all(tars));
  vll xs, ys;
  ll x = 0, y = 0, er = 0;
  forn(i, 300010) { er = max(er, row[i]); }
  forn(i, 300010) {
    if (er == row[i]) {
      xs.pb(i);
    }
  }
  ll temp = er;
  er = 0;
  forn(i, 300010) {
    er = max(er, col[i]);
    if (er + temp == n + m) {
      cout << n + m - 1 << endl;
      return;
    }
  }
  y = er;
  forn(i, 300010) {
    if (er == col[i]) {
      ys.pb(i);
      // break;
    }
  }

  ll sx = 0, sy = 0;
  forn(i, sz(xs)) {
    forn(j, sz(ys)) {
      if (*lower_bound(all(tars), mp(xs[i], ys[j])) == mp(xs[i], ys[j])) {

        ans = max(ans, row[xs[i]] + col[ys[j]] - 1);

      } else
        ans = max(ans, row[xs[i]] + col[ys[j]]);
      if (ans == n + m - 1 || temp + er == ans) {
        cout << ans << endl;
        return;
      }
      // if(ans<row[xs[i]]+col[ys[j]])
      // {
      // 	sx=xs[i];
      // 	sy=ys[j];
      // }
    }
  }
  // dbg(ans)
  // dbg(sx)
  // dbg(sy)
  // ll res=1;
  // forn(i,b)
  // {
  // 	if(tars[i]==mp(sx,sy))
  // 	{
  // 		//res=0;
  // 		ans--;
  // 		break;
  // 	}

  // }
  // dbg(res)
  // if(res)ans--;
  cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast_io;
  int T = 1;
  // cin>>T;
  //  cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:



#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\\n"
typedef long long LL;
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define mod2 998244353
#define INFINITE 1000000000000000001;
#define all(c) c.begin(), c.end()
#define sz(v) ((ll)(v).size())
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define dbg(a) cout << #a << " --> " << (a) << "\\n";
#define forn(i, n) for (ll i = 0; i < (n); ++i)
#define fornd(i, n) for (ll i = (n)-1; i >= 0; --i)
#define forab(i, a, b) for (ll i = (a); i <= (b); ++i)
#define read(a)                                                                \\
  ll a;                                                                        \\
  cin >> a;
#define reads(s)                                                               \\
  string s;                                                                    \\
  cin >> s;
#define readb(a, b)                                                            \\
  ll a, b;                                                                     \\
  cin >> a >> b;
#define readc(a, b, c)                                                         \\
  ll a, b, c;                                                                  \\
  cin >> a >> b >> c;
#define readarr(a, n)                                                          \\
  int a[(n) + 1] = {};                                                         \\
  forab(i, 0, (n - 1)) { cin >> a[i]; }
#define readmat(a, n, m)                                                       \\
  int a[n + 1][m + 1] = {};                                                    \\
  forab(i, 1, n) { forab(j, 1, m) cin >> a[i][j]; }
#define readv(A, n)                                                            \\
  vll A(n, 0);                                                                 \\
  forn(i, n) cin >> A[i];
#define print(a) cout << a << endl;
#define printarr(a, n)                                                         \\
  forab(i, 1, n) cout << a[i] << " ";                                          \\
  cout << endl;
#define printv(v)                                                              \\
  for (int i : v)                                                              \\
    cout << i << " ";                                                          \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  forab(i, 1, n) {                                                             \\
    forab(j, 1, m) cout << a[i][j] << " ";                                     \\
    cout << endl;                                                              \\
  }

#define INF (1e18 + 5)
#define inf (1e9 + 5)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
using namespace std;
bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); }
int takemod(int x) { return (x % mod + mod) % mod; }

int fpow(int base, int power, int MOD) {
  int result = 1;
  base = base % MOD;
  while (power) {
    if (power & 1)
      result = (result * base) % MOD;
    base = (base * base) % MOD;
    power = power >> 1;
  }
  return result;
}

int modinv(int x) { return fpow(x, mod - 2, mod); }
void solve() {
  ll n, m;
  cin >> n >> m;
  ll b;
  cin >> b;
  // set<pll> pq;
  vll row(300010, 0);
  vll col(300010, 0);
  vector<pll> tars(b);
  forn(i, b) {
    cin >> tars[i].f >> tars[i].s;
    row[tars[i].f]++;
    col[tars[i].s]++;
    // pq.insert({tars[i].f,tars[i].s});
  }
  ll ans = 0;
  sort(all(tars));
  vll xs, ys;
  ll x = 0, y = 0, er = 0;
  forn(i, 300010) { er = max(er, row[i]); }
  forn(i, 300010) {
    if (er == row[i]) {
      xs.pb(i);
      //		break;
    }
  }
  ll temp = er;
  er = 0;
  forn(i, 300010) {
    er = max(er, col[i]);
    if (er + temp == n + m) {
      cout << n + m - 1 << endl;
      return;
    }
  }
  forn(i, 300010) {
    if (er == col[i]) {
      ys.pb(i);
      // break;
    }
  }
  // printv(xs)
  // printv(ys)
  forn(i, sz(xs)) {
    forn(j, sz(ys)) {
      if (*lower_bound(all(tars), mp(xs[i], ys[j])) == mp(xs[i], ys[j])) {

        ans = max(ans, row[xs[i]] + col[ys[j]] - 1);
      } else
        ans = max(ans, row[xs[i]] + col[ys[j]]);
    }
  }
  cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast_io;
  int T = 1;
  // cin>>T;
  //  cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    solve();
  }
  return 0;
}