Curious Robin Hood


Submit solution

Points: 100 (partial)
Time limit: 2.0s
Java 8 4.0s
Python 3 4.0s
Memory limit: 64M
Java 8 128M
Python 3 128M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, BrainF***, C, C#, C++, Dart, Go, Java, JS, Kotlin, Lua, Pascal, Perl, Prolog, Python, Scala, Swift, VB

Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps N sacks where he keeps this money. The sacks are numbered from 0 to N-1.

Now each time he can he can do one of the three tasks.

Give all the money of the i-th sack to the poor, leaving the sack empty.
Add new amount (given in input) in the i-th sack.
Find the total amount of money from i-th sack to j-th sack.

Since he is not a programmer, he seeks your help.

Input

Input starts with an integer T \; (1 \leq T \leq 5), denoting the number of test cases. Each case contains two integers N \; (1 \leq N \leq 10^5) and Q \; (1 \leq Q \leq 5 \cdot 10^4). The next line contains N space separated integers in the range [0, 1000]. The i-th integer denotes the initial amount of money in the i-th sack (0 \leq i < N).

Each of the next Q lines contains a task in one of the following form:

  • 1 \; i Give all the money of the i-th (0 \leq i < N) sack to the poor.
  • 2 \; i \; v Add money v \; (1 \leq v \leq 1000) to the i-th (0 \leq i < N) sack.
  • 3 \; i \; j Find the total amount of money from i-th sack to j-th sack (0 \leq i \leq j < N).

Output

For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from i-th to j-th sack.

Sample input:

2
5 6
3 2 1 4 5
1 4
2 3 4
3 0 3
1 2
3 0 4
1 1
5 6
3 2 1 4 5
1 4
2 3 4
3 0 3
1 2
3 0 4
1 1

Sample output:

Case 1:
5
14
1
13
2
Case 2:
5
14
1
13
2

Comments


  • -1
    Pimienta  commented on Aug. 18, 2021, 1:39 p.m.

    Oye Zagaro, trata d buscar información sobre algo q se llama Segment Trees o sino sobre Fenwick Trees. Com el uso d esas estructuras es q sale el ejercicio. Saludos


  • -1
    Osnielfc_07  commented on Dec. 19, 2020, 5:13 p.m.

    ya gracias aniervs , de verdad que estoy cocinado.


  • -7
    Osnielfc_07  commented on Sept. 2, 2020, 4:35 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 1
      aniervs  commented on Sept. 22, 2020, 2:24 a.m.

      A qué te refieres? El segment tree que vas a user se construye en tiempo lineal en el tamaño del arreglo, así que construir el segment tree en cada caso de prueba es eficiente.


  • 0
    Kevin_Manzano1  commented on April 28, 2020, 2:53 p.m.

    Hay un caso que hay q añadir 0 xfavor q lo arreglen


  • -2
    Kevin_Manzano1  commented on April 18, 2020, 10:48 p.m.

    el caso 3 creo que tiene un problema?


    • 3
      aniervs  commented on April 18, 2020, 11:37 p.m. edit 2

      Si. Tenías un pequeño error en tu code que tal vez no influía en la respuesta, pero la razón del WA fue porque en el caso de prueba 3, hay una operación del tipo 2 que piden añadir 0 a una posición cuando está bien claro en las especificaciones del problema que el valor a añadir es mayor o igual que 1. Los admin deberían revisar el caso.


  • 9
    Primervirgen  commented on Aug. 15, 2019, 1:04 a.m. edited

    .


  • -8
    dmesadiaz  commented on Nov. 10, 2018, 1:38 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 3
      aniervs  commented on April 28, 2020, 3:12 p.m.

      Con ABI y te da TLE? Debes tener algo mal en el ABI, de hecho, estoy casi seguro q estás usando el ABI desde la posición 0.