#C2133. Trading Game

    ID: 45416 Type: Default 1000ms 256MiB

Trading Game

Trading Game

You are given \(N\) players, each starting with a single item whose value is provided in an initial array. You must simulate a trading game with the following operations:

  • Add Operation (A u v): Add an item with value \(v\) to player \(u\) (1-indexed).
  • Remove Operation (R u k): Remove the \(k\)-th smallest item from player \(u\)'s inventory. Formally, if the sorted inventory of player \(u\) is \(a_1 \le a_2 \le \cdots \le a_m\), then remove \(a_k\) if \(k \le m\); otherwise, ignore this operation.
  • Maximum Query (QMAX l r): Among players in the range \([l, r]\) (inclusive), output the player ID with the maximum total item value. In case of ties, choose the one with the smallest index.
  • Minimum Query (QMIN l r): Among players in the range \([l, r]\) (inclusive), output the player ID with the minimum total item value. In case of ties, choose the one with the smallest index.

Each player \(i\) (1-indexed) initially holds one item with the given value. Process the queries sequentially and for each query of type QMAX or QMIN, print the answer on a new line.

inputFormat

The input is given in the following format:

N Q
v1 v2 ... vN
query1
query2
...
queryQ

Where:

  • The first line contains two integers \(N\) and \(Q\), the number of players and the number of queries.
  • The second line contains \(N\) integers representing the initial item values for each player.
  • Each of the next \(Q\) lines contains a query in one of the following formats:
    • A u v: Add an item with value \(v\) to player \(u\).
    • R u k: Remove the \(k\)-th smallest item from player \(u\)'s inventory.
    • QMAX l r: Query the player with the maximum total value among players from \(l\) to \(r\).
    • QMIN l r: Query the player with the minimum total value among players from \(l\) to \(r\).

outputFormat

For each query of type QMAX or QMIN, output a single line with the corresponding player's ID (1-indexed).

## sample
3 5
10 20 30
A 2 5
QMAX 1 3
R 2 1
QMIN 1 3
QMIN 2 3
3

1 2

</p>