Ruka
Time limit2sMemory limit512 MB
Update vectors of a polyline through cursor commands and report how many segments cross the coordinate axes.
- Level
Medium7 of 10
- Topics
- Segment tree, Prefix sum
- Solved
- No attempts yet
Problem
Stjepan is programming a robot arm that draws on a blackboard with a piece of chalk. The blackboard is a standard coordinate plane: the coordinate grows to the right and the coordinate grows upwards.
A plan for the arm is an array of exactly vectors , where every and every is an even integer. The arm executes the plan from the point in steps. In step it moves the chalk in a straight line from the current point to the point . The mark left on the blackboard is a broken line whose segments are the given vectors.
While Stjepan writes and changes the plan, he sometimes wants to know how many times the chalk goes over the coordinate axes. Write a program that simulates the changes to the plan and answers Stjepan's queries.

The plan at each Q command of the second example. The dotted line marks the segment that was changed most recently.
Suppose Stjepan keeps the plan in a text file of lines, where line holds the vector . The cursor starts on the first line of the file. Your program simulates the following commands.
B: the cursor moves to the previous line. If it is already on the first line, nothing happens.F: the cursor moves to the next line. If it is already on the last line, nothing happens.C nx ny: here and are even integers. The vector on the cursor line is replaced with the vector .Q: print how many times the broken line drawn by the current plan goes over the coordinate axes. If the broken line passes through the origin, count that as going over the axes twice.
Input
The first line contains the integer , the number of vectors in the plan. Line of the following lines contains two even integers and separated by a single space, the coordinates of the -th vector of the initial plan.
The next line contains the integer , the number of commands to simulate. Each of the following lines contains one command. A command is one of the uppercase letters B, F, Q, or an expression of the form C nx ny with the even integers and described above.
Output
For each Q command in the input, print its result on a single line. Print the results in the order in which the commands appear in the input.
Constraints
- For every vector of the initial plan and every new vector given by a
Ccommand, , , , are even and lie between and , inclusive.