#P15495. [ICPC 2025 APC] Tower of Hanoi

[ICPC 2025 APC] Tower of Hanoi

Description

While visiting Hanoi to compete in The ICPC Asia Pacific Championship last year, you learned about the famous Tower of Hanoi problem. In the problem, there are three rods and several disks of distinct radii, which can slide onto any rod. The rods are numbered from 11 to 33. At any point in time, each disk must be stacked on one of the rods, and the disks stacked on each rod must be arranged in increasing order of radius from top to bottom. In one step, you can move the disk on top of one rod to the top of another rod, provided this move does not violate the restriction above. The goal is to move all the disks to rod 11 in the minimum number of steps.

You are solving an extension of this famous problem. You have a sequence of nn integers p1,p2,,pnp_1, p_2, \ldots, p_n, the initial values of which are given to you.

You are also given qq operations. Each operation is either of the following:

  • Change operation: Two integers xx and yy are given. This operation requires you to change the value of pxp_x to yy.
  • Solve operation: Two integers ll and rr are given. This operation requires you to solve the Tower of Hanoi problem with rl+1r - l + 1 disks of radii l,l+1,,rl, l + 1, \ldots, r, where the disk of radius ii is initially stacked on rod pip_i, for each lirl \leq i \leq r. The order of the disks initially stacked on each rod satisfies the restriction explained earlier. You need to find the minimum number of steps to move all disks to rod 11 modulo 998244353998\,244\,353.

Your task is to perform all the given operations sequentially.

Input Format

The first line of input contains two integers nn and qq (1n1000001 \leq n \leq 100\,000; 1q1000001 \leq q \leq 100\,000). The second line contains nn integers representing the initial values of p1,p2,,pnp_1, p_2, \ldots, p_n (1pi31 \leq p_i \leq 3). The next qq lines represent the operations in the order they are to be performed. Each line is in one of the following formats:

  1. "c xx yy" (1xn1 \leq x \leq n; 1y31 \leq y \leq 3) to apply a Change operation for the specified integers xx and yy.
  2. "s ll rr" (1lrn1 \leq l \leq r \leq n) to apply a Solve operation for the specified integers ll and rr.

The input contains at least one Solve operation.

Output Format

For each Solve operation, in order, output the minimum number of steps to solve the Tower of Hanoi problem with disks of radii l,l+1,,rl, l+1, \ldots, r modulo 998244353998\,244\,353.

4 4
2 3 1 3
s 2 4
s 1 3
c 3 3
s 2 4
6
2
7

Hint

Explanation for the sample input/output #1

The first operation requires you to solve the Tower of Hanoi problem with disks of radii 22, 33, and 44 initially stacked on rods 33, 11, and 33 respectively. All disks can be moved to rod 11 in 66 steps as illustrated by Figure D.1.

:::align{center}

Figure D.1: 6 steps to move all disks to rod 1. The shaded rod represents the rod moved on the last step. :::

The second operation requires you to solve the Tower of Hanoi problem with disks of radii 11, 22, and 33 initially stacked on rods 22, 33, and 11 respectively.

The fourth operation requires you to solve the Tower of Hanoi problem with disks of radii 22, 33, and 44 initially stacked on rods 33, 33, and 33 respectively.