This page is still under construction.

Parts of this page are still being built. What you see may change.

Mountains

Time limit3sMemory limit256 MB

Summary
Maintain a piecewise-constant sequence of elevation changes under range assignments, and after each update find the first prefix-sum position whose elevation exceeds a query height h.
Level

Hard8 of 10

Topics
Segment tree, Binary search, Prefix sum, Array
Solved
No attempts yet

Problem

A new roller-coaster simulator has been installed at an amusement park. The simulated track is a sequence of nn rails joined end to end, with the start of the first rail fixed at elevation 00.

The simulator stores the track as a sequence of nn elevation changes d1,d2,…,dnd_1, d_2, \dots, d_n. Here did_i is the elevation change (in centimetres) over the ii-th rail: if a car is at elevation ee after traversing i−1i-1 rails, then after traversing the ii-th rail it is at elevation e+die + d_i. Thus the start is at elevation 00, and the elevation after ii rails is d1+d2+⋯+did_1 + d_2 + \dots + d_i.

Initially every rail is horizontal; that is, di=0d_i = 0 for all ii.

Throughout the day two kinds of events are interleaved.

  • Reconfiguration: given three integers aa, bb, and DD. For every rail ii with a≤i≤ba \le i \le b, the elevation change is set to di=Dd_i = D. The elevation change over every other rail is unchanged. The start stays at elevation 00, and the rest of the track is shifted up or down as needed so that it remains connected.
  • Ride: given one integer hh. A car is launched with just enough energy to reach height hh. The car keeps moving as long as the elevation of the track does not exceed hh and the end of the track has not been reached.

For each ride, determine the number of rails the car fully traverses before it stops.

Input

The first line contains the number of rails nn (1≤n≤1091 \le n \le 10^9).

The following lines contain reconfigurations and rides interleaved, followed by an end marker. Each line is one of:

  • Reconfiguration: the letter I and three integers aa, bb, DD separated by single spaces (1≤a≤b≤n1 \le a \le b \le n, −109≤D≤109-10^9 \le D \le 10^9). It sets di=Dd_i = D for every rail a≤i≤ba \le i \le b.
  • Ride: the letter Q and an integer hh separated by a single space (0≤h≤1090 \le h \le 10^9).
  • A single letter E: the end marker denoting the end of the input.

You may assume that at any moment the elevation of every point on the track lies in the interval [0,109][0, 10^9] centimetres. The input contains at most 100 000100\,000 lines.

In 50%50\% of the test cases, nn satisfies 1≤n≤20 0001 \le n \le 20\,000 and there are at most 1 0001\,000 lines of input.

Output

For each ride, print on its own line a single integer — the number of rails the car traverses. The ii-th line corresponds to the ii-th ride.

Hint

The track before and after each reconfiguration. The x axis denotes the rail number; the y axis and the numbers above points denote elevation; the numbers above segments denote elevation changes.

Examples2

  1. Example 1

    Input
    4
    Q 1
    I 1 4 2
    Q 3
    Q 1
    I 2 2 -1
    Q 3
    E
    
    Expected output
    4
    1
    0
    3
    
  2. Example 2

    Input
    5
    I 1 5 2
    Q 7
    Q 8
    Q 10
    Q 1
    E
    
    Expected output
    3
    4
    5
    0