Data Structures Master

시간 제한2초메모리 제한2048 MB

요약
세 수열 중 하나에 값을 덧붙일 때마다, 세 위치의 최솟값과 최댓값이 이루는 구간에서 a의 최댓값을 모든 삼중항에 대해 더한 값을 구한다.
난이도

어려움10점 중 8점

유형
배열, 세그먼트 트리, 조합론, 누적 합
정답자
아직 제출이 없습니다

문제

Today, Esmaan decided to prove to the world that he is not ordinary. He went to take the exam to become a data structures master. But the first question of the exam stumped him. Help him solve the problem:

You have a sequence of integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n. In addition, you have three empty sequences: AA, BB, and CC.

  • Let f(ℓ,r)f(\ell, r) be the maximum among the numbers a_ℓ,a_ℓ+1,…,a_ra\_{\ell}, a\_{\ell + 1}, \ldots, a\_r.
  • Let g(p_1,p_2,p_3)g(p\_1, p\_2, p\_3) be f(min⁡(p_1,p_2,p_3),max⁡(p_1,p_2,p_3))f(\min(p\_1, p\_2, p\_3), \max(p\_1, p\_2, p\_3)).
  • Let SS be the sum of the values g(A_i,B_j,C_k)g(A\_i, B\_j, C\_k) for all possible combinations (i,j,k)(i, j, k) where 1≤i≤size(A)1 \le i \le \mathrm{size}(A), 1≤j≤size(B)1 \le j \le \mathrm size(B), and 1≤k≤size(C)1 \le k \le \mathrm{size}(C).

You need to perform qq queries of the following type:

  • "XX val\mathit{val}": add the value val\mathit{val} to the end of sequence XX.

After each query, output SS modulo 998,244,353998\\,244\\,353.

입력

The first line contains two integers nn and qq (1≤n,q≤1051 \le n, q \le 10^5): the number of elements in the sequence and the number of queries.

The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤1051 \le a\_i \le 10^5): the elements of the sequence.

Then follow qq lines, each containing a query in the format "XX val\mathit{val}" (X \in \\{A, B, C\\}, 1≤val≤n1 \le \mathit{val} \le n).

출력

After each query, output a line with a single integer: the current value of SS modulo 998,244,353998\\,244\\,353.

예제2

  1. 예제 1

    입력
    5 5
    2 2 9 1 10
    A 5
    A 1
    C 4
    B 1
    C 5
    
    예상 출력
    0
    0
    0
    19
    39
    
  2. 예제 2

    입력
    10 10
    5 6 5 5 10 4 8 9 5 4
    C 8
    C 8
    B 8
    B 2
    A 7
    C 7
    C 4
    B 4
    A 7
    B 5
    
    예상 출력
    0
    0
    0
    0
    38
    57
    77
    117
    234
    314