Tom and Jerry
Practice
4.2 (5 votes)
Advanced data structures
Data structures
Lazy propagation in interval/segment trees
Number theory
Problem
59% Success 1292 Attempts 50 Points 2s Time Limit 512MB Memory 1024 KB Max Code

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 \(1\)\(2\)\(3\), 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 \(i^{th}\) 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\) (\(1 \le Q \le 2e5\)), \(K\) (\(1\le K \le10\))
  • Next lines: \(Q\) operations (\(1\le Total\ number\ of\ addition\ of\ blocks\ (AL,AR\ operations) \le1e5\))
    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

\(1 \le Q \le 2e5\\ 1\le K \le10\\ 1\le x \le1000000000\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
3 votes
Tags:
Advanced Data StructuresData StructuresLazy Propagation in Interval/Segment Trees
Points:50
2 votes
Tags:
Lazy Propagation in Interval/Segment TreesData StructuresBit manipulationAdvanced Data Structures
Points:50
Tags:
Hard
Editorial

Login to unlock the editorial