alexjercan/bugnet huggingface dataset:
Problem p00662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

ll Math, Greedy, Geometry, DP, Graph, Other;

int main() {
  while (scanf("%lld%lld%lld%lld%lld%lld", &Math, &Greedy, &Geometry, &DP,
               &Graph, &Other) &&
         Math + Greedy + Geometry + DP + Graph + Other) {
    ll g[3], res = 0, s;
    g[0] = Math + DP, g[1] = Greedy + Graph, g[2] = Geometry + Other;
    s = min(g[0], min(g[1], g[2]));
    for (int i = max((ll)0, s - 2); i <= s; i++)
      res = max(res, (g[0] - i) / 3 + (g[1] - i) / 3 + (g[2] - i) / 3 + i);
    printf("%lld\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define between(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

int math, greedy, geometry, dp, graph, other;

int main() {
  while (
      scanf("%d%d%d%d%d%d", &math, &greedy, &geometry, &dp, &graph, &other) &&
      math + greedy + geometry + dp + graph + other) {
    int ar[3];
    ar[0] = math + dp;
    ar[1] = greedy + graph;
    ar[2] = geometry + other;

    int res = 0;
    while (ar[0] && ar[1] && ar[2]) {
      ar[0]--;
      ar[1]--;
      ar[2]--;
      res++;
    }
    res += ar[0] / 3;
    ar[0] %= 3;
    res += ar[1] / 3;
    ar[1] %= 3;
    res += ar[2] / 3;
    ar[2] %= 3;

    printf("%d\\n", res);
  }
  return 0;
}
Problem p00662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int A, B, C;

int solve() {
  int a1, b1, c1, a2, b2, c2, a3, b3, c3;
  a1 = a2 = a3 = A;
  b1 = b2 = b3 = B;
  c1 = c2 = c3 = C;

  int res1 = 0, res2 = 0, res3 = 0;

  int minus1 = min(a1, min(b1, c1));
  a1 -= minus1, b1 -= minus1, c1 -= minus1;
  res1 += minus1;

  res1 += a1 / 3;
  res1 += b1 / 3;
  res1 += c1 / 3;

  res2 += a2 / 3;
  res2 += b2 / 3;
  res2 += c2 / 3;

  a2 %= 3;
  b2 %= 3;
  c2 %= 3;

  int minus2 = min(a2, min(b2, c2));
  res2 += minus2;

  int minus3 = min(a3, min(b3, c3));
  if (minus3 > 0)
    minus3--;
  a3 -= minus3, b3 -= minus3, c3 -= minus3;
  res3 += minus3;

  res3 += a3 / 3;
  res3 += b3 / 3;
  res3 += c3 / 3;

  // cout << "res1 = " << res1 << ", res2 = " << res2 << endl;

  return max(res1, max(res2, res3));
}

int main() {
  int Math, Greedy, Geometry, DP, Graph, Other;
  while (cin >> Math >> Greedy >> Geometry >> DP >> Graph >> Other) {
    // if(!Math && !Greedy && !Geometry && !DP && !Graph && !Other) break;
    if (Math == 0 && Greedy == 0 && Geometry == 0 && DP == 0 && Graph == 0 &&
        Other == 0)
      break;

    A = Math + DP;
    B = Greedy + Graph;
    C = Geometry + Other;

    // cout << "A = " << A << ", B = " << B << ", C = " << C << endl;
    int ans = solve();
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int A, B, C;

int solve() {
  int res = 0;
  for (;;) {
    if (A == 0 || B == 0 || C == 0)
      break;
    A--, B--, C--;
    res++;
  }

  for (int i = A; i >= 3; i -= 3)
    res++;
  for (int i = B; i >= 3; i -= 3)
    res++;
  for (int i = C; i >= 3; i -= 3)
    res++;

  return res;
}

int main() {
  int Math, Greedy, Geometry, DP, Graph, Other;
  while (cin >> Math >> Greedy >> Geometry >> DP >> Graph >> Other) {
    if (!Math && !Greedy && !Geometry && !DP && !Graph && !Other)
      break;

    A = Math + DP;
    B = Greedy + Graph;
    C = Geometry + Other;

    int ans = solve();
    cout << ans << endl;
    // cout << "A = " << A << ", B = " << B << ", C = " << C << endl;
  }
  return 0;
}
Problem p00662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  while (1) {
    int a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    int x = a + d, y = b + e, z = c + f;
    if (!x && !y && !z)
      break;
    int ans = 0;
    int amin = min(min(x, y), z);
    for (int i = 0; i <= min(2, amin); ++i) {
      int sum = i;
      sum += (x - i) / 3 + (y - i) / 3 + (z - i) / 3;
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  while (1) {
    int a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    int x = a + d, y = b + e, z = c + f;
    if (!x && !y && !z)
      break;
    int ans = 0;
    int amin = min(min(x, y), z);
    for (int i = 0; i <= amin; ++i) {
      int sum = i;
      sum += (x - i) / 3 + (y - i) / 3 + (z - i) / 3;
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int Math, Greedy, Geometry, DP, Graph, Other;

  while (cin >> Math >> Greedy >> Geometry >> DP >> Graph >> Other) {

    if (!Math && !Greedy && !Geometry && !DP && !Graph && !Other)
      break;

    int MD = Math + DP, GG = Greedy + Graph;
    int GO = Geometry + Other;

    int ans = 0;
    if (MD >= 0 && GG >= 0 && GO >= 0)
      ans = max(ans, MD / 3 + GG / 3 + GO / 3);

    if (MD >= 1 && GG >= 1 && GO >= 1)
      ans = max(ans, ((MD - 1) / 3 + (GG - 1) / 3 + (GO - 1) / 3) + 1);

    if (MD >= 2 && GG >= 2 && GO >= 2)
      ans = max(ans, ((MD - 2) / 3 + (GG - 2) / 3 + (GO - 2) / 3) + 2);

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int Math, Greedy, Geometry, DP, Graph, Other;

  while (cin >> Math >> Greedy >> Geometry >> DP >> Graph >> Other) {

    if (!Math && !Greedy && !Geometry && !DP && !Graph && !Other)
      break;

    int MD = Math + DP, GG = Greedy + Graph;
    int GO = Geometry + Other;

    int ans = 0;
    while (true) {
      if (MD >= 3) {
        ans++;
        MD -= 3;
      } else if (GG >= 3) {
        ans++;
        GG -= 3;
      } else if (GO >= 3) {
        ans++;
        GO -= 3;
      } else if ((0 < MD && MD < 3) && (0 < GG && GG < 3) &&
                 (0 < GO && GO < 3)) {
        MD--;
        GG--;
        GO--;
        ans++;
      } else
        break;
    }
    cout << ans << endl;
  }

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

#include <iostream>
using namespace std;

int main() {
  int math, greedy, geometry, DP, graph, other;
  while (cin >> math >> greedy >> geometry >> DP >> graph >> other) {
    if (math + greedy + geometry + DP + graph + other == 0)
      break;

    int A = math + DP;
    int B = greedy + graph;
    int C = geometry + other;

    int res = 0;

    int max_game = min(min(A, B), C);
    for (int balance_game = 0; balance_game <= min(2, max_game);
         balance_game++) {
      int tmpA = A;
      int tmpB = B;
      int tmpC = C;
      tmpA -= balance_game;
      tmpB -= balance_game;
      tmpC -= balance_game;
      int math_game = tmpA / 3;
      int algorithm_game = tmpB / 3;
      int implement_game = tmpC / 3;

      res =
          max(balance_game + math_game + algorithm_game + implement_game, res);
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {

  int mat, gre, geo, dp, gra, oth;
  int cnt = 0;
  int end = 1;

  while (cin >> mat >> gre >> geo >> dp >> gra >> oth) {
    if (mat == 0 && gre == 0 && geo == 0 && dp == 0 && gra == 0 && oth == 0) {
      break;
    }
    cnt = 0;
    end = 1;
    while (end) {
      // if((mat>0 || dp>0) && (gre>0 || gra>0) && (geo>0 || oth>0)){
      if (mat + dp > 0 && gre + gra > 0 && geo + oth > 0) {
        if (mat > 0) {
          mat--;
        } else {
          dp--;
        }
        if (gre > 0) {
          gre--;
        } else {
          gra--;
        }
        if (geo > 0) {
          geo--;
        } else {
          oth--;
        }
        cnt++;
      } else if (mat + dp >= 3) {
        if (mat >= 3) {
          mat -= 3;
        } else if (mat >= 2) {
          mat -= 2;
          dp -= 1;
        } else if (mat >= 1) {
          mat -= 1;
          dp -= 2;
        } else {
          dp -= 3;
        }
        cnt++;
      } else if (gre + gra >= 3) {
        if (gre >= 3) {
          gre -= 3;
        } else if (gre >= 2) {
          gre -= 2;
          gra -= 1;
        } else if (gre >= 1) {
          gre -= 1;
          gra -= 2;
        } else {
          gra -= 3;
        }
        cnt++;
      } else if (geo + oth >= 3) {
        if (geo >= 3) {
          geo -= 3;
        } else if (gre >= 2) {
          geo -= 2;
          oth -= 1;
        } else if (gre >= 1) {
          geo -= 1;
          oth -= 2;
        } else {
          oth -= 3;
        }
        cnt++;
      } else {
        end = 0;
      }
    }
    cout << cnt << endl;
  }
}
Problem p00663 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>
#include <string>
using namespace std;

bool clause(const string &s, int &p) {
  set<char> v, nv;
  while (s[p] != ')') {
    if (s[p] == '&') {
      ++p;
    } else if (s[p] == '~') {
      ++p;
      nv.insert(s[p++]);
    } else {
      v.insert(s[p++]);
    }
  }

  bool ret = true;
  for (set<char>::iterator it = v.begin(); it != v.end() && ret; ++it) {
    if (nv.find(*it) != nv.end())
      ret = false;
  }
  return ret;
}

bool exp(const string &s, int &p) {
  bool ret = false;
  while (p < (int)s.size()) {
    if (s[p] == '(') {
      ++p;
      ret |= clause(s, p);
    } else {
      ++p;
    }
  }
  return ret;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "#")
      break;

    int p = 0;
    if (exp(s, p))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <set>
#include <string>
using namespace std;

bool clause(string &s, int &p) {
  set<char> v, nv;
  while (s[p] != '(') {
    if (s[p] == '~') {
      ++p;
      nv.insert(s[p++]);
    } else {
      v.insert(s[p++]);
    }
  }

  bool ret = true;
  for (set<char>::iterator it = v.begin(); it != v.end() && ret; ++it) {
    if (nv.find(*it) != nv.end())
      ret = false;
  }
  return ret;
}

bool exp(string &s, int &p) {
  bool ret = false;
  while (p < (int)s.size()) {
    if (s[p] == '(') {
      ++p;
      ret |= clause(s, p);
    } else if (s[p] == '|') {
      ++p;
    }
  }
  return ret;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "#")
      break;

    int p = 0;
    if (exp(s, p))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p00663 (C++) — buggy submission, verdict: Runtime Error

// Name: SAT-EN-3
// Level: 3
// Category: 構文解析
// Note:

/**
 * 加法標準形なので、X&~Xの形を含まない項があればその項は真にでき、全体も真になる。
 *
 * オーダーは O(|S|)。
 */
#include <cassert>
#include <iostream>
#include <map>
#include <string>

using namespace std;

pair<char, bool> literal(const string &str, int &pos) {
  pair<char, bool> res('\\0', false);
  if (str[pos] == '~') {
    res.second = true;
    ++pos;
  }
  res.first = str[pos++];
  return res;
}

bool parse(const string &str, int pos) {
  if (pos >= str.size())
    return false;
  if (str[pos] == '|')
    ++pos;
  assert(str[pos] == '(');
  ++pos;
  map<char, bool> vars;
  bool ok = true;
  for (int i = 0; i < 3; ++i) {
    pair<char, bool> var = literal(str, pos);
    if (vars.count(var.first) && vars[var.first] != var.second) {
      ok = false;
    }
    vars[var.first] = var.second;
    if (i != 2) {
      assert(str[pos] == '&');
      ++pos;
    }
  }
  assert(str[pos] == ')');
  ++pos;
  if (ok)
    return true;
  else
    return parse(str, pos);
}

bool solve() {
  string str;
  if (!(cin >> str))
    return false;
  if (str == "#")
    return false;

  cout << (parse(str, 0) ? "yes" : "no") << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;
  return 0;
}

Error: 0

Fixed submission:

// Name: SAT-EN-3
// Level: 3
// Category: 構文解析
// Note:

/**
 * 加法標準形なので、X&~Xの形を含まない項があればその項は真にでき、全体も真になる。
 *
 * オーダーは O(|S|)。
 */
#include <cassert>
#include <iostream>
#include <map>
#include <string>

using namespace std;

pair<char, bool> literal(const string &str, int &pos) {
  pair<char, bool> res('\\0', false);
  if (str[pos] == '~') {
    res.second = true;
    ++pos;
  }
  res.first = str[pos++];
  return res;
}

bool parse(const string &str, int pos) {
  if (pos >= str.size())
    return false;
  assert(str[pos] == '(');
  ++pos;
  map<char, bool> vars;
  bool ok = true;
  for (int i = 0; i < 3; ++i) {
    pair<char, bool> var = literal(str, pos);
    if (vars.count(var.first) && vars[var.first] != var.second) {
      ok = false;
    }
    vars[var.first] = var.second;
    if (i != 2) {
      assert(str[pos] == '&');
      ++pos;
    }
  }
  assert(str[pos] == ')');
  ++pos;
  if (ok)
    return true;
  else
    return parse(str, pos);
}

bool solve() {
  string str;
  if (!(cin >> str))
    return false;
  if (str == "#")
    return false;

  cout << (parse(str, 0) ? "yes" : "no") << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;
  return 0;
}
Problem p00664 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

int main() {
  int r, c, q;
  int a[50000];
  int b[50000];
  int o[50000];
  int row[50000];
  int col[50000];
  int nrow, ncol;

  long int out;

  while (cin >> r >> c >> q, (r || c || q)) {
    memset(row, 0, sizeof(row));
    memset(col, 0, sizeof(col));
    nrow = 0;
    ncol = 0;
    out = 0;

    for (int i = 0; i < q; i++) {
      cin >> a[q - i - 1] >> b[q - i - 1] >> o[q - i - 1];
    }

    for (int i = 0; i < q; i++) {
      if (a[i] == 0) {
        if (row[b[i]] == 1) {
          continue;
        } else {
          row[b[i]] = 1;
          nrow++;

          if (o[i] == 1)
            out += c - ncol;
        }
      } else {
        if (col[b[i]] == 1) {
          continue;
        } else {
          col[b[i]] = 1;
          ncol++;

          if (o[i] == 1)
            out += r - nrow;
        }
      }
    }

    cout << out << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

int main() {
  int r, c, q;
  int a[10000], b[10000], o[10000];
  int nrow, ncol;
  long int out;
  bool row[50000], col[50000];

  while (cin >> r >> c >> q, (r || c || q)) {
    for (int i = 0; i < q; i++) {
      cin >> a[q - i - 1] >> b[q - i - 1] >> o[q - i - 1];
    }

    memset(row, 0, sizeof(row));
    memset(col, 0, sizeof(col));
    out = 0;
    nrow = 0;
    ncol = 0;

    for (int i = 0; i < q; i++) {

      if (a[i] == 0) {
        if (row[b[i]])
          continue;

        row[b[i]] = 1;
        nrow++;
        if (o[i] == 1)
          out += c - ncol;
      } else {
        if (col[b[i]])
          continue;

        col[b[i]] = 1;
        ncol++;
        if (o[i] == 1)
          out += r - nrow;
      }
    }
    cout << out << endl;
  }

  return 0;
}
Problem p00664 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n, m, q;
  while (cin >> n >> m >> q, n + m + q) {
    vi h(n), w(m);
    int coh = 0, cow = 0;
    vvi in(q, vi(3));
    rep(i, q) rep(j, 3) cin >> in[q - 1 - i][j];
    int out = 0;
    rep(i, q) {
      if (in[i][0] && w[in[i][1]] == 0) {
        if (in[i][2])
          out += n - coh;
        cow++;
        w[in[i][1]] = true;
      } else if (in[i][0] == 0 && h[in[i][1]] == 0) {
        if (in[i][2])
          out += m - cow;
        coh++;
        h[in[i][1]] = true;
      }
    }
    cout << out << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n, m, q;
  while (cin >> n >> m >> q, n + m + q) {
    vi h(n), w(m);
    int coh = 0, cow = 0;
    vvi in(q, vi(3));
    rep(i, q) rep(j, 3) cin >> in[n - 1 - i][j];
    int out = 0;
    rep(i, q) {
      if (in[i][0] && w[in[i][1]] == 0) {
        if (in[i][2])
          out += n - coh;
        cow++;
        w[in[i][1]] = true;
      } else if (in[i][0] == 0 && h[in[i][1]] == 0) {
        if (in[i][2])
          out += m - cow;
        coh++;
        h[in[i][1]] = true;
      }
    }
    cout << out << endl;
  }
}
Problem p00664 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>

using namespace std;

int main() {
  for (;;) {
    int r, c, q;

    scanf("%d %d %d", &r, &c, &q);
    if (r == 0 && c == 0 && q == 0)
      break;

    int a[50000], b[50000], o[50000];
    for (int i = 0; i < q; ++i)
      scanf("%d %d %d", &a[i], &b[i], &o[i]);

    int e = 0, nr = r, nc = c;
    bool f[2][50000] = {};
    for (int i = q - 1; i >= 0; --i) {
      if (!f[a[i]][b[i]]) {
        f[a[i]][b[i]] = true;

        if (a[i] == 0) {
          --nr;
          if (o[i] == 1)
            e += nc;
        } else {
          --nc;
          if (o[i] == 1)
            e += nr;
        }
      }
    }

    printf("%d\\n", e);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <set>
#include <vector>

using namespace std;

struct l {
  bool all;
  set<int> f;
};

int main() {
  for (;;) {
    int r, c, q;

    scanf("%d %d %d", &r, &c, &q);
    if (r == 0 && c == 0 && q == 0)
      break;

    vector<l> d(r);
    for (int i = 0; i < r; ++i) {
      d[i].all = false;
      d[i].f.clear();
    }

    for (int i = 0; i < q; ++i) {
      int a, b, o;

      scanf("%d %d %d", &a, &b, &o);

      bool f = (o == 1 ? true : false);
      if (a == 0) {
        d[b].all = f;
        d[b].f.clear();
      } else {
        if (f) {
          for (int j = 0; j < r; ++j) {
            if (d[j].all)
              d[j].f.erase(b);
            else
              d[j].f.insert(b);
          }
        } else {
          for (int j = 0; j < r; ++j) {
            if (d[j].all)
              d[j].f.insert(b);
            else
              d[j].f.erase(b);
          }
        }
      }
    }

    int e = 0;
    for (int i = 0; i < r; ++i) {
      if (d[i].all)
        e += c - d[i].f.size();
      else
        e += d[i].f.size();
    }
    printf("%d\\n", e);
  }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;
struct data {
  int A, B, O;
};
const int MAX = 50001;
const int Row = 0;
const int Col = 1;

bool fin[2][MAX];
int r, c, q;

int main() {

  while (cin >> r >> c >> q && r + c + q) {
    memset(fin, false, sizeof(fin));

    vector<data> V(q);

    for (int i = 0; i < q; i++)
      cin >> V[i].A >> V[i].B >> V[i].O;

    int delR = 0, delC = 0;
    long long ans = 0;

    for (int i = V.size() - 1; i >= 0; i--) {
      if (V[i].A) {
        // column

        if (fin[Col][V[i].B])
          continue;

        if (V[i].O) {
          ans += r - delC;
          fin[Col][V[i].B] = true;
          delR++;
        } else {
          delR++;
          fin[Col][V[i].B] = true;
        }

      } else {
        // row

        if (fin[Row][V[i].B])
          continue;

        if (V[i].O) {
          ans += c - delR;
          fin[Row][V[i].B] = true;
          delC++;
        } else {
          delC++;
          fin[Row][V[i].B] = true;
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

typedef pair<int, bool> P;

const int MAX = 50001;
P stand[2][MAX];
int r, c, q;

int main() {

  while (cin >> r >> c >> q && r + c + q) {
    fill(stand[0], stand[0] + 2 * MAX, P(-1, false));

    for (int i = 0; i < q; i++) {
      int a, b, o;
      cin >> a >> b >> o;
      stand[a][b] = P(i, o);
    }

    long long ans = 0;
    for (int i = 0; i < r; i++)
      for (int j = 0; j < c; j++) {
        P p = max(stand[0][i], stand[1][j]);
        ans += p.second;
      }

    cout << ans << endl;
  }
  return 0;
}