[Topic link]
Https://codeforces.com/contest/1058/problem/E
Algorithm
Obviously, we just need to consider the binary representation of each number in the sequence as the number of the next 1. It may be useful to make the AI represent the number of binary in the number of the next 1.
A sub-sequence [L,r] is "good" when and only if:
1. sigma{Ai} (L <= i <= R) is even
2. max{ai} (L <= i <= R) <= sigma{ai}/2
Enumeration sequence left endpoint L, can be suffixed and processed R
Time complexity: O (N)
Code
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 3e5 +Ten;intN;intcnt[maxn][2];intA[MAXN];Long Longans;template<typename t> InlinevoidRead (T &x) {T F=1; x =0; Charc =GetChar (); for(;!isdigit (c); c = GetChar ())if(c = ='-') F =-F; for(; IsDigit (c); c = GetChar ()) x = (x <<3) + (x <<1) + C-'0'; X*=F;}intMain () {read (n); for(inti =1; I <= N; i++) { Long Longx; Read (x); while(X >0) {A[i]+ = x &1; X>>=1; } } intSuf =0; Cnt[n+1][0] =1; for(inti = n; I >=1; i--) { intsum =0, mx =0; intAdd =0; for(intj = i; J <= N && j-i < $; J + +) {sum+=A[j]; MX=Max (mx,a[j]); if(Sum%2==0&& mx > Sum-mx) add--; } Suf+=A[i]; Add+ = Cnt[i +1][suf &1]; Ans+=add; cnt[i][0] = Cnt[i +1][0]; cnt[i][1] = Cnt[i +1][1]; if(Suf &1) cnt[i][1]++; Elsecnt[i][0]++; } printf ("%i64d\n", ans); return 0; }
[Codeforces 1058E] Vasya and good sequences