#P5848. [IOI 2005] mou

[IOI 2005] mou

Description

An amusement park has started operating a brand new simulated roller coaster. The simulated track consists of nn rail segments, and the first and last segments are connected to form a loop. The first rail segment starts at height 00.

The operator Byteman can modify the track by adjusting the heights of several consecutive rail segments. The heights of the segments before the modified part are not affected. Each time the rail heights are adjusted, the segments after the modified part must be raised or lowered to keep the track connected, and the starting height must remain 00. The next page illustrates the modification process.

At the start of each run, the car has enough energy to reach height hh. That is, as long as the height of the track does not exceed hh, the car keeps moving, even until it finishes.

Given the daily runs and modifications, for each run compute how many rail segments the car passes before it stops.

The rails are represented as a sequence of nn numbers, where each number corresponds to one rail segment. The ii-th value did_i denotes the height change on the ii-th segment. In other words, if the car’s height is hh before reaching segment ii, then after passing segment ii, the height becomes h+dih+d_i.

Initially, the track is a horizontal line, meaning di=0d_i=0 for all ii. Runs and modifications are interleaved. Each modification is given by three numbers: aa, bb, and DD, meaning that all did_i for ii from aa to bb (inclusive) are set to di=Dd_i=D. Each run is given a number hh, the maximum height the car can reach.

Input Format

The first line of input contains a positive integer nn, the number of rail segments.

The following lines contain modifications and runs, each starting with an identifier:

  • Modification: a letter I, followed by integers aa, bb, and DD, separated by single spaces.
  • Run: a letter Q, followed by an integer hh, separated by a single space.
  • A letter E: the termination symbol, indicating the end of input.

You may assume that at any time, the height of any rail segment is within the range [1,1000000000][1,1000000000].

The input contains no more than 1000010000 lines.

Output Format

For the ii-th run, output one integer per line: the number of rail segments passed in that run.

4
Q 1
I 1 4 2
Q 3
Q 1
I 2 2 -1
Q 3
E

4
1
0
3

Hint

For 50%50\% of the testdata, 1n2×1041 \le n \le 2 \times 10^4, and the input contains no more than 10001000 lines.

For 100%100\% of the testdata, 1n1091 \le n \le 10^9, 1a,bn1 \le a,b \le n, 109D109- 10^9 \le D \le 10^9, 0h1090 \le h \le 10^9.

Translated by ChatGPT 5