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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define inf 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
struct node {
  int num[26];
  bool friend operator<(node a, node b) {
    for (int i = 0; i < 26; i++) {
      if (a.num[i] != b.num[i])
        return a.num[i] > b.num[i];
    }
    return false;
  }
} sum[4005], sum1[4005];

set<node> q;
string a, b;

int main() {
  q.clear();
  met(sum, 0);
  met(sum1, 0);
  cin >> a >> b;
  for (int i = 0; i < a.length(); i++) {
    for (int j = 0; j < 26; j++) {
      sum[i + 1].num[j] += sum[i].num[j];
    }
    sum[i + 1].num[a[i] - 'a']++;
  }
  for (int i = 0; i < b.length(); i++) {
    for (int j = 0; j < 26; j++) {
      sum1[i + 1].num[j] += sum1[i].num[j];
    }
    sum1[i + 1].num[b[i] - 'a']++;
  }
  int Max = 0;
  for (int len = min((int)a.length(), (int)b.length()); len >= 1; len--) {
    q.clear();
    for (int i = 0; i + len <= a.length(); i++) {
      int j = i + len;
      node x;
      for (int k = 0; k < 26; k++) {
        x.num[k] = sum[j].num[k] - sum[i].num[k];
      }
      q.insert(x);
    }
    for (int i = 0; i + len <= b.length(); i++) {
      int j = i + len;
      node x;
      for (int k = 0; k < 26; k++) {
        x.num[k] = sum1[j].num[k] - sum1[i].num[k];
      }
      if (q.find(x) != q.end()) {
        int ans = 0;
        for (int k = 0; k < 26; k++)
          ans += x.num[k];
        Max = max(Max, ans);
      }
    }
  }
  printf("%d\\n", Max);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 26;
map<string, int> mp;
int main(int argc, char const *argv[]) {
  char s[5000];
  char s2[5000];
  while (~scanf("%s", s + 1)) {
    scanf("%s", s2 + 1);
    char s1[5000];
    int min1 = 0;
    int len = strlen(s + 1);
    int len2 = strlen(s2 + 1);
    for (int i = 1; i <= len2; i++) {
      mp.clear();
      for (int j = 1; j + i - 1 <= len; j++) {
        char t = s[j + i];
        s[j + i] = '\\0';
        strcpy(s1 + 1, s + j);
        sort(s1 + 1, s1 + i + 1);
        //  printf("%s\\n",s1+1);
        mp[s1 + 1] = 1;
        s[i + j] = t;
      }
      for (int j = 1; j + i - 1 <= len2; j++) {
        char t = s2[i + j];
        s2[i + j] = '\\0';
        strcpy(s1 + 1, s2 + j);
        sort(s1 + 1, s1 + 1 + i);
        if (mp[s1 + 1]) {
          min1 = max(min1, i);
        }
        s2[i + j] = t;
      }
    }
    printf("%d\\n", min1);
  }
  return 0;
}
Problem p00949 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  string S, T;
  cin >> S >> T;
  if (S.length() > T.length()) {
    swap(S, T);
  }
  vector<vector<int>> memo(S.length() + 1, vector<int>(26));
  for (int i = 0; i < S.length(); i++) {
    memo[i + 1] = memo[i];
    memo[i + 1][S[i] - 'a']++;
  }
  vector<vector<int>> memo2(T.length() + 1, vector<int>(26));
  for (int i = 0; i < T.length(); i++) {
    memo2[i + 1] = memo2[i];
    memo2[i + 1][T[i] - 'a']++;
  }
  {
    bool f = false;
    for (int i = 0; i < 26; i++) {
      if (memo[S.length()][i] && memo2[T.length()][i]) {
        f = true;
        break;
      }
    }
    if (!f) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<int> now(26);
  int ans = 0;
  for (int len = S.length(); len >= 1; len--) {
    set<vector<int>> SVI;
    for (int i = 0; i + len < memo.size(); i++) {
      for (int k = 0; k < 26; k++) {
        now[k] = memo[i + len][k] - memo[i][k];
      }
      SVI.insert(now);
    }
    for (int i = 0; i + len < memo2.size(); i++) {
      for (int k = 0; k < 26; k++) {
        now[k] = memo2[i + len][k] - memo2[i][k];
      }
      if (SVI.count(now)) {
        cout << len << endl;
        return 0;
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string S, T;
  cin >> S >> T;
  if (S.length() > T.length()) {
    swap(S, T);
  }
  vector<vector<int>> memo(S.length() + 1, vector<int>(26));
  for (int i = 0; i < S.length(); i++) {
    memo[i + 1] = memo[i];
    memo[i + 1][S[i] - 'a']++;
  }
  vector<vector<int>> memo2(T.length() + 1, vector<int>(26));
  for (int i = 0; i < T.length(); i++) {
    memo2[i + 1] = memo2[i];
    memo2[i + 1][T[i] - 'a']++;
  }
  {
    bool f = false;
    for (int i = 0; i < 26; i++) {
      if (memo[S.length()][i] && memo2[T.length()][i]) {
        f = true;
        break;
      }
    }
    if (!f) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<int> now(26);
  int ans = 0;
  for (int i = 0; i < memo.size(); i++) {
    for (int j = memo.size() - 1; j > i + ans; j--) {
      for (int l = 0; l + j - i < memo2.size(); l++) {
        int m = l + j - i;
        bool f = true;
        for (int k = 0; k < 26; k++) {
          if (memo[j][k] - memo[i][k] - memo2[m][k] + memo2[l][k] != 0) {
            f = false;
            break;
          }
        }
        if (f) {
          ans = max(ans, j - i);
          break;
        }
      }
    }
  }

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

#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

#define ALPHA 4003

using namespace std;

typedef long long ll;

char A[4005], B[4005];

int MOD[6] = {
    0, (int)1e9 + 7, (int)1e9 + 9, (int)1e9 + 23, (int)1e9 + 97, (int)1e9 + 33};
int off[6] = {0, 0, (int)1e2, (int)1e4, int(1e6), (int)1e8};

bitset<1000000097> h[2];
int pot[6][30];

inline int add(int a, int b, int m) {
  ll ret = a + b;
  if (ret >= m)
    ret -= m;
  return (int)ret;
}

int main() {
  scanf(" %s", A);
  scanf(" %s", B);

  for (int j = 1; j <= 5; j++)
    pot[j][0] = 1;
  for (int i = 1; i < 30; i++) {
    for (int j = 1; j <= 5; j++)
      pot[j][i] = ((ll)pot[j][i - 1] * ALPHA) % MOD[j];
  }

  for (int i = 0; A[i]; i++) {
    int l[6] = {0};
    for (int j = i; A[j]; j++) {
      for (int k = 1; k <= 5; k++) {
        l[k] = add(l[k], pot[k][A[j] - 'a'], MOD[k]);

        int pos = add(l[k], off[k], MOD[k]);

        if (k <= 2)
          h[0][pos] = 1;
        else
          h[1][pos] = 1;
      }
    }
  }

  int ans = 0;
  for (int i = 0; B[i]; i++) {
    int l[6] = {0};
    for (int j = i; B[j]; j++) {
      bool flag = 1;
      for (int k = 1; k <= 5; k++) {
        l[k] = add(l[k], pot[k][B[j] - 'a'], MOD[k]);

        int x = 0;
        int pos = add(l[k], off[k], MOD[k]);
        if (k <= 2)
          x = h[0][pos];
        else
          x = h[1][pos];

        if (x == 0)
          flag = 0;
      }

      if (flag)
        ans = max(ans, j - i + 1);
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

#define ALFA 29
#define MOD1 1000000007
#define MOD2 1000000009
#define MOD3 1000000023

using namespace std;

typedef long long ll;

char A[4005], B[4005];

bitset<MOD3> h1, h2, h3;
int pot[4][30];

inline int add(int a, int b, int m) {
  ll ret = a + b;
  if (ret >= m)
    ret -= m;
  return (int)ret;
}

int main() {
  scanf(" %s", A);
  scanf(" %s", B);

  pot[1][0] = pot[2][0] = pot[3][0] = 1;
  for (int i = 1; i < 30; i++) {
    pot[1][i] = ((ll)pot[1][i - 1] * ALFA) % MOD1;
    pot[2][i] = ((ll)pot[2][i - 1] * ALFA) % MOD2;
    pot[3][i] = ((ll)pot[3][i - 1] * ALFA) % MOD3;
  }

  for (int i = 0; A[i]; i++) {
    int l1 = 0, l2 = 0, l3 = 0;
    for (int j = i; A[j]; j++) {
      l1 = add(l1, pot[1][A[j] - 'a'], MOD1);
      l2 = add(l2, pot[2][A[j] - 'a'], MOD2);
      l3 = add(l3, pot[3][A[j] - 'a'], MOD3);
      h1[l1] = 1;
      h2[l2] = 1;
      h3[l3] = 1;
    }
  }

  int ans = 0;
  for (int i = 0; B[i]; i++) {
    int l1 = 0, l2 = 0, l3 = 0;
    for (int j = i; B[j]; j++) {
      l1 = add(l1, pot[1][B[j] - 'a'], MOD1);
      l2 = add(l2, pot[2][B[j] - 'a'], MOD2);
      l3 = add(l3, pot[3][B[j] - 'a'], MOD3);
      if (h1[l1] && h2[l2] && h3[l3]) {
        ans = max(ans, j - i + 1);
      }
    }
  }

  printf("%d\\n", ans);

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const ll MOD0 = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll base = 4007;

ll modpow(ll a, int n, ll q) {
  ll ret = 1, k = a;
  for (int i = 0; (1 << i) <= n; i++) {
    if ((1 << i) & n)
      ret = ret * k % q;
    k = k * k % q;
  }
  return ret;
}

int main() {

  //	ll p=0;
  //	{
  //		bool f[10000]={};
  //		for(int i=2; i<10000; i++){
  //			if( !f[i] ){
  //				if( i>4005 ){
  //					p = i;
  //					break;
  //				}
  //				for(int j=i; j<10000; j+=i) f[j] = true;
  //			}
  //		}
  //	}

  string s[2];
  rep(i, 2) cin >> s[i];

  int ans = 0;
  ll hs[2][2][4005] = {};
  rep(l, min(s[0].length(), s[1].length())) {
    // cout << l << endl;
    set<pii> st;
    rep(k, 2) {
      rep(i, s[k].length() - l) {
        int c = s[k][i + l] - 'a' + 1;
        hs[k][0][i] += modpow(base, c, MOD0);
        hs[k][0][i] %= MOD0;
        hs[k][1][i] += modpow(base, c, MOD1);
        hs[k][1][i] %= MOD1;
        auto key = pii(hs[k][0][i], hs[k][1][i]);
        if (k == 0) {
          // cout << "insert " << i << " " << l+1 << " " << key << endl;
          st.insert(key);
        } else {
          // cout << "count  " << i << " " << l+1 << " " << key << endl;
          if (st.count(key)) {
            ans = max(ans, l + 1);
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;

ll modpow(ll a, int n) {
  ll ret = 1, k = a;
  for (int i = 0; (1 << i) <= n; i++) {
    if ((1 << i) & n)
      ret = ret * k % MOD;
    k = k * k % MOD;
  }
  return ret;
}

int main() {

  ll p;
  bool f[10000] = {};
  for (int i = 2; i < 10000; i++) {
    if (!f[i]) {
      if (i > 4005) {
        p = i;
        break;
      }
      for (int j = i; j < 10000; j += i)
        f[j] = true;
    }
  }

  string s[2];
  rep(i, 2) cin >> s[i];

  int ans = 0;
  set<int> st;
  rep(k, 2) {
    ll hs[4005] = {};
    rep(i, s[k].length()) {
      int c = s[k][i] - 'a';
      rep(j, i + 1) {
        hs[j] += modpow(p, c);
        hs[j] %= MOD;
        int l = i + 1 - j;
        int key = hs[j]; // + MOD*l;
        if (k == 0) {
          st.insert(key);
        } else {
          if (st.count(key))
            ans = max(ans, l);
        }
      }
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
const int maxn = 40005;
const long long p = 1e14 + 7;
char a[maxn], b[maxn];
int num[26], tmp, l1, l2;
inline long long H() {
  long long hash = 0;
  for (int i = 0; i < 26; i++)
    hash = (hash * 6887 + num[i]) % p;
  return hash;
}
long long f[maxn];
inline long long erfen(long long x) {
  int l = 1, r = tmp;
  while (l < r) {
    int mid = l + r >> 1;
    if (f[mid] >= x)
      r = mid;
    else
      l = mid + 1;
  }
  return f[l];
}
inline int work() {
  for (int len = min(l1, l2); len >= 1; len--) {
    tmp = 0;
    memset(num, 0, sizeof(num));
    for (int i = 1; i <= len; i++)
      num[a[i] - 'a']++;
    for (int j = len; j <= l1; j++) {
      long long hash = H();
      f[++tmp] = hash;
      num[a[j + 1] - 'a']++;
      num[a[j - len + 1] - 'a']--;
    }
    sort(f + 1, f + 1 + tmp);
    memset(num, 0, sizeof(num));
    for (int i = 1; i <= len; i++)
      num[b[i] - 'a']++;
    for (int j = len; j <= l2; j++) {
      long long hash = H();
      if (erfen(hash) == hash)
        return len;
      num[b[j + 1] - 'a']++;
      num[b[j - len + 1] - 'a']--;
    }
  }
  return 0;
}
int main() {
  while (scanf("%s %s", a + 1, b + 1) != EOF) {
    l1 = strlen(a + 1);
    l2 = strlen(b + 1);
    printf("%d\\n", work());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
const int maxn = 40005;
const long long p = 1e14 + 7;
char a[maxn], b[maxn];
int num[26];
inline long long H() {
  long long hash = 0;
  for (int i = 0; i < 26; i++)
    hash = (hash * 6887 + num[i]) % p;
  return hash;
}
set<long long> s;
int main() {

  while (scanf("%s%s", a + 1, b + 1) != EOF) {
    int l1 = strlen(a + 1), l2 = strlen(b + 1), ans = 0;
    for (int i = 1; i <= l2; i++)
      num[b[i] - 'a']++;
    for (int len = 1; len <= l1 && len <= l2; len++) {
      s.clear();
      memset(num, 0, sizeof(num));
      for (int i = 1; i <= len; i++)
        num[a[i] - 'a']++;
      for (int j = len; j <= l1; j++) {
        long long hash = H();
        if (s.find(hash) == s.end())
          s.insert(hash);
        num[a[j + 1] - 'a']++;
        num[a[j - len + 1] - 'a']--;
      }
      memset(num, 0, sizeof(num));
      for (int i = 1; i <= len; i++)
        num[b[i] - 'a']++;
      for (int j = len; j <= l2; j++) {
        long long hash = H();
        if (s.find(hash) != s.end())
          ans = len;
        num[b[j + 1] - 'a']++;
        num[b[j - len + 1] - 'a']--;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00952 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n;
  cin >> n;
  int x[n];
  for (int i = 0; i < n; i++)
    scanf("%lld", x + i);
  set<int> s, u;
  for (int i = 0; i < n; i++) {
    if (s.count(0)) {
      cout << "No" << endl;
      continue;
    }
    int k = x[i], c = 0;
    while (s.count(k) && !u.count(k))
      k--, c++;
    if (u.count(k)) {
      if ((int)s.size() == (int)u.size() + c - 1) {
        s.insert(0);
        puts("Yes");
      } else {
        puts("No");
        for (int j = k; j <= x[i]; j++)
          u.insert(j);
      }
      continue;
    }
    if (k == 0) {
      if ((int)s.size() == c) {
        s.insert(0);
        puts("Yes");
      } else {
        puts("No");
        for (int j = k; j <= x[i]; j++)
          u.insert(j);
      }
      continue;
    }
    s.insert(k);
    puts("Yes");
    for (int j = x[i]; j > k; j--)
      s.erase(j);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n;
  cin >> n;
  int x[n];
  for (int i = 0; i < n; i++)
    scanf("%lld", x + i);
  unordered_set<int> s;
  int l = 0;
  for (int i = 0; i < n; i++) {
    // cout<<i<<":"<<x[i]<<endl;
    // cout<<"s:";for(int j:s) cout<<" "<<j;cout<<endl;
    // cout<<"u:";for(int j:u) cout<<" "<<j;cout<<endl;
    if (s.count(0)) {
      cout << "No" << endl;
      continue;
    }
    int k = x[i];
    while (s.count(k) && l <= k)
      k--;
    if (k < l) {
      puts("No");
      l = x[i];
      continue;
    }
    if (k == 0) {
      if ((int)s.size() == x[i]) {
        s.insert(0);
        puts("Yes");
      } else {
        puts("No");
        l = x[i];
      }
      continue;
    }
    s.insert(k);
    puts("Yes");
    for (int j = x[i]; j > k; j--)
      s.erase(j);
  }
  return 0;
}
Problem p00952 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
int b[500000], p = 1, cnt = 0;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (b[0] == 1 || x < p - 1) {
      cout << "No" << endl;
      continue;
    }
    if (p - 1 == x && cnt > x) {
      cout << "No" << endl;
      continue;
    }
    cnt++;
    cout << "Yes" << endl;
    if (x >= n)
      continue;
    b[x]++;
    while (x > 0 && b[x] >= 2)
      b[x] -= 2, b[x - 1]++, x--, cnt--;
    while (p < n && b[p] == 1)
      p++;
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
int b[500000], p = 1, cnt = 0;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (b[0] == 1 || x < p - 1) {
      cout << "No" << endl;
      continue;
    }
    if (p - 1 == x && cnt > x) {
      cout << "No" << endl;
      continue;
    }
    cnt++;
    cout << "Yes" << endl;
    if (x >= n)
      continue;
    b[x]++;
    int j = x;
    while (j > 0 && b[j] >= 2)
      b[j] -= 2, b[j - 1]++, j--, cnt--;
    p = 1;
    while (p < n && b[p] == 1)
      p++;
  }
}
Problem p00952 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5 * 1e5 + 12345;
int num[maxn];
set<int> s;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &num[i]);
  int max_dep = 1;
  for (int i = 1; i <= n; i++) {
    if (s.count(0) >= 1) {
      printf("No\\n");
      continue;
    }
    int tmp_num = num[i];
    while (s.count(max_dep)) {
      max_dep++;
    }
    if (max_dep - 1 < tmp_num) {
      printf("Yes\\n");
    } else if (max_dep - 1 > tmp_num) {
      printf("No\\n");
      continue;
    } else if (max_dep - 1 == tmp_num) {
      if (s.size() != max_dep - 1) {
        printf("No\\n");
        continue;
      } else {
        printf("Yes\\n");
      }
    }
    while (s.count(tmp_num))
      s.erase(tmp_num--);
    s.insert(tmp_num);
  }
}

Error: TLE

Fixed submission:

// created by missever

#include <bits/stdc++.h>
#define MAX 1000000007
using namespace std;
typedef long long LL;

const int maxn = 1e6 + 5;
int f[maxn], tot = 0;
bool vis = 1, g[maxn];
set<int> mp;

bool dfs(int x) {
  if (x >= maxn) {
    mp.insert(x);
    return true;
  }
  if (x == 0) {
    if (tot == 0) {
      vis = 0;
      return true;
    } else
      return false;
  }

  if (g[x] && mp.upper_bound(x) != mp.end())
    return false;

  if (f[x] == 1) {
    tot--;
    mp.erase(x);
    if (dfs(x - 1)) {
      f[x] = 0;
      return true;
    } else {
      tot++;
      mp.insert(x);
      return false;
    }
  } else {
    tot++;
    f[x] = 1;
    mp.insert(x);
    g[x] = g[x - 1];
    if (g[x]) {
      int y = x + 1;
      while (y < maxn && f[y] == 1)
        g[y++] = 1;
    }
    return true;
  }
}

int main() {
  int n, i, j, x;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &x);
    if (vis && dfs(x))
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}
Problem p00952 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int f[N], cur, s[N];

void pn() { puts("No"); }
void py() { puts("Yes"); }

void add(int x, int val) {
  if (!x)
    return;
  while (x < N) {
    s[x] += val;
    x += x & -x;
  }
}

int query(int x) {
  int ret = 0;
  while (x) {
    ret += s[x];
    x -= x & -x;
  }
  return ret;
}

void Add(int t) {
  while (t) {
    if (!f[t])
      break;
    f[t]--;
    add(t, -1);
    t--;
  }
  f[t]++;
  add(t, 1);
}

int main() {
  int n, x;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    if (f[0])
      pn();
    else {
      // printf("%di\\n", i);
      if (x >= N) {
        cur = 1;
        py();
      } else {
        // printf("%d %d\\n", x, query(x));
        if (query(x) == x) {
          if (query(N - 1) > x || cur)
            pn();
          else {
            py();
            Add(x);
          }
        } else {
          py();
          Add(x);
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int main() {
  int n, x, i, j, k, one, maxx;
  bool flag;
  cin >> n;
  one = 0;
  flag = false;
  mp[0] = 0;
  mp.clear();
  maxx = 1e9 + 7;
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (flag) {
      printf("No\\n");
      continue;
    }
    x = 1e9 - x;
    if (x >= maxx) {
      printf("No\\n");
      continue;
    }
    if (!mp.count(x)) {
      mp[x] = 1;
      one++;
    } else if (!mp[x]) {
      mp[x] = 1;
      one++;
    } else {
      for (j = x + 1; j <= 1e9; j++) {
        if (!mp.count(j))
          break;
        else if (!mp[j])
          break;
      }
      if (j == 1e9) {
        if (one == 1e9 - x) {
          printf("Yes\\n");
          flag = true;
          continue;
        }
        maxx = min(maxx, x);
        printf("No\\n");
        continue;
      }
      for (k = x; k < j; k++) {
        mp[k] = 0;
        one--;
      }
      mp[j] = 1;
      one++;
    }
    printf("Yes\\n");
  }
  return 0;
}
Problem p00952 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#define maxn 500300
using namespace std;

bitset<1000000006> S;

int n;
int a[maxn];

char s[10000];

inline void read(int &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}

bool full = 0;
int tot = 0;
int pre = 1;

int T = 0;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    // scanf("%d",&x);
    read(a[i]);
    int x = a[i];
    if (full) {
      puts("No");
      continue;
    }
    if (a[i] < pre) {
      puts("No");
      continue;
    }
    while (S.test(a[i])) {
      a[i]--;
      if (a[i] == 0)
        break;
    }
    if (a[i] == 0)
      if (tot == x) {
        puts("Yes");
        full = 1;
      } else
        puts("No");
    else {
      while (x != a[i]) {
        S.set(x, 0);
        tot--;
        if (tot < 0)
          return 0;
        x--;
      }
      tot++;
      while (S.test(pre))
        pre++;
      S.set(a[i], 1);
      puts("Yes");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#define maxn 500300
using namespace std;

bitset<1000000006> S;

int n;
int a[maxn];

char s[10000];

void read(int &x) {
  char ch = ' ';
  while ((ch > '9') || (ch < '0'))
    ch = getchar();
  int tmp = 0;
  while ((ch <= '9') && (ch >= '0')) {
    tmp = tmp * 10 + ch - '0';
    ch = getchar();
  }
  x = tmp;
}

bool full = 0;
int tot = 0;

int T = 0;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    // scanf("%d",&x);
    read(a[i]);
    int x = a[i];
    if (full) {
      puts("No");
      continue;
    }
    while (S.test(a[i])) {
      a[i]--;
      if (a[i] == 0)
        break;
    }
    if (a[i] == 0)
      if (tot == x) {
        puts("Yes");
        full = 1;
      } else
        puts("No");
    else {
      while (x != a[i]) {
        T++;
        if (T > n)
          return 0;
        S.set(x, 0);
        tot--;
        x--;
      }
      tot++;
      S.set(a[i], 1);
      puts("Yes");
    }
  }
  return 0;
}