Tower of Hanoi

시간 제한2초메모리 제한2048 MB

요약
각 원판의 시작 막대가 점마다 갱신될 때, 주어진 구간의 원판을 1번 막대로 모두 옮기는 최소 이동 횟수를 998244353으로 나눈 나머지를 구한다.
난이도

어려움10점 중 9점

유형
세그먼트 트리, 동적 계획법, 재귀, 분할 정복
정답자
아직 제출이 없습니다

문제

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 p_1,p_2,…,p_np\_1, p\_2, \dots , 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 p_xp\_x to yy.

Solve operation: Two integers ll and rr are given. This operation requires you to solve the Tower of Hanoi problem with r−l+1r − l + 1 disks of radii l,l+1,…,rl, l + 1, \dots , r, where the disk of radius ii is initially stacked on rod p_ip\_i, for each l≤i≤rl ≤ 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 11 modulo 998,244,353998\\, 244\\, 353.

Your task is to perform all the given operations sequentially.

입력

The first line of input contains two integers nn and qq (1≤n≤100,0001 ≤ n ≤ 100\\, 000; 1≤q≤100,0001 ≤ q ≤ 100\\, 000). The second line contains nn integers representing the initial values of p_1,p_2,…,p_np\_1, p\_2, \dots , p\_n (1≤p_i≤31 ≤ p\_i ≤ 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. “cc xx yy” (1≤x≤n1 ≤ x ≤ n; 1≤y≤31 ≤ y ≤ 3) to apply a Change operation for the specified integers xx and yy.
  2. “ss ll rr” (1≤l≤r≤n1 ≤ l ≤ r ≤ n) to apply a Solve operation for the specified integers ll and rr.

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,…,rl, l + 1, \dots , r modulo 998,244,353998\\, 244\\, 353.

예제1

  1. 예제 1

    입력
    4 4
    2 3 1 3
    s 2 4
    s 1 3
    c 3 3
    s 2 4
    
    예상 출력
    6
    2
    7