Tom and Jerry

4.2

5 votes
Advanced Data Structures, Data Structures, Lazy Propagation in Interval/Segment Trees, Number theory
Problem

Tom and Jerry are playing a pile game and they are provided with a number K. The game is played on a pile of cheese blocks and each of them can eat 123, or ... up to K cheese blocks and who ever is not able to eat is declared as the looser of the game. Initially, there is no pile in front of them but as time progresses more piles of cheese blocks are added.
Tom is asked the total of Q operations to perform in the game. This includes additional operations and also some questions. The operations and questions are as follows:

  1. AL x: Add a pile containing x cheese blocks at the left of the current available total number of cheese piles.
  2. AR x: Add a pile containing x cheese blocks at the right of the current available total number of cheese piles.
  3. UR l r x: Add x cheese blocks to all the piles in range l to r.
  4. UP i x: Remove the ith pile and introduce a pile containing x cheese blocks at position i.

    The types of questions are as follows:
  5. Q1 l r: Tom must tell the total number of ways he can choose a single pile in range [l, r], such that after Tom and Jerry play the game optimally on that pile, Tom definitely wins (Tom always starts the game).
  6. Q2 l r: Tom must tell who will win he or Jerry if the game is played on all piles in range [l, r]. If Tom wins, then display Tom. Otherwise, display Jerry.
    Note: Both play the game optimally and Tom always starts the game.

Input format

  • First line: Two space-seperated integers Q (1Q2e5), K (1K10)
  • Next lines: Q operations (1Total number of addition of blocks (AL,AR operations)1e5)
    For each query of all the types and it is guaranteed that l, r, i are always valid indexes of the current available states of the piles array.

Output format

The answer for the following types of questions:

  1. Q1: Print a single integer that denotes the total number of ways of winning the game.
  2. Q2: Print either Tom or Jerry with respect to whoever wins the game.

Constraints

1Q2e51K101x1000000000

Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation

After 1st,2nd and 3rd operation the array is [10,9,14]

The answer to 4th query is 2

The answer to 5th query is Tom

After 6th operation the array is [18,17,14]

After 7thoperation the array is [12,18,17,14]

After 8th operation the array is [12,18,8,14]

The answer to 9th query is 0

The answer to 10th query is Jerry

Editor Image

?