CF1766E.Decomposition

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For a sequence of integers [x1,x2,…,xk][x_1, x_2, \dots, x_k], let's define its decomposition as follows:

Process the sequence from the first element to the last one, maintaining the list of its subsequences. When you process the element xix_i, append it to the end of the first subsequence in the list such that the bitwise AND of its last element and xix_i is greater than 00. If there is no such subsequence in the list, create a new subsequence with only one element xix_i and append it to the end of the list of subsequences.

For example, let's analyze the decomposition of the sequence [1,3,2,0,1,3,2,1][1, 3, 2, 0, 1, 3, 2, 1]:

  • processing element 11, the list of subsequences is empty. There is no subsequence to append 11 to, so we create a new subsequence [1][1];
  • processing element 33, the list of subsequences is [[1]][[1]]. Since the bitwise AND of 33 and 11 is 11, the element is appended to the first subsequence;
  • processing element 22, the list of subsequences is [[1,3]][[1, 3]]. Since the bitwise AND of 22 and 33 is 22, the element is appended to the first subsequence;
  • processing element 00, the list of subsequences is [[1,3,2]][[1, 3, 2]]. There is no subsequence to append 00 to, so we create a new subsequence [0][0];
  • processing element 11, the list of subsequences is [[1,3,2],[0]][[1, 3, 2], [0]]. There is no subsequence to append 11 to, so we create a new subsequence [1][1];
  • processing element 33, the list of subsequences is [[1,3,2],[0],[1]][[1, 3, 2], [0], [1]]. Since the bitwise AND of 33 and 22 is 22, the element is appended to the first subsequence;
  • processing element 22, the list of subsequences is [[1,3,2,3],[0],[1]][[1, 3, 2, 3], [0], [1]]. Since the bitwise AND of 22 and 33 is 22, the element is appended to the first subsequence;
  • processing element 11, the list of subsequences is [[1,3,2,3,2],[0],[1]][[1, 3, 2, 3, 2], [0], [1]]. The element 11 cannot be appended to any of the first two subsequences, but can be appended to the third one.

The resulting list of subsequences is [[1,3,2,3,2],[0],[1,1]][[1, 3, 2, 3, 2], [0], [1, 1]].

Let f([x1,x2,…,xk])f([x_1, x_2, \dots, x_k]) be the number of subsequences the sequence [x1,x2,…,xk][x_1, x_2, \dots, x_k] is decomposed into.

Now, for the problem itself.

You are given a sequence [a1,a2,…,an][a_1, a_2, \dots, a_n], where each element is an integer from 00 to 33. Let a[i..j]a[i..j] be the sequence [ai,ai+1,…,aj][a_i, a_{i+1}, \dots, a_j]. You have to calculate ∑i=1n∑j=inf(a[i..j])\sum \limits_{i=1}^n \sum \limits_{j=i}^n f(a[i..j]).

对于整数序列 [x1,x2,…,xk][x_1, x_2, \dots, x_k],我们定义其分解如下:

从第一个元素开始依次处理该序列,同时维护一个子序列列表。当处理元素 xix_i 时,将其追加到当前子序列列表中第一个满足条件的子序列末尾:该子序列最后一个元素与 xix_i 的按位与(bitwise AND)结果大于 00。若列表中不存在满足该条件的子序列,则新建一个仅含单个元素 xix_i 的子序列,并将其追加到子序列列表末尾。

例如,我们分析序列 [1,3,2,0,1,3,2,1][1, 3, 2, 0, 1, 3, 2, 1] 的分解过程:

  • 处理元素 11:子序列列表为空,无法追加,因此新建子序列 [1][1];
  • 处理元素 33:子序列列表为 [[1]][[1]];由于 3&1=1>03 \mathbin{\&} 1 = 1 > 0,将 33 追加至第一个子序列;
  • 处理元素 22:子序列列表为 [[1,3]][[1, 3]];由于 2&3=2>02 \mathbin{\&} 3 = 2 > 0,将 22 追加至第一个子序列;
  • 处理元素 00:子序列列表为 [[1,3,2]][[1, 3, 2]];由于对任意非零整数 yy 都有 y&0=0y \mathbin{\&} 0 = 0,故无法追加,新建子序列 [0][0];
  • 处理元素 11:子序列列表为 [[1,3,2],[0]][[1, 3, 2], [0]];因 1&2=01 \mathbin{\&} 2 = 0、1&0=01 \mathbin{\&} 0 = 0,均不满足条件,故新建子序列 [1][1];
  • 处理元素 33:子序列列表为 [[1,3,2],[0],[1]][[1, 3, 2], [0], [1]];因 3&2=2>03 \mathbin{\&} 2 = 2 > 0,将 33 追加至第一个子序列;
  • 处理元素 22:子序列列表为 [[1,3,2,3],[0],[1]][[1, 3, 2, 3], [0], [1]];因 2&3=2>02 \mathbin{\&} 3 = 2 > 0,将 22 追加至第一个子序列;
  • 处理元素 11:子序列列表为 [[1,3,2,3,2],[0],[1]][[1, 3, 2, 3, 2], [0], [1]];检查前两个子序列末尾元素 22 和 00:1&2=01 \mathbin{\&} 2 = 0,1&0=01 \mathbin{\&} 0 = 0,均不满足;但 1&1=1>01 \mathbin{\&} 1 = 1 > 0,故可追加至第三个子序列。

最终得到的子序列列表为 [[1,3,2,3,2],[0],[1,1]][[1, 3, 2, 3, 2], [0], [1, 1]]。

令 f([x1,x2,…,xk])f([x_1, x_2, \dots, x_k]) 表示序列 [x1,x2,…,xk][x_1, x_2, \dots, x_k] 经上述过程分解所得的子序列个数。

现在进入本题正文:

给定一个长度为 nn 的序列 [a1,a2,…,an][a_1, a_2, \dots, a_n],其中每个元素均为 00 到 33 之间的整数。记 a[i..j]a[i..j] 表示子序列 [ai,ai+1,…,aj][a_i, a_{i+1}, \dots, a_j]。你需要计算

∑i=1n∑j=inf(a[i..j]).\sum \limits_{i=1}^n \sum \limits_{j=i}^n f(a[i..j]).

输入格式

The first line contains one integer nn (1≤n≤3⋅1051 \le n \le 3 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤30 \le a_i \le 3).

第一行包含一个整数 nn(1≤n≤3⋅1051 \le n \le 3 \cdot 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤30 \le a_i \le 3)。

输出格式

Print one integer, which should be equal to ∑i=1n∑j=inf(a[i..j])\sum \limits_{i=1}^n \sum \limits_{j=i}^n f(a[i..j]).

输出一个整数,其值应等于 ∑i=1n∑j=inf(a[i..j])\sum \limits_{i=1}^n \sum \limits_{j=i}^n f(a[i..j])。

输入输出样例

  • 输入#1

    8
    1 3 2 0 1 3 2 1

    输出#1

    71
  • 输入#2

    5
    0 0 0 0 0

    输出#2

    35

输入解题思路,AI测评打分。不知道怎么写?

首页