[Codeforces 1058E] Vasya and good sequences

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.