defmodule Main do def main do [n, q] = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) an = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.with_index() |> Enum.reduce(%{}, &(Map.put(&2, elem(&1, 1), String.to_integer(elem(&1, 0))))) queries = for _ <- 0..(q-1) do IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(fn v when v in ["A", "B"] -> v v -> String.to_integer(v) end) end solve(n, q, an, queries) |> Enum.join(" ") |> IO.puts() end def solve(n, _q, an, queries) do {_, bn} = Enum.reduce(queries, {an, Enum.reduce(0..(n-1), %{}, &(Map.put(&2, &1, 0)))}, fn ["A", x, y], {an, bn} -> {Map.put(an, x - 1, an[x - 1] + y), bn} ["B", x, y], {an, bn} -> {an, Enum.reduce((x - 1)..(y - 1), bn, &(Map.put(&2, &1, bn[&1] + an[&1])))} end) (n-1)..0 |> Enum.reduce([], fn i, acc -> [bn[i] | acc] end) end end