#P11572. Axis Intersection Query on Cumulative Vectors
Axis Intersection Query on Cumulative Vectors
Axis Intersection Query on Cumulative Vectors
We are given ( n ) 2D vectors ( v_i = (x_i, y_i) ) in the Cartesian plane. Define ( P_0 = (1,1) ) and for ( 1 \le i \le n ), ( P_i = P_{i-1} + v_i ). There is a pointer variable ( pos ) initially set to (1). Then ( m ) operations are performed. The operations are as follows:
- B: Update \( pos \) to \( \max(1, pos - 1) \).
- F: Update \( pos \) to \( \min(n, pos + 1) \).
- C x y: Change the current vector \( v_{pos} \) to \( (x,y) \). (It is guaranteed that at every moment, for every \( 1 \le i \le n \), both \( x_i \) and \( y_i \) are even integers.)
- Q: Query and output the value of \( \displaystyle \sum_{i=1}^{n} f(i) \), where for each \( i \), \( f(i) \) is defined as the number of times the segment \( P_{i-1}P_i \) intersects the coordinate axes.
Note: In particular, if the segment \( P_{i-1}P_i \) passes through the origin, it is considered to intersect both axes.
A segment ( \overline{AB} ) (with ( A=(a_x,a_y) ) and ( B=(b_x,b_y) )) is considered to intersect the x-axis if ( a_y ) and ( b_y ) have opposite signs (i.e. ( a_y \cdot b_y < 0 )). Similarly, it intersects the y-axis if ( a_x ) and ( b_x ) have opposite signs (i.e. ( a_x \cdot b_x < 0 )). Since the endpoints ( P_i ) are always odd, they never lie exactly on an axis. Therefore, each segment contributes ( f(i)=\text{(1 if y-axis is crossed)} + \text{(1 if x-axis is crossed)} ).
inputFormat
The first line contains two integers ( n ) and ( m ) separated by space.
The next ( n ) lines each contain two even integers representing ( x_i ) and ( y_i ) for ( 1 \le i \le n ).
Each of the following ( m ) lines represents an operation, which can be one of the following forms:
- B
- F
- C x y
- Q
It is guaranteed that throughout the operations, the vectors always have even coordinates.
outputFormat
For each ( Q ) operation, output a single integer on a separate line representing ( \displaystyle \sum_{i=1}^{n} f(i) ) at that moment.
sample
3 5
2 2
-4 0
6 -2
Q
F
Q
C -2 -2
Q
2
2
1
</p>