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

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define int long long int
#define ld long double
#define pb push_back
#define MOD 1000000007
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero) (returns an
// iterator)

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int32_t main() {
  fastIO inp_out();

  int R, C, k;

  cin >> R >> C >> k;

  mii mp1, mp2;
  map<int, vi> mp;
  map<pii, int> mp3;
  for (int i = 0; i < k; ++i) {
    int x, y;
    cin >> x >> y;
    mp[x].pb(y);
    mp1[x]++;
    mp2[y]++;
    mp3[{x, y}]++;
  }

  multiset<int, greater<int>> st;
  for (auto i : mp2)
    st.insert(i.se);

  int ans = 0;

  int mx1 = 0, mx2 = 0;
  for (auto i : mp3) {
    ans = max(ans, mp1[i.fi.fi] + mp2[i.fi.se] - i.se);
  }

  for (auto i : mp1) {
    int can = 0;
    can += i.se;

    for (auto j : mp[i.fi]) {
      st.erase(st.find(mp2[j]));
      mp2[j]--;
      st.insert(mp2[j]);
      // 	db(mp2[j], j);
    }

    // 	db(i.fi, can);

    can += *st.begin();

    // 	db(can);

    for (auto j : mp[i.fi]) {
      st.erase(st.find(mp2[j]));
      mp2[j]++;
      st.insert(mp2[j]);
    }

    ans = max(ans, can);
  }

  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define int long long int
#define ld long double
#define pb push_back
#define MOD 1000000007
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero) (returns an
// iterator)

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int32_t main() {
  fastIO inp_out();

  int R, C, k;

  cin >> R >> C >> k;

  mii mp1, mp2;
  map<pii, int> mp3;
  for (int i = 0; i < k; ++i) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    mp1[x]++;
    mp2[y]++;
    mp3[{x, y}]++;
  }

  int ans = 0;
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      ans = max(ans, mp1[i] + mp2[j] - mp3[{i, j}]);
    }
  }

  cout << ans;

  return 0;
}