Rigged ㄱ Bomb Game
Time limit1sMemory limit256 MB
Boards hold A, B, and C bombs, and each query asks who wins the combined partizan game on a range of boards after swapping A and B on K of them.
- Level
Hard9 of 10
- Topics
- Game theory, Segment tree, Math
- Solved
- No attempts yet
Rigged ㄱ Bomb Game
Ahgus and Bagus are playing the ㄱ Bomb Game.
The ㄱ Bomb Game is played on boards. The -th board is a rectangle with width and height of and , respectively, made of 1×1 square cells.
Each cell holds one ㄱ bomb, and each bomb is one of three types: A, B, or C. Only Ahgus can choose an A bomb, only Bagus can choose a B bomb, and nobody can choose a C bomb.
When an ㄱ bomb explodes, it ignites the bombs to its left and below. An ignited bomb explodes right away and ignites the bombs to its own left and below. For example, if you detonate the bomb in the top-right cell of a board, every bomb on that board explodes. When the chain ends and no more bombs explode, the turn of the current player is over.
Ahgus always goes first. The two players alternate turns, and on each turn a player detonates one of the bombs they are allowed to choose. The player who has no bomb left to detonate on their turn loses.
Both players swapped some bombs to gain an advantage. On every board, there is exactly one bomb of type A or B, or of both types combined.
A stonejjun, an alchemist, appeared and proposed playing games. In each game, he selects boards and swaps the A and B bombs on each of them. He also chooses two integers and and hides every board except those numbered from to inclusive, so only those boards can be used.
Angered by this, Ahgus and Bagus decide to determine the winner without playing. If both players make the best possible choices, determine who wins each game.
Input
The first line contains .
For each of the boards, one line contains two positive integers and . Below that, lines of characters each describe the -th board, starting from the top-left cell.
The next line contains , the number of games. Each of the following lines contains three positive integers , , and .
Output
For each of the games, print the winner, either Ahgus or Bagus, on its own line.
Constraints
- Every character on every board is A, B, or C. On each board, exactly one character is A or B, or the board has one bomb of each kind that is counted as a single bomb.