RMT Subway Load Test
Time limit5sMemory limit512 MB
Each subway line is a cycle of stations; line operations rotate passenger counts around the cycle, and range-sum surveys must be answered online.
- Level
Hard8 of 10
- Topics
- Segment tree, Prefix sum, Implementation, Math
- Solved
- No attempts yet
Problem
Rail Metro Transit (RMT) runs an unusual subway system. There are subway stations numbered from to , and subway lines numbered from to . Every station belongs to exactly one line, and every line has at least one station.
Each line is circular. For a station , the next station after is the station of the same line whose number is the smallest among those larger than . If already has the largest number among the stations of its line, the next station after is the station of that line with the smallest number.
RMT is running a load test with volunteer passengers. When the test starts, one train stands at every station, and the train at station carries passengers. The volunteers stay on their own train until the test ends.
During the test, RMT performs actions in order. Each action is one of two kinds:
- a survey of the total number of passengers on the trains standing at stations through ,
- an operation of line , which sends every train of line to the next station of that line.
Report the answer of every survey.
Input
The first line contains three space separated integers , and (, ).
The second line contains integers (), where is the line that station belongs to. Every line has at least one station.
The third line contains integers (), the number of passengers at each station when the test starts.
Each of the next lines holds one action in one of these two forms:
1 l r, a survey of stations through ().2 x, an operation of line ().
Output
For every survey, print its answer on its own line.
Hint
The first example is drawn below. The stations are numbered from to , and every connection carries the number of the line it belongs to.

At the start the passenger counts are {1, 2, 3, 4, 5}. The first survey answers .
Operating line 1 changes the counts to {3, 2, 1, 4, 5}, so the second survey answers .
Operating line 2 changes the counts to {3, 5, 1, 2, 4}, so the third survey answers .
The second example is drawn below. The stations are numbered from to , and every connection belongs to line 1.

Just before the first survey the counts are {114, 101, 109}. Just before the second survey they are {109, 114, 101}, just before the third survey {101, 109, 114}, and just before the fourth survey {114, 101, 109}.