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

struct op {
  ll n, o, x, cnt = 0;
};
const int MAXN = 2e5 + 1;
int n, d, a[MAXN];
op t[2 * MAXN];


ll makeAnd(int a, int b) {
  return (a & b);
}
ll makeOr(int a, int b) {
  return (a | b);
}
ll makeXor(int a, int b) {
  return (a ^ b);
}
void build(int v, int tl, int tr) {
  if(tl == tr) {
    t[v].n = a[tl], t[v].o = a[tl], t[v].x = a[tl];
    if(a[tl] == d)
      t[v].cnt++;
  }
  else {
    int tm = tl + (tr - tl) / 2, rightN = v + 2 * (tm - tl + 1);
    build(v + 1, tl, tm);
    build(rightN, tm + 1, tr);
    t[v].n = makeAnd(t[v + 1].n, t[rightN].n);
    t[v].o = makeOr(t[v + 1].o, t[rightN].o);
    t[v].x = makeXor(t[v + 1].x, t[rightN].x);
    t[v].cnt = t[v + 1].cnt + t[rightN].cnt;
    if(t[v].n == d && t[v].o == d && t[v].x == d)
      t[v].cnt++;
  }
}
void solve() {
  cin >> n >> d;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  build(1, 1, n);
  ll ans = 0;
  for(int i = 1; i < 2 * n; i++) {
    ans = max(ans, t[i].cnt);
  }
  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
13 ms
13.4 MB
Wrong Answer