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

// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << '\\n';
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++) {
    std::cout << i << ' ';
    vdeb(da[i]);
  }
  std::cout << '\\n';
}

using namespace std;

int solve() {
  int n;
  cin >> n;
  vector<ll> da(n);
  rip(i, n, 0) cin >> da[i];
  string s;
  cin >> s;
  vector<ll> bases(0);
  for (int i = n - 1; i > -1; --i) {
    ll now = da[i];
    rip(j, bases.size(), 0) chmin(now, now ^ bases[j]);
    if (s[i] == '0' && now > 0)
      bases.push_back(now);
    else if (s[i] == '1' && now > 0)
      return true;
  }
  return false;
}

int main() {
  int t;
  cin >> t;
  vector<bool> ans(t);
  rip(i, t, 0) ans[i] = solve();
  rip(i, t, 0) cout << ans[i] << '\\n';
}

Error: TLE

Fixed submission:

// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << '\\n';
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++) {
    std::cout << i << ' ';
    vdeb(da[i]);
  }
  std::cout << '\\n';
}

using namespace std;

bool solve() {
  int n;
  cin >> n;
  vector<ll> da(n);
  rip(i, n, 0) cin >> da[i];
  string s;
  cin >> s;
  set<ll> st;
  st.insert(0);
  rip(i, n, 0) {
    if (s[n - i - 1] == '0') {
      set<ll> tmp = st;
      for (auto j : st) {
        tmp.insert(j ^ da[n - i - 1]);
      }
      st = tmp;
    } else {
      if (!bool(st.count(da[n - i - 1])))
        return true;
    }
  }
  return false;
}

int main() {
  int t;
  cin >> t;
  vector<bool> ans(t);
  rip(i, t, 0) ans[i] = solve();
  rip(i, t, 0) cout << ans[i] << endl;
}