alexjercan/bugnet huggingface dataset:
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;

const ll modo = 1e9 + 7;
const ll ms = 3e5 + 5;
const ll inf = 1e9 + 9;

vector<int> vv[ms];

map<int, int> ma;
map<int, int> mb;

void add(int v) {
  int f = ma[v];
  if (mb[f] == 1)
    mb.erase(f);
  else
    mb[f] -= 1;
  mb[f + 1] += 1;
  ma[v] += 1;
}

void del(int v) {
  int f = ma[v];
  if (mb[f] == 1)
    mb.erase(f);
  else
    mb[f] -= 1;
  mb[f - 1] += 1;
  ma[v] -= 1;
}

int main() {
  idfc;

  int n, m;
  int t;
  cin >> n >> m >> t;
  int x, y;
  while (t--) {
    cin >> x >> y;
    x--;
    y--;
    vv[y].pb(x);
    add(x);
  }

  // buildtree(0,n-1,1);
  int i, j;
  int ans = 0;
  int c;
  int p;
  // for(auto it:mb)cout<<it.first<<" "<<it.second<<"\\n";
  for (i = 0; i < m; i++) {
    c = vv[i].size();
    //  for(auto it:mb)cout<<it.first<<" "<<it.second<<"\\n";
    for (int ii : vv[i])
      del(ii);
    //   for(auto it:mb)cout<<it.first<<" "<<it.second<<"\\n";
    if (mb.empty())
      p = 0;
    else
      p = (*mb.rbegin()).first;
    ans = max(ans, c + p);
    for (int ii : vv[i])
      add(ii);
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;

const ll modo = 1e9 + 7;
const ll ms = 3e5 + 5;
const ll inf = 1e9 + 9;

vector<int> vv[ms];
int ar[ms] = {0};
int tree[ms * 4];

void buildtree(int l, int r, int ind) {
  if (l == r) {
    tree[ind] = ar[l];
    return;
  }
  int mid = (l + r) >> 1;
  buildtree(0, mid, 2 * ind);
  buildtree(mid + 1, r, 2 * ind + 1);
  tree[ind] = max(tree[2 * ind], tree[2 * ind + 1]);
}

void update(int l, int r, int i, int v, int ind) {
  if (l == r) {
    tree[ind] += v;
    return;
  }
  int mid = (l + r) >> 1;
  if (i <= mid)
    update(l, mid, i, v, 2 * ind);
  else
    update(mid + 1, r, i, v, 2 * ind + 1);
  tree[ind] = max(tree[2 * ind], tree[2 * ind + 1]);
}

int main() {
  idfc;

  int n, m;
  int t;
  cin >> n >> m >> t;
  int x, y;

  while (t--) {
    cin >> x >> y;
    x--;
    y--;
    vv[y].pb(x);
    ar[x]++;
  }

  buildtree(0, n - 1, 1);
  int i, j;
  int ans = 0;
  int c;
  for (i = 0; i < m; i++) {
    c = vv[i].size();
    for (int ii : vv[i])
      update(0, n - 1, ii, -1, 1);
    c += tree[1];
    ans = max(ans, c);
    for (int ii : vv[i])
      update(0, n - 1, ii, 1, 1);
  }
  cout << ans;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define endl "\\n"
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;

  ll mx1 = 0, mx2 = 0;
  map<ll, ll> row, col;
  set<pair<ll, ll>> s;
  ll rr, cc;
  for (ll i = 0; i < k; i++) {
    ll x, y;
    cin >> x >> y;
    row[x]++;
    col[y]++;
    s.insert({x, y});

    if (mx1 < row[x]) {
      mx1 = row[x];
      rr = x;
    }
    if (mx2 < col[y]) {
      mx2 = col[y];
      cc = y;
    }
  }
  ll flag = 0;

  vector<ll> vr, vc;
  for (auto it : row)
    if (it.second == mx1)
      vr.push_back(it.first);
  for (auto it : col)
    if (it.second == mx2)
      vc.push_back(it.first);

  for (ll i = 0; i < vr.size(); i++) {
    for (ll j = 0; j < vc.size(); j++) {
      ll t1 = vr[i];
      ll t2 = vc[j];
      if (s.find({t1, t2}) == s.end())
        flag = 1;
      if (flag == 1)
        break;
    }
    if (flag == 1)
      break;
  }
  if (flag == 1)
    cout << mx1 + mx2;
  else
    cout << mx1 + mx2 - 1;
}

int main() {
  FAST ll t = 1;
  // cin>>t;
  for (ll i = 0; i < t; i++)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define endl "\\n"
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;

  ll mx1 = 0, mx2 = 0;
  map<ll, ll> row, col;
  set<pair<ll, ll>> s;
  ll rr, cc;
  for (ll i = 0; i < k; i++) {
    ll x, y;
    cin >> x >> y;
    row[x]++;
    col[y]++;
    s.insert({x, y});

    if (mx1 < row[x]) {
      mx1 = row[x];
      rr = x;
    }
    if (mx2 < col[y]) {
      mx2 = col[y];
      cc = y;
    }
  }
  ll flag = 0;

  vector<ll> vr, vc;
  for (auto it : row)
    if (it.second == mx1)
      vr.push_back(it.first);
  for (auto it : col)
    if (it.second == mx2)
      vc.push_back(it.first);

  for (ll i = 0; i < vr.size(); i++) {
    for (ll j = 0; j < vc.size(); j++) {
      ll t1 = vr[i];
      ll t2 = vc[j];
      if (s.find({t1, t2}) == s.end())
        flag = 1;
    }
  }
  if (flag == 1)
    cout << mx1 + mx2;
  else
    cout << mx1 + mx2 - 1;
}

int main() {
  FAST ll t = 1;
  // cin>>t;
  for (ll i = 0; i < t; i++)
    solve();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

// RAVAN_2070
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fo(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define mp make_pair
void solve() {
  int h, w, m, i, r, c;
  cin >> h >> w >> m;
  vector<int> R(h, 0), C(w, 0);
  map<pair<int, int>, int> M;
  fo(i, 0, m) {
    cin >> r >> c;
    r--;
    c--;
    M[mp(r, c)]++;
    R[r]++;
    C[c]++;
  }
  int mxr, mxc;
  mxr = mxc = -1;
  fo(i, 0, h) mxr = max(mxr, R[i]);
  fo(i, 0, w) mxc = max(mxc, C[i]);

  vector<int> row, col;
  fo(i, 0, h) if (R[i] == mxr) row.pb(i);
  fo(i, 0, w) if (C[i] == mxc) col.pb(i);

  int ans = 0;
  for (auto itr : row) {
    int f;
    for (auto itc : col) {
      f = 1;
      int cnt = R[itr] + C[itc];
      if (M[mp(itr, itc)])
        f = 0, cnt--;
      ans = max(ans, cnt);
      if (f == 1)
        break;
    }
    if (f == 1)
      break;
  }
  cout << ans << "\\n";
}
int main() {
  fastio solve();
  return 0;
}


Error: TLE

Fixed submission:

// RAVAN_2070
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fo(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define mp make_pair
void solve() {
  int h, w, m, i, r, c;
  cin >> h >> w >> m;
  vector<int> R(h, 0), C(w, 0);
  map<pair<int, int>, int> M;
  fo(i, 0, m) {
    cin >> r >> c;
    r--;
    c--;
    M[mp(r, c)]++;
    R[r]++;
    C[c]++;
  }
  int mxr, mxc;
  mxr = mxc = -1;
  fo(i, 0, h) mxr = max(mxr, R[i]);
  fo(i, 0, w) mxc = max(mxc, C[i]);

  vector<int> row, col;
  fo(i, 0, h) if (R[i] == mxr) row.pb(i);
  fo(i, 0, w) if (C[i] == mxc) col.pb(i);

  int ans = 0;
  for (auto itr : row) {
    for (auto itc : col) {
      int cnt = R[itr] + C[itc];
      if (M[mp(itr, itc)])
        cnt--;
      ans = max(ans, cnt);
    }
  }
  cout << ans << "\\n";
}
int main() {
  fastio solve();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define int long long
#define double long double

const int INF = 2147483647;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-18;

using namespace std;

int h, w;
int m;
set<int> target;

int hsum[300001];
int wsum[300001];

vector<int> hmax, wmax;

int res;

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int x, y;

  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    target.insert(x * MOD + y);
    hsum[x]++;
    wsum[y]++;
  }

  res = -1;

  int Max = -INF;
  for (int i = 0; i < h; i++) {
    Max = max(Max, hsum[i]);
  }
  for (int i = 0; i < h; i++) {
    if (hsum[i] == Max) {
      hmax.push_back(i);
    }
  }
  res += Max;

  Max = -INF;
  for (int i = 0; i < w; i++) {
    Max = max(Max, wsum[i]);
  }
  for (int i = 0; i < w; i++) {
    if (wsum[i] == Max) {
      wmax.push_back(i);
    }
  }
  res += Max;

  bool flag = 0;
  for (int i = 0; i < hmax.size(); i++) {

    for (int j = 0; j < wmax.size(); j++) {

      if (target.find(hmax[i] * MOD + wmax[j]) == target.end()) {
        res++;
        flag = 1;
        break;
      }
    }

    if (flag == 1) {
      break;
    }
  }

  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define double long double

const int INF = 2147483647;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-18;

using namespace std;

int h, w;
int m;
set<int> target;

int hsum[300001];
int wsum[300001];

int res;

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int x, y;
  int t;

  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    target.insert(x * MOD + y);
    hsum[x]++;
    wsum[y]++;
  }

  res = -INF;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      t = hsum[i] + wsum[j];
      if (target.find(i * MOD + j) != target.end()) {
        t--;
      }
      res = max(res, t);
    }
  }

  cout << res << endl;

  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 300004;
int cnth[N], cntw[N], x[N], y[N];
map<int, bool> mp[N];

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int h, w, m;
  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    mp[x[i]][y[i]] = true;
    cnth[x[i]]++, cntw[y[i]]++;
  }
  int mxh = 0, mxw = 0;
  for (int i = 0; i < N; i++) {
    mxh = max(mxh, cnth[i]);
    mxw = max(mxw, cntw[i]);
  }
  vector<int> hall, wall;
  for (int i = 0; i < N; i++) {
    if (cnth[i] == mxh) {
      hall.push_back(i);
    }
    if (cntw[i] == mxw) {
      wall.push_back(i);
    }
  }
  if (hall.size() * wall.size() > m) {
    cout << mxh + mxw << endl;
    return 0;
  }
  int cnt = 0, mx = 0;
  for (int i = 0; i < hall.size(); i++) {
    for (int j = 0; j < wall.size(); j++) {
      mx = max(mx, mxh + mxw - mp[hall[i]][wall[j]]);
    }
  }
  cout << mx;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 300004;
int cnth[N], cntw[N], x[N], y[N];
map<int, bool> mp[N];

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int h, w, m;
  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    mp[x[i]][y[i]] = true;
    cnth[x[i]]++, cntw[y[i]]++;
  }
  int mxh = 0, mxw = 0;
  for (int i = 0; i < N; i++) {
    mxh = max(mxh, cnth[i]);
    mxw = max(mxw, cntw[i]);
  }
  vector<int> hall, wall;
  for (int i = 0; i < N; i++) {
    if (cnth[i] == mxh) {
      hall.push_back(i);
    }
    if (cntw[i] == mxw) {
      wall.push_back(i);
    }
  }
  int cnt = 0, mx = 0;
  for (int i = 0; i < hall.size(); i++) {
    for (int j = 0; j < wall.size(); j++) {
      mx = max(mx, mxh + mxw - mp[hall[i]][wall[j]]);
    }
  }
  cout << mx;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
ll n, m;
map<int, int> x, y;
struct node {
  int x, y;
};
node a[maxn];
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m;
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cin >> a[i].x >> a[i].y;
    x[a[i].x]++;
    y[a[i].y]++;
  }
  int maxx = 0, maxy = 0;
  for (int i = 1; i <= t; i++)
    maxx = max(maxx, x[a[i].x]), maxy = max(maxy, y[a[i].y]);
  if (x.size() == 1 || y.size() == 1) {
    cout << t << endl;
  } else {
    if (maxx == 1 && maxy == 1) {
      cout << 2 << endl;
    } else {
      ll x1 = 0, x2 = 0, y1 = 0, y2 = 0;
      map<int, int>::iterator it;
      for (it = x.begin(); it != x.end(); it++) {
        if (it->second == maxx)
          x1++;
        else if (it->second == maxx - 1)
          x2++;
      }
      for (it = y.begin(); it != y.end(); it++) {
        if (it->second == maxy)
          y1++;
        else if (it->second == maxy - 1)
          y2++;
      }
      ll ans1 = 0, ans2 = 0;
      ans1 = x1 * y1;
      ans2 = x2 * y1 + x1 * y2;
      for (int i = 1; i <= t; i++) {
        if (maxx + maxy == x[a[i].x] + y[a[i].y]) {
          ans1--;
          ans2++;
        } else if (maxx + maxy - 1 == x[a[i].x] + y[a[i].y]) {
          ans2--;
        }
      }
      if (ans1) {
        cout << maxx + maxy << endl;
      } else {
        cout << maxx + maxy - 1 << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
ll n, m;
map<int, int> x, y;
struct node {
  int x, y;
};
node a[maxn];
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m;
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cin >> a[i].x >> a[i].y;
    x[a[i].x]++;
    y[a[i].y]++;
  }
  int maxx = 0, maxy = 0;
  for (int i = 1; i <= t; i++)
    maxx = max(maxx, x[a[i].x]), maxy = max(maxy, y[a[i].y]);
  if (x.size() == 1 || y.size() == 1) {
    cout << t << endl;
  } else {
    if (maxx == 1 && maxy == 1) {
      cout << 2 << endl;
    } else {
      ll x1 = 0, x2 = 0, y1 = 0, y2 = 0;
      map<int, int>::iterator it;
      for (it = x.begin(); it != x.end(); it++) {
        if (it->second == maxx)
          x1++;
        else if (it->second == maxx - 1)
          x2++;
      }
      for (it = y.begin(); it != y.end(); it++) {
        if (it->second == maxy)
          y1++;
        else if (it->second == maxy - 1)
          y2++;
      }
      ll ans1 = 0, ans2 = 0;
      ans1 = x1 * y1;
      ans2 = x2 * y1 + x1 * y2;
      for (int i = 1; i <= t; i++) {
        if (maxx + maxy == x[a[i].x] + y[a[i].y]) {
          ans1--;
          ans2++;
        } else if (maxx + maxy - 1 == x[a[i].x] + y[a[i].y]) {
          ans2--;
        }
      }
      if (ans1) {
        cout << maxx + maxy << endl;
      } else {
        cout << maxx + maxy - 1 << endl;
      }
    }
  }
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten53 300005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
set<pair<ll, ll>> spll;
// map<string,ll> mp;
// map<pair<ll,ll>,ll>::iterator iter;
// map<ll,ll> mp;
// deque<ll> deq;
vector<pair<ll, ll>> vh(ten53), vw(ten53);
// vector<ll> vll;
ll numh[ten53], numw[ten53];
// priority_queue<pair<ll,ll>> pq;          //decreasing

int main(void) {
  ll m, n, i, j, k = 0, d, pre = 0, now, h, w, inh, inw, bh = 0, bw = 0;
  string s;
  cin >> h >> w >> m;
  if (m == 1) {
    cout << 1;
    return 0;
  }
  for (i = 1; i <= m; i++) {
    cin >> inh >> inw;
    spll.insert(mkpr(inh, inw));
    vh[inh].first--;
    vh[inh].second = inh;
    vw[inw].first--;
    vw[inw].second = inw;
  }
  sort(all(vh));
  sort(all(vw));
  for (i = 0; i < m; i++) {
    if (bh > -vh[i].first)
      break;
    bh = -vh[i].first;
    for (j = 0; j < m; j++) {
      if (bw > -vw[j].first)
        break;
      bw = -vw[j].first;
      pre = bw + bh;
      if (spll.find(mkpr(vh[i].second, vw[j].second)) != spll.end()) {
        pre--;
        k = max(k, pre);
      } else {
        cout << pre;
        return 0;
      }
    }
  }
  cout << k;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten53 300005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
set<pair<ll, ll>> spll;
// map<string,ll> mp;
// map<pair<ll,ll>,ll>::iterator iter;
// map<ll,ll> mp;
// deque<ll> deq;
vector<pair<ll, ll>> vh(ten53), vw(ten53);
// vector<ll> vll;
ll numh[ten53], numw[ten53];
// priority_queue<pair<ll,ll>> pq;          //decreasing

int main(void) {
  ll m, n, i, j, k = 0, d, pre = 0, now, h, w, inh, inw, bh = 0, bw = 0;
  string s;
  cin >> h >> w >> m;
  for (i = 1; i <= m; i++) {
    cin >> inh >> inw;
    spll.insert(mkpr(inh, inw));
    vh[inh].first--;
    vh[inh].second = inh;
    vw[inw].first--;
    vw[inw].second = inw;
  }
  sort(all(vh));
  sort(all(vw));
  for (i = 0; i < h; i++) {
    if (bh > -vh[i].first)
      break;
    bh = -vh[i].first;
    for (j = 0; j < w; j++) {
      if (bw > -vw[j].first)
        break;
      bw = -vw[j].first;
      pre = bw + bh;
      if (spll.find(mkpr(vh[i].second, vw[j].second)) != spll.end())
        pre--;
      k = max(k, pre);
    }
  }
  cout << k;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long n, m, k, a[300010], b[300010], x[300010], y[300010], ans, c, d, f, aa,
    bb;
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    cin >> x[i] >> y[i];
    a[x[i]]++;
    b[y[i]]++;
  }
  for (int i = 1; i <= n; i++)
    c = max(c, a[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] == c)
      aa++;
  for (int i = 1; i <= m; i++)
    d = max(d, b[i]);
  for (int i = 1; i <= m; i++)
    if (b[i] == d)
      bb++;
  for (int i = 0; i < k; i++)
    if (a[x[i]] == c && b[y[i]] == d)
      f++;
  if (f < aa * bb)
    cout << c + d << endl;
  else
    cout << c + d - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, k, a[300010], b[300010], x[300010], y[300010], ans, z[300020],
    zz[300010];
vector<int> v[300010];
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    cin >> x[i] >> y[i];
    v[x[i]].push_back(y[i]);
    a[x[i]]++;
    b[y[i]]++;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      z[a[i] + b[j]]++;
  for (int i = 0; i < k; i++)
    zz[a[x[i]] + b[y[i]]]++;
  for (int i = 300010 - 1; i >= 0; i--)
    if (z[i] == zz[i] && z[i] != 0) {
      cout << i - 1 << endl;
      break;
    } else if (zz[i] < z[i]) {
      cout << i << endl;
      break;
    }
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> st;
  vector<int> H_n(H, 0);
  vector<int> W_n(W, 0);

  for (int i = 0; i != M; ++i) {
    int h, w;
    cin >> h >> w;
    --h;
    --w;
    st.insert(make_pair(h, w));
    ++H_n[h];
    ++W_n[w];
  }
  /*
  cout<<"----set----"<<endl;

  for(set<pair<int,int> >::iterator itr=st.begin();itr!=st.end();++itr){
      cout<<itr->first<<":"<<itr->second<<endl;
  }
  */

  int max_h = 0;
  int max_w = 0;

  vector<int> vh;
  vector<int> vw;

  for (int i = 0; i < H; i++) {
    max_h = max(max_h, H_n[i]);
  }

  // cout<<max_h<<endl;

  for (int i = 0; i < H; i++) {
    if (H_n[i] == max_h) {
      vh.push_back(i);
    }
  }

  /*
  for(int i=0;i<vh.size();i++){
       cout<<vh[i]<<" ";
   }
   cout<<endl;
   */

  for (int i = 0; i < W; i++) {
    max_w = max(max_w, W_n[i]);
  }

  // cout<<max_w<<endl;

  for (int i = 0; i < W; i++) {
    if (W_n[i] == max_w) {
      vw.push_back(i);
    }
  }
  /*
  for(int i=0;i<vw.size();i++){
      cout<<vw[i]<<" ";
  }
  cout<<endl;
  */

  bool flag = false;
  for (int i = 0; i < vh.size(); ++i) {
    for (int j = 0; j < vw.size(); ++j) {
      if (st.find(make_pair(vh[i], vw[j])) == st.end()) {

        // cout<<"????"<<endl;
        flag = true;
        break;
      }
    }
  }

  // cout<<flag<<endl;

  if (flag) {
    cout << max_h + max_w << endl;
  } else {
    cout << max_h + max_w - 1 << endl;
  }
}


Error: TLE

Fixed submission:


#include <iostream>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> st;
  vector<int> H_n(H, 0);
  vector<int> W_n(W, 0);

  for (int i = 0; i != M; ++i) {
    int h, w;
    cin >> h >> w;
    --h;
    --w;
    st.insert(make_pair(h, w));
    ++H_n[h];
    ++W_n[w];
  }

  int ans = 0;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (H_n[i] + W_n[j] > ans) {
        if (st.find(make_pair(i, j)) != st.end()) {
          ans = H_n[i] + W_n[j] - 1;
        } else {
          ans = H_n[i] + W_n[j];
        }
      }
    }
  }

  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, b;
  cin >> h >> w >> b;
  unordered_map<int, int> xtrack;
  unordered_map<int, int> ytrack;
  map<pair<int, int>, int> coor;
  for (int i = 1; i <= b; i++) {
    int x, y;
    cin >> x >> y;
    coor[make_pair(x, y)] = 1;
    xtrack[x]++;
    ytrack[y]++;
  }
  int xmax = INT_MIN;
  int ymax = INT_MIN;
  for (auto i : xtrack) {
    xmax = max(xmax, i.second);
  }
  for (auto j : ytrack) {
    ymax = max(ymax, j.second);
  }
  vector<int> xcan;
  vector<int> ycan;
  for (auto i : xtrack) {
    if (i.second == xmax) {
      xcan.push_back(i.first);
    }
  }
  for (auto j : ytrack) {
    if (j.second == ymax) {
      ycan.push_back(j.first);
    }
  }
  int ans = xmax + ymax - 1;
  for (int i = 0; i < (int)xcan.size(); i++) {
    for (int j = 0; j < (int)ycan.size(); j++) {
      if (coor[make_pair(xcan[i], ycan[j])] == 0) {
        ans = xmax + ymax;
        break;
      }
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, b;
  cin >> h >> w >> b;
  int ans = INT_MIN;
  unordered_map<int, int> xtrack;
  unordered_map<int, int> ytrack;
  map<pair<int, int>, int> coor;
  for (int i = 1; i <= b; i++) {
    int x, y;
    cin >> x >> y;
    coor[make_pair(x, y)] = 1;
    xtrack[x]++;
    ytrack[y]++;
  }
  int xmax = INT_MIN;
  int ymax = INT_MIN;
  for (auto i : xtrack) {
    xmax = max(xmax, i.second);
  }
  for (auto j : ytrack) {
    ymax = max(ymax, j.second);
  }
  vector<int> xcan;
  vector<int> ycan;
  for (auto i : xtrack) {
    if (i.second == xmax) {
      xcan.push_back(i.first);
    }
  }
  for (auto j : ytrack) {
    if (j.second == ymax) {
      ycan.push_back(j.first);
    }
  }
  for (int i = 0; i < (int)xcan.size(); i++) {
    for (int j = 0; j < (int)ycan.size(); j++) {
      if (coor[make_pair(xcan[i], ycan[j])] == 1) {
        if (ans < xmax + ymax - 1) {
          ans = xmax + ymax - 1;
        }
      } else {
        if (ans < xmax + ymax) {
          ans = xmax + ymax;
        }
      }
    }
  }
  cout << ans;
  return 0;
}