#K10021. Traffic Density Update
Traffic Density Update
Traffic Density Update
You are given n intersections with initial traffic densities given as an array of integers. Then, m events occur that affect the traffic density at a specified intersection. Each event is described by a string, which can be one of the two forms:
entry i x
: Increase the traffic density at intersection i by x.departure i x
: Decrease the traffic density at intersection i by x.
Note that the intersections are 1-indexed. In mathematical terms, if we denote the traffic density at the ith intersection as \(T_i\), then for an event "entry i x" or "departure i x" the update rule is:
[ T_i = \begin{cases} T_i + x, & \text{if the event is } \texttt{entry} \ T_i - x, & \text{if the event is } \texttt{departure} \end{cases} ]
Your task is to compute the final states of all intersections after all events have been processed.
inputFormat
The input is read from stdin and consists of the following:
- An integer
n
denoting the number of intersections. - A line with
n
space-separated integers representing the initial traffic densities at each intersection. - An integer
m
denoting the number of events. m
lines, each containing an event in one of the following forms:entry i x
ordeparture i x
, wherei
is the 1-indexed intersection number andx
is an integer value.
outputFormat
Output a single line to stdout with n
space-separated integers representing the final traffic densities at each intersection after processing all events.
4
10 20 30 40
5
entry 1 10
departure 2 5
entry 3 15
departure 4 20
entry 1 5
25 15 45 20