Bfs
Time limit1sMemory limit1024 MB
Boys push the tilt right and girls push it left, in an order that keeps the tilt within S. After each swap inside one group, decide whether a valid order still exists.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree, Prefix sum
- Solved
- No attempts yet
Problem
Kids from 402 heard the bell and ran outside to play. A tall pole stood in the middle of the field, so they came up with this game. At first the kids split into a group of girls and a group of boys, then they took positions somewhere on the field.
The pole starts completely vertical, with tilt . Each player has a force: is the force of the -th boy and is the force of the -th girl. The game runs step by step. At each step, either the boy or the girl closest to the pole (the one with the smallest index) throws a ball at the pole, then leaves the game. A boy's throw tilts the pole to the right, and a girl's throw tilts it to the left. Each throw changes the tilt by the thrower's force.
To keep the game from ending in disaster, the tilt of the pole must never exceed in either direction. The kids want to know whether some order of throws keeps the tilt within this limit.
The teacher picks moments and swaps the order of two consecutive boys or two consecutive girls. The swaps are permanent. After each swap, decide whether a valid order of throws exists.
Input
The first line contains three integers , , and .
The second line contains integers, the forces of the boys.
The third line contains integers, the forces of the girls.
The fourth line contains a single integer .
Each of the next lines has the form B x y or F x y. B means the swap is in the boys' group, F means it is in the girls' group, and and are the swapped positions.
Output
Print lines. Print if the game can be successful after the swap, or otherwise.
Constraints
- For every swap, .
- It is guaranteed that the game is successful before the first swap.