luogu#P2174. 小Z的神奇数列

小Z的神奇数列

Background

Xiao Z has been studying sequences. He wants to know, for the sequence he studies, what the maximum number is (Max), what the minimum number is (Min), what Max to the power of Min (Max^Min) is, and what the product of all numbers is. Of course, such questions are no problem for Xiao Z. However, he recently had a sudden idea to explore deeper properties of this sequence, so he decided to keep deleting some numbers from the sequence and study the current sequence after each deletion. Since the sequence has many terms, this causes a lot of trouble for Xiao Z, so he asks you to write a program to perform the following operations.

Problem Description

You need to maintain a multiset that supports five operations:

  • D x Delete xx; it is guaranteed that xx exists. If there are multiple occurrences, delete only one.
  • B Query the maximum value in the set.
  • S Query the minimum value in the set.
  • M Let the maximum be aa and the minimum be bb; query abmod317847191a^b \bmod 317847191.
  • T Query the product of all numbers, modulo 317847191317847191.

For all queries, the multiset is guaranteed to be non-empty.

Input Format

The first line contains two positive integers n,mn, m, the initial multiset size and the number of operations.
The second line contains nn positive integers aia_i, the initial multiset.
Then mm lines follow, each describing one operation.

Output Format

For each query, output one integer per line representing the answer.

3 6
2 6 9
M
D 9
B
S
M
T
81
6
2
36
12

Hint

Constraints
For partial testdata, 1n10001 \le n \le 1000, 1m1001 \le m \le 100, 1ai4001 \le a_i \le 400.
For 100%100\% of the testdata, 1n,m1061 \le n, m \le 10^6, 1ai1081 \le a_i \le 10^8.

Translated by ChatGPT 5