| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 2048 MB | 13 | 12 | 11 | 91.667% |
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 1ドル$ to 3ドル$. 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 1ドル$ in the minimum number of steps.
You are solving an extension of this famous problem. You have a sequence of $n$ integers $p_1, p_2, \dots , p_n,ドル the initial values of which are given to you.
You are also given $q$ operations. Each operation is either of the following:
Change operation: Two integers $x$ and $y$ are given. This operation requires you to change the value of $p_x$ to $y$.
Solve operation: Two integers $l$ and $r$ are given. This operation requires you to solve the Tower of Hanoi problem with $r − l + 1$ disks of radii $l, l + 1, \dots , r,ドル where the disk of radius $i$ is initially stacked on rod $p_i,ドル for each $l ≤ i ≤ 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 1ドル$ modulo 998ドル,円 244,円 353$.
Your task is to perform all the given operations sequentially.
The first line of input contains two integers $n$ and $q$ (1ドル ≤ n ≤ 100,円 000$; 1ドル ≤ q ≤ 100,円 000$). The second line contains $n$ integers representing the initial values of $p_1, p_2, \dots , p_n$ (1ドル ≤ p_i ≤ 3$). The next $q$ lines represent the operations in the order they are to be performed. Each line is in one of the following formats:
The input contains at least one Solve operation.
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, \dots , r$ modulo 998ドル,円 244,円 353$.
4 4 2 3 1 3 s 2 4 s 1 3 c 3 3 s 2 4
6 2 7
The first operation requires you to solve the Tower of Hanoi problem with disks of radii 2ドル,ドル 3ドル,ドル and 4ドル$ initially stacked on rods 3ドル,ドル 1ドル,ドル and 3ドル$ respectively. All disks can be moved to rod 1ドル$ in 6ドル$ steps as illustrated by Figure D.1.
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 1ドル,ドル 2ドル,ドル and 3ドル$ initially stacked on rods 2ドル,ドル 3ドル,ドル and 1ドル$ respectively.
The fourth operation requires you to solve the Tower of Hanoi problem with disks of radii 2ドル,ドル 3ドル,ドル and 4ドル$ initially stacked on rods 3ドル,ドル 3ドル,ドル and 3ドル$ respectively.