#P9461. 「EZEC-14」众数 II

    ID: 10346 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>洛谷原创O2优化洛谷月赛链表单调栈

「EZEC-14」众数 II

Background

dXqwq is an unlovely boy. In the mode problem of NOI2022, he defined 10610^6 std::deque and successfully got MLE.

Problem Description

Given a sequence aa of length nn, we construct a sequence bb in the following way:

  • Initially, bb is an empty sequence.
  • For i=1,2,,ni=1,2,\cdots,n, we append 1,2,,ai1,2,\cdots,a_i to the end of bb in order.

dXqwq defines the minimum mode of a sequence as the smallest value among all numbers with the maximum frequency. For example, the minimum mode of [1,1,4,5,1,4][1,1,4,5,1,4] is 11, and the minimum mode of [1,14,5,14,19,19,8,10][1,14,5,14,19,19,8,10] is 1414.

You need to compute the sum of the minimum mode over every subarray of bb. Since the answer may be very large, output it modulo 998244353998244353.

Input Format

The first line contains an integer nn.

The second line contains nn integers aia_i.

Output Format

Output one integer, representing the sum of the minimum mode over every subarray of bb modulo 998244353998244353.

3
1 2 3
28
9
9 9 8 2 4 4 3 5 3
1912

Hint

[Sample Explanation]

In the first sample, b=[1,1,2,1,2,3]b=[1,1,2,1,2,3].

There are 1515 subarrays whose minimum mode is 11, 55 subarrays whose minimum mode is 22, and 11 subarray whose minimum mode is 33. Therefore, the answer is 15×1+5×2+1×3=2815\times 1+5\times 2+1\times 3=28.

[Hint]

Allocating 10610^6 std::deque will definitely cause MLE under a 512MB memory limit.

[Constraints]

This problem uses bundled testdata.

  • Subtask 1 (10 pts): ai100\sum a_i\leq 100.
  • Subtask 2 (20 pts): ai103\sum a_i\leq 10^3.
  • Subtask 3 (20 pts): ai106\sum a_i\leq 10^6.
  • Subtask 4 (10 pts): n2n\leq 2.
  • Subtask 5 (20 pts): n103n\leq 10^3.
  • Subtask 6 (10 pts): ai2a_i\leq 2.
  • Subtask 7 (10 pts): no special constraints.

For 100%100\% of the testdata, 1n1061\leq n\leq 10^6, 1ai1061\leq a_i\leq 10^6.

Translated by ChatGPT 5