Source Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAXN = 2e5 + 1;
int n, d, a[MAXN];
pair<int, int> t[2 * MAXN];

int makeAnd(int a, int b) {
  return (a & b);
}
int makeOr(int a, int b) {
  return (a | b);
}
int makeXor(int a, int b) {
  return (a ^ b);
}

void build(int v, int tl, int tr) {
  if(tl == tr) {
    t[v].first = a[tl];
    if(t[v].first == d)
      t[v].second = 1;
  }
  else {
    int tm = tl + (tr - tl) / 2;
    build(v + 1, tl, tm);
    build(v + 2 * (tm - tl + 1), tm + 1, tr);
    if(t[v + 1].first == -1 || t[v + 2 * (tm - tl + 1)].first == -1)
      t[v] = {-1, max(t[v + 1].second, t[v + 2 * (tm - tl + 1)].second)};
    else {
      int rightN = v + 2 * (tm - tl + 1);
      int anding = makeAnd(t[v + 1].first, t[rightN].first),
          oring = makeOr(t[v + 1].first, t[rightN].first), 
          xoring = makeXor(t[v + 1].first, t[rightN].first);
      if(anding == d && oring == d && xoring == d)
        t[v] = {d, t[v + 1].second + t[rightN].second + 1};
      else 
        t[v] = {-1, max(t[v + 1].second, t[rightN].second)};
    }
  }
}
void solve() {
  cin >> n >> d;
  for(int i = 0; i < n; i++) {
    cin >> a[i];
  }
  build(1, 1, n);
  int ans = 0;
  for(int i = 1; i < 2 * n; i++) {
    if(ans < t[i].second) 
      ans = t[i].second;
  }
  cout << ans;
}
 
int main(){ 
  #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
  #endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int TC = 1;   
  // cin >> TC;
  while(TC--) {
    solve();
  }
  return 0;
}
Copy
Legendary Ammar_Lahloh
GNU G++17
3 ms
836 KB
Wrong Answer