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

#include <bits/stdc++.h>

using i64 = int64_t;
using namespace std;
const i64 MOD = 1000000007LL;
vector<i64> fact(500000);

inline i64 powermod(i64 a, i64 b) {
  i64 r = 1LL;
  for (int i = 32 - __builtin_clz(b) - 1; i >= 0; i--) {
    if ((1 << i) & b)
      r = r * r % MOD * a % MOD;
    else
      r = r * r % MOD;
  }
  return r;
}

inline i64 divmod(i64 a, i64 b) { return a * powermod(b, MOD - 2) % MOD; }

i64 nCr(i64 n, i64 r) {
  if (r < 0)
    return 0;
  i64 ne = fact[n];
  i64 di = fact[n - r] * fact[r] % MOD;
  return divmod(ne, di);
}

inline i64 A(i64 a, i64 b) {
  // cerr << nCr(a,b) << " " << nCr(a,b-1) << endl;
  return (nCr(a, b) + MOD - nCr(a, b - 1)) % MOD;
}

int main() {
  i64 N, M, K;
  cin >> N >> M >> K;

  fact[0] = fact[1] = 1LL;
  for (int i = 2; i < 500000; i++) {
    fact[i] = fact[i - 1] * (i64)i % MOD;
  }

  i64 res = 0LL;
  for (i64 i = 0LL; i <= K; i++) {
    res += nCr(N + M + K + K, N + i * 2) * A(N + i * 2, i) % MOD *
           A(M + (K - i) * 2, K - i) % MOD;
    // cerr << nCr(N+M+K+K,N+i*2) << " " << A(N+i*2,i) << " " <<
    // A(M+(K-i)*2,K-i) << endl;
    res %= MOD;
  }

  cout << res << endl;
  // cerr << nCr(138,7) << endl;
  // cerr << nCr(3,1) << endl;
  // cerr << powermod(2,10) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using i64 = int64_t;
using namespace std;
const i64 MOD = 1000000007;

inline i64 addmod(i64 a, i64 b) { return (a + b) % MOD; }

inline i64 mulmod(i64 a, i64 b) { return a * b % MOD; }

inline i64 submod(i64 a, i64 b) { return (a + MOD - b) % MOD; }

inline i64 powermod(i64 a, i64 b) {
  // if(b==0)return 1;
  // else if(b==1)return a;
  // else{
  //   i64 r = powermod(a,b/2);
  //   return b % 2 ? r * r % MOD * a % MOD : r * r % MOD;
  // }
  i64 r = 1LL;
  // cerr << __builtin_clz(b) << endl;
  for (int i = 32 - __builtin_clz(b) - 1; i >= 0; i--) {
    if ((1 << i) & b)
      r = r * r % MOD * a % MOD;
    else
      r = r * r % MOD;
  }
  return r;
}

inline i64 divmod(i64 a, i64 b) { return a * powermod(b, MOD - 2) % MOD; }

i64 nCr(i64 n, i64 r) {
  if (r < 0)
    return 0;
  i64 res = 1LL;
  r = min(n - r, r);
  for (i64 i = 1LL; i <= r; i++) {
    res = mulmod(res, n - i + 1);
    res = divmod(res, i);
  }
  return res;
}

i64 A(i64 a, i64 b) {
  i64 r = submod(nCr(a, b), nCr(a, b - 1));
  return r;
}

int main() {
  i64 N, M, K;
  cin >> N >> M >> K;
  i64 res = 0LL;
  for (i64 i = 0LL; i <= K; i++) {
    i64 sub = nCr(N + M + K + K, N + i * 2) * A(N + i * 2, i) % MOD *
              A(M + (K - i) * 2, K - i) % MOD;
    res = addmod(res, sub);
  }
  cout << res << endl;
  // cerr << nCr(138,7) << endl;
  // cerr << powermod(2,10) << endl;
}
Problem p01452 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

typedef long long ll;
const ll mod = 1000000007;

ll pow_mod(ll a, ll n) {
  ll cur = 1;
  ll mul = a;
  while (n) {
    if (n % 2)
      cur = (cur * mul) % mod;
    mul = (mul * mul) % mod;
    n /= 2;
  }
  return cur;
}
ll div_mod(int a) { return pow_mod(a, mod - 2); }
ll fact[1000001];

ll comb(ll a, ll b) {
  if (a < 0 || b < 0)
    return 0;
  else if (b == 0)
    return 1;
  ll res = fact[a];
  res = (res * div_mod(fact[b])) % mod;
  res = (res * div_mod(fact[a - b])) % mod;
  return res;
}

int main() {
  fact[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  int N, M, K;
  cin >> N >> M >> K;
  ll res = 0;
  for (int p = 0; p <= K; p++) {
    ll a = 1;
    a = a * comb(N + 2 * p, p) % mod;
    a = (a * (N + 1)) % mod;
    a = (a * div_mod(N + p + 1)) % mod;
    a = a * comb(M + 2 * (K - p), K - p) % mod;
    a = (a * (M + 1)) % mod;
    a = (a * div_mod(M - p + 1 + K)) % mod;
    a = (a * comb(N + M + 2 * K, N + 2 * p)) % mod;
    res = (res + a) % mod;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

typedef long long ll;
const ll mod = 1000000007;

ll pow_mod(ll a, ll n) {
  ll cur = 1;
  ll mul = a;
  while (n) {
    if (n % 2)
      cur = (cur * mul) % mod;
    mul = (mul * mul) % mod;
    n /= 2;
  }
  return cur;
}
ll div_mod(int a) { return pow_mod(a, mod - 2); }
ll fact[200001];

ll comb(ll a, ll b) {
  if (a < 0 || b < 0)
    return 0;
  else if (b == 0)
    return 1;
  ll res = fact[a];
  res = (res * div_mod(fact[b])) % mod;
  res = (res * div_mod(fact[a - b])) % mod;
  return res;
}

int main() {
  fact[0] = 1;
  for (int i = 1; i <= 200000; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  int N, M, K;
  cin >> N >> M >> K;
  ll res = 0;
  for (int p = 0; p <= K; p++) {
    ll a = 1;
    a = a * comb(N + 2 * p, p) % mod;
    a = (a * (N + 1)) % mod;
    a = (a * div_mod(N + p + 1)) % mod;
    a = a * comb(M + 2 * (K - p), K - p) % mod;
    a = (a * (M + 1)) % mod;
    a = (a * div_mod(M - p + 1 + K)) % mod;
    a = (a * comb(N + M + 2 * K, N + 2 * p)) % mod;
    res = (res + a) % mod;
  }
  cout << res << endl;
  return 0;
}
Problem p01452 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MOD 1000000007
using namespace std;
typedef long long ll;

ll fact[220010];

ll rev(int a) {
  ll ans = 1;
  ll b = a;
  ll n = MOD - 2;
  while (n) {
    if (n & 1)
      ans = (ans * b) % MOD;
    b = (b * b) % MOD;
    n >>= 1;
  }
  return (ans + MOD) % MOD;
}

ll com(ll n, ll m) {
  ll ans = 1;
  // while(m){
  //	ans=((ans*n%MOD)*rev(m))%MOD;
  //	n--;
  //	m--;
  // }
  ans = (fact[n] * rev(fact[m])) % MOD * rev(fact[n - m]) % MOD;
  return (ans + MOD) % MOD;
}
int main(void) {
  fact[0] = 1;
  for (int i = 1; i <= 220000; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  int N, M, K;
  cin >> N >> M >> K;
  ll ans = 0;
  for (int p = 0; p <= K; p++) {
    int q = K - p;
    ll a = ((com(N + 2 * p, p) * (N + 1)) % MOD * rev(N + p + 1)) % MOD;
    ll b = ((com(M + 2 * q, q) * (M + 1)) % MOD * rev(M + q + 1)) % MOD;
    ll c = (com(M + N + 2 * K, N + 2 * p)) % MOD;
    ans = (ans + ((a * b) % MOD * c) % MOD) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define MOD 1000000007
using namespace std;
typedef long long ll;

ll fact[22010];

ll rev(int a) {
  ll ans = 1;
  ll b = a;
  ll n = MOD - 2;
  while (n) {
    if (n & 1)
      ans = (ans * b) % MOD;
    b = (b * b) % MOD;
    n >>= 1;
  }
  return (ans + MOD) % MOD;
}

ll com(ll n, ll m) {
  ll ans = 1;
  // while(m){
  //	ans=((ans*n%MOD)*rev(m))%MOD;
  //	n--;
  //	m--;
  // }
  ans = (fact[n] * rev(fact[m])) % MOD * rev(fact[n - m]) % MOD;
  return (ans + MOD) % MOD;
}
int main(void) {
  fact[0] = 1;
  for (int i = 1; i <= 22000; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  int N, M, K;
  cin >> N >> M >> K;
  ll ans = 0;
  for (int p = 0; p <= K; p++) {
    int q = K - p;
    ll a = ((com(N + 2 * p, p) * (N + 1)) % MOD * rev(N + p + 1)) % MOD;
    ll b = ((com(M + 2 * q, q) * (M + 1)) % MOD * rev(M + q + 1)) % MOD;
    ll c = (com(M + N + 2 * K, N + 2 * p)) % MOD;
    ans = (ans + ((a * b) % MOD * c) % MOD) % MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p01456 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  for (int u = 0; u < N; u++)
    cin >> a[u];
  vector<int> d(N);
  vector<vector<int>> G(N);
  while (M--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    d[u]++;
    G[v].push_back(u);
  }
  vector<vector<bool>> b(N, vector<bool>(N));
  for (int u = 0; u < N; u++)
    b[u][0] = true;
  queue<int> q;
  for (int u = 0; u < N; u++)
    if (!d[u])
      q.push(u);
  vector<bool> unko(N);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int x;
    for (x = 0; b[u][x]; x++)
      ;
    if (a[u] % 2)
      unko[x] = !unko[x];
    for (int v : G[u]) {
      b[v][x] = true;
      d[v]--;
      if (!d[v])
        q.push(v);
    }
  }
  bool ok = false;
  for (int x = 0; x < N; x++)
    if (unko[x])
      ok = true;
  cout << (ok ? 1 : 2) << endl;
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  for (int u = 0; u < N; u++)
    cin >> a[u];
  vector<int> d(N);
  vector<vector<int>> G(N);
  while (M--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    d[u]++;
    G[v].push_back(u);
  }
  vector<vector<bool>> b(N, vector<bool>(1 << 20));
  for (int u = 0; u < N; u++)
    b[u][0] = true;
  queue<int> q;
  for (int u = 0; u < N; u++)
    if (!d[u])
      q.push(u);
  int X = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int x;
    for (x = 0; b[u][x]; x++)
      ;
    if (a[u] % 2)
      X ^= x;
    for (int v : G[u]) {
      vector<bool> _b = b[v];
      for (int y = 0; y < 1 << 20; y++)
        if (b[v][y])
          _b[y ^ x] = true;
      b[v] = _b;
      d[v]--;
      if (!d[v])
        q.push(v);
    }
  }
  cout << (X ? 1 : 2) << endl;
}
Problem p01457 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define INF 0xfffffff
#define CL(x, v)                                                               \\
  ;                                                                            \\
  memset(x, v, sizeof(x));
#define LL long long
#define REP(i, r, n) for (int i = r; i <= n; i++)
#define RREP(i, n, r) for (int i = n; i >= r; i--)
#define max(x, y) x > y ? x : y
#define min(x, y) x < y ? x : y
#define EPS 1e-8
using namespace std;
int main() {

  int q;

  long long p, n;
  char c;
  while (scanf("%d", &q) != EOF) {
    long long ans = 0;
    for (int i = 0; i < q; i++) {
      scanf("%I64d %c %I64d", &p, &c, &n);
      if (c == '(') {
        ans += n;
      } else
        ans -= n;
      if (ans == 0)
        printf("Yes\\n");
      else
        printf("No\\n");
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char str;
int num[22485760];
int main() {
  int n;
  scanf("%d", &n);
  int x, y;
  int t = n;
  int i = 0;
  int sum1 = 0;
  int sum2 = 0;
  memset(num, 0, sizeof(num));
  int max = 0;
  while (i < n) {
    i++;
    scanf("%d %c %d", &x, &str, &y);
    if (x > max)
      max = x;
    if (str == '(')
      num[x] = num[x] + y;
    else
      num[x] = num[x] - y;

    int j = 0;
    while (j <= max) {

      if (num[j] + num[x] == 0) {
        printf("Yes\\n");
        num[j] = 0;
        num[x] = 0;
        break;
      }
      j++;
    }
    if (j > max)
      printf("No\\n");
  }
}
Problem p01457 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
#include <string>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
  int q, p, n;
  char c;
  std::stack<bool> lang;
  cin >> q;
  long r = 0;
  long l = 0;
  for (int t = 0; t < q; t++) {
    cin >> p;
    cin >> c;
    cin >> n;
    if (c == '(') {
      r += n;
    } else {
      l += n;
    }

    if (r == l) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <string>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
  int q, p, n;
  char c;
  std::stack<bool> lang;
  cin >> q;
  for (int t = 0; t < q; t++) {
    cin >> p;
    cin >> c;
    cin >> n;
    if (c == '(') {
      for (int i = 0; i < n; i++) {
        if (lang.empty() || lang.top()) {
          lang.push(true);
        } else {
          lang.pop();
        }
      }
    } else {
      for (int i = 0; i < n; i++) {
        if (lang.empty() || !lang.top()) {
          lang.push(false);
        } else {
          lang.pop();
        }
      }
    }
    if (lang.empty()) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p01457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int main() {
  long long int num = 0;
  int q, p, n;
  char c;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> p >> c >> n;
    if (c == '(')
      num += n;
    else
      num -= n;
    if (num == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int main() {
  string s = "";
  string reverses = "";
  int q, p, n;
  char c;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> p >> c >> n;
    if (p == 0) {
      for (int i = 0; i < n; i++) {
        s = c + s;
        if (c == '(')
          reverses = reverses + ')';
        else
          reverses = reverses + '(';
      }
    } else if (p == s.length()) {
      for (int i = 0; i < n; i++) {
        s = s + c;
        if (c == '(')
          reverses = ')' + reverses;
        else
          reverses = '(' + reverses;
      }
    } else {
      string s1, s2, rs1, rs2;
      s1 = s.substr(0, p);
      s2 = s.substr(p);
      // cout << s1 << " " << s2 << endl;
      rs1 = reverses.substr(0, reverses.length() - p);
      rs2 = reverses.substr(reverses.length() - p);
      // cout << s1 << " " << s2 << " " << rs1 << " " << rs2 << endl;
      for (int i = 0; i < n; i++) {
        s1 += c;
        if (c == '(')
          rs1 = ')' + rs1;
        else
          rs2 = '(' + rs2;
      }
      s = s1 + s2;
      reverses = rs1 + rs2;
    }
    // cout << s << " " << reverses << endl;
    if (s == reverses)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01457 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  long long cnt[2] = {};
  while (n--) {
    int a, b;
    char c;
    cin >> a >> c >> b;
    cnt[c - '('] += b;
    if (cnt[0] == cnt[1])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
vector<char> S, SS;
int main() {
  int q;
  cin >> q;
  int a, b;
  char c;

  while (q--) {
    cin >> a >> c >> b;
    SS = S;
    S.clear();
    for (int i = 0; i <= SS.size(); i++) {
      if (i == a)
        for (int j = 0; j < b; j++)
          S.push_back(c);
      if (SS.size() != i)
        S.push_back(SS[i]);
    }

    SS.clear();
    for (int i = 0; i < S.size(); i++) {
      if (SS.empty())
        SS.push_back(S[i]);
      else if (S[i] == '(' && SS.back() == ')')
        SS.erase(SS.begin() + SS.size() - 1);
      else if (S[i] == ')' && SS.back() == '(')
        SS.erase(SS.begin() + SS.size() - 1);
      else
        SS.push_back(S[i]);
    }
    if (SS.empty())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01457 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  string s;
  long long int left = 0, right = 0;
  for (int i = 0; i < N; i++) {
    long long int n, p;
    char c;
    cin >> p >> c >> n;

    if (c == '(')
      left += n;
    else
      right += n;

    if (right == left)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

map<string, bool> m;

bool check(string str) {
  // cout << "# " << str << endl;
  if (m.find(str) != m.end())
    return m[str];
  if (str == "S")
    return true;
  bool res = false;
  if (str.find("()") != string::npos) {
    string next = string(str.begin(), str.begin() + str.find("()")) + "S" +
                  string(str.begin() + str.find("()") + 2, str.end());
    res |= check(next);
  }
  if (str.find("(S)") != string::npos) {
    string next = string(str.begin(), str.begin() + str.find("(S)")) + "S" +
                  string(str.begin() + str.find("(S)") + 3, str.end());
    res |= check(next);
  }
  if (str.find(")(") != string::npos) {
    string next = string(str.begin(), str.begin() + str.find(")(")) + "S" +
                  string(str.begin() + str.find(")(") + 2, str.end());
    res |= check(next);
  }
  if (str.find(")S(") != string::npos) {
    string next = string(str.begin(), str.begin() + str.find(")S(")) + "S" +
                  string(str.begin() + str.find(")S(") + 3, str.end());
    res |= check(next);
  }
  if (str.find("SS") != string::npos) {
    string next = string(str.begin(), str.begin() + str.find("SS")) + "S" +
                  string(str.begin() + str.find("SS") + 2, str.end());
    res |= check(next);
  }

  return m[str] = res;
}

int main() {
  int N;
  cin >> N;
  string s;
  int left = 0, right = 0;
  for (int i = 0; i < N; i++) {
    int n, p;
    char c;
    cin >> p >> c >> n;

    if (c == '(')
      left += n;
    else
      right += n;

    for (int i = 0; i < n; i++) {
      s.insert(s.begin() + p, c);
    }

    if (right == left && check(s))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

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


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2340

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  int Q;
  cin >> Q;

  int P, N;
  char C;
  long long LPAR = 0, RPAR = 0;
  rep(nth, Q) {
    cin >> P >> C >> N;

    if (C == '(') {
      LPAR += N;
    } else {
      RPAR += N;
    }

    cout << ((LPAR == RPAR) ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2340

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

bool judge(vector<char> &S) {
  long long LPAR = 0, RPAR = 0;

  rep(i, S.size()) {
    if (S[i] == '(') {
      LPAR++;
    } else {
      RPAR++;
    }
  }

  return LPAR == RPAR;
}

int main() {
  int Q;
  cin >> Q;

  int P, N;
  char C;
  vector<char> S;
  rep(nth, Q) {
    cin >> P >> C >> N;
    rep(i, N) { S.insert(S.begin() + P, C); }
    cout << (judge(S) ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01457 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

int main(void) {
  int q;
  cin >> q;
  string s = "";

  long long int left = 0;
  long long int right = 0;
  int p;
  char c;
  int n;
  for (int i = 0; i < q; i++) {
    cin >> p >> c >> n;
    if (c == '(') {
      left += n;
    } else {
      right += n;
    }
    if (left != right) {
      cout << "No" << endl;
      continue;
    } else {
      cout << "Yes" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

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

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

void add(string &s, int p, char c, int n) {
  string new_str(n, c);
  if (p == 0) {
    s = new_str + s;
  } else if (p == (int)s.length()) {
    s = s + new_str;
  } else {
    string f = s.substr(0, p);
    string b = s.substr(p, (int)s.length() - p);
    s = f + new_str + b;
  }
  return;
}

bool check(string s) {
  int len = (int)s.length();
  if (len % 2 == 1) {
    return false;
  } else {
    bool ans = true;
    for (int i = 0; i < len / 2; i++) {
      if (s[i] == s[len - 1 - i]) {
        ans = false;
      }
    }
    return ans;
  }
}

int main(void) {
  int q;
  cin >> q;
  string s = "";

  int left = 0;
  int right = 0;
  int p;
  char c;
  int n;
  for (int i = 0; i < q; i++) {
    cin >> p >> c >> n;
    add(s, p, c, n);
    if (c == '(') {
      left += n;
    } else {
      right += n;
    }
    if (left != right) {
      cout << "No" << endl;
      continue;
    }
    // cout << s << endl;
    if (check(s) == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}