I want to be the very best too!
Time limit5sMemory limit512 MB
Count distinct Pokémon types reachable from a start cell through trainers of level at most L, with point updates that change a cell's type.
- Level
Hard9 of 10
- Topics
- Graph, Union-find, Hash map, Divide and conquer
- Solved
- No attempts yet
Problem
Ash Ketchum became the very best, and Mr. Panda wants to follow in his footsteps. He wants to catch as many different types of Pokémon as possible to fill his Pokédex. Other trainers stand in his way, and he has to defeat them to reach his goal.
Mr. Panda and the Pokémon live in a world shaped like an grid. Mr. Panda can only move from one square to an adjacent square. The top left square is and the bottom right square is .
Each square has a trainer. To pass through a square, Mr. Panda must defeat its trainer, and then he obtains that trainer's Pokémon. Every trainer has a different level, and the trainer with the higher level is stronger. The trainer in square has level and battles with a Pokémon of type .
Trainers sometimes switch the type of Pokémon they use to improve their chances of winning. Mr. Panda wants to plan when to start his expedition. At certain times he asks: if he starts at square and can only defeat trainers of level at most , how many different Pokémon types can he obtain? He cannot pass through a square whose trainer he cannot defeat. Obtaining the same type from several trainers counts as one type.
Input
The first line contains three positive integers , , and . It is guaranteed that . Then lines follow.
The first lines each contain integers. The -th integer on the -th line is , where . All values are distinct.
The next lines each contain integers. The -th integer on the -th line is , where .
The last lines are queries, each with 4 integers.
- If the first integer is 1, the query is type 1 and the next three integers are , , . The trainer at changes to use Pokémon of type , where .
- If the first integer is 2, the query is type 2 and the next three integers are , , . Mr. Panda starts at and can only defeat trainers of level at most , where . Find the number of different Pokémon types he can obtain.
Output
For each query of type 2, output one line containing the number of Pokémon types Mr. Panda can obtain.