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

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

void buff() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
// End of template code!
constexpr int mx = 64;

struct B {
  vector<long long> basis;
  B() { basis.assign(64, 0ll); }
  void insert_num(long long num) {
    for (int i = 0; i < mx; ++i) {
      if ((num & 1ll << i) == 0)
        continue;
      if (!basis[i]) {
        basis[i] = num;
        return;
      }
      num ^= basis[i];
    }
  }
  bool contains(long long num) {
    for (int i = 0; i < mx; ++i) {
      if ((num & 1ll << i) == 0)
        continue;
      if (basis[i] == 0) {
        return true;
      }
      num ^= basis[i];
    }
    return false;
  }
  void insert_vec(vector<long long> v) {
    for (auto &x : v)
      insert_num(x);
  }
};

int main() {
  buff();
  int t;
  cin >> t;
  while (t--) {

    int n;
    cin >> n;
    vector<long long> v(n);
    for (auto &x : v)
      cin >> x;
    string s;
    cin >> s;

    vector<B> dp(n + 1);
    bool win = false;
    for (int i = n - 1; i >= 0; --i) {
      if (s[i] == '0') {
        dp[i] = dp[i + 1];
        dp[i].insert_num(v[i]);
      } else {
        if (dp[i + 1].contains(v[i])) {
          win = true;
          break;
        } else
          dp[i] = dp[i + 1];
      }
    }
    if (win)
      cout << 1 << '\\n';
    else
      cout << 0 << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

void buff() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
// End of template code!
int main() {
  buff();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<long long> v(n);
    for (auto &x : v)
      cin >> x;
    string S;
    cin >> S;

    unordered_set<long long> f, s;
    f.insert(0);
    s.insert(0);
    for (int i = 0; i < n; ++i) {
      if (S[i] == '0') {
        set<long long> bota;
        for (const auto &x : f)
          bota.insert(x ^ v[i]);
        for (const auto &b : bota)
          f.insert(b);
      } else {
        set<long long> bota;
        for (const auto &x : s)
          bota.insert(x ^ v[i]);
        for (const auto &b : bota)
          s.insert(b);
      }
    }
    bool f_win = false;
    for (auto &x : f) {
      if (s.find(x) == s.end()) {
        f_win = true;
        break;
      }
    }
    cout << (f_win ? 1 : 0) << '\\n';
  }
  return 0;
}