#P15456. [JOI 2026 SemiFinal] 奇妙な機械 / Strange Machine

[JOI 2026 SemiFinal] 奇妙な機械 / Strange Machine

Problem Description

You have NN tiles, numbered from 11 to NN. Each tile has a front side and a back side, and each side is colored either black or white. Here, black is represented by the character 'B', and white is represented by the character 'W'. For tile ii (1≤i≤N1 \le i \le N), its front-side color is given by the ii-th character of the string SS, and its back-side color is given by the ii-th character of the string TT.

Uzbekistan is famous for its historical buildings decorated with tiles. After visiting mosques and madrasas in Uzbekistan, you were fascinated by these beautiful buildings and bought a strange machine. This machine has a platform on the left and a platform on the right. By placing one tile on each platform, you can exchange these two tiles for one new tile. Let the tile placed on the left platform be aa, and the tile placed on the right platform be bb. The new tile cc obtained from aa and bb satisfies the following:

  • The front-side color of cc: it is black if the back-side color of aa is the same as the front-side color of bb; otherwise, it is white.
  • The back-side color of cc: it is black if the front-side color of aa is the same as the back-side color of bb; otherwise, it is white.

You plan to use these NN tiles and the strange machine to do the following activities over QQ days. On day jj (1≤j≤Q1 \le j \le Q), the activity is either type 1 or type 2:

  • Type 1: Change the front-side color of tile XjX_j to the color represented by the character YjY_j, and change the back-side color of tile XjX_j to the color represented by the character ZjZ_j. Here Yj,ZjY_j, Z_j are 'B' or 'W'.
  • Type 2: Arrange tiles Lj,Lj+1,…,RjL_j, L_j+1, \dots, R_j in this order into a row from left to right. For this row, you may perform the following operation any number of times (from 00 up to Rj−LjR_j - L_j times), and then determine whether it is possible to make the number of tiles whose front side is white in the row equal to exactly MjM_j:
    • Choose two adjacent tiles in the row and remove them from the row. Put the tile that was originally on the left onto the left platform of the machine, and the one originally on the right onto the right platform. Exchange them for one new tile, and then insert the new tile back into the position where the two tiles were.

Given the initial tile information and the activities for each day, write a program to output the answer for all type 2 activities.

Input Format

Input is given from standard input in the following format:

NN
SS
TT
QQ
(Query 1)
(Query 2)
⋮\vdots
(Query QQ)

Each (Query jj) (1≤j≤Q1 \le j \le Q) contains several space-separated integers or characters. The first value is an integer 11 or 22, denoted by PjP_j, and the rest of the line is one of the following:

  • If Pj=1P_j = 1, then it is followed by an integer XjX_j and two characters Yj,ZjY_j, Z_j in this order. This means the activity on day jj is type 1: change the front-side color of tile XjX_j to the color represented by YjY_j, and change the back-side color to the color represented by ZjZ_j. Here Yj,ZjY_j, Z_j are B or W.
  • If Pj=2P_j = 2, then it is followed by three integers Lj,Rj,MjL_j, R_j, M_j in this order. This means the activity on day jj is type 2: arrange tiles Lj,Lj+1,…,RjL_j, L_j+1, \dots, R_j in order into a row from left to right, and determine whether it is possible (using the operations) to make the number of tiles whose front side is white in the row exactly MjM_j.

Output Format

For all jj such that Pj=2P_j = 2 (1≤j≤Q1 \le j \le Q), output one line per query in increasing order of jj: output Yes if it is possible to make the number of tiles whose front side is white in the row exactly MjM_j, and output No otherwise.

4
WBWB
BWBB
5
2 3 4 1
2 1 2 0
1 3 B B
2 3 4 2
2 2 4 1
Yes
Yes
No
Yes
6
BWBWWB
WBWBBB
8
2 1 3 2
2 2 6 0
2 1 5 3
2 3 3 0
2 3 4 1
2 5 6 2
2 2 6 4
2 1 4 2
No
Yes
Yes
Yes
Yes
No
No
Yes

Hint

Sample Explanation 1

  • Day 1: Arrange tiles 3,43,4 in this order into a row from left to right. If you do not perform any operation, only tile 33 has a white front side, so it is possible to make the number of tiles with a white front side exactly 11. Therefore output Yes.
  • Day 2: Arrange tiles 1,21,2 in this order into a row. If you choose tiles 11 and 22 and perform the operation once, the resulting row contains one tile whose front and back sides are both black, so it is possible to make the number of tiles with a white front side exactly 00. Therefore output Yes.
  • Day 3: Change both the front and back sides of tile 33 to black.
  • Day 4: Arrange tiles 3,43,4 into a row. It can be proven that it is impossible to make the number of tiles with a white front side exactly 22 by using the operations. Therefore output No.
  • Day 5: Arrange tiles 2,3,42,3,4 into a row. If you first choose tiles 33 and 44 and perform the operation once, the row will contain two tiles: tile 22 on the left and a new tile (whose front and back sides are both black) on the right. Then choose these two tiles and perform the operation once more; the row will contain one tile whose front side is white and back side is black, so it is possible to make the number of tiles with a white front side exactly 11. Therefore output Yes.

This sample input satisfies the constraints of subtasks 1,5,6,71,5,6,7.

Constraints

  • 1≤N≤300 0001 \le N \le 300\,000
  • SS is a string of length NN consisting of B and W
  • TT is a string of length NN consisting of B and W
  • 1≤Q≤300 0001 \le Q \le 300\,000
  • PjP_j is 11 or 22 (1≤j≤Q1 \le j \le Q)
  • If Pj=1P_j = 1, then 1≤Xj≤N1 \le X_j \le N (1≤j≤Q1 \le j \le Q)
  • If Pj=1P_j = 1, then YjY_j is 'B' or 'W' (1≤j≤Q1 \le j \le Q)
  • If Pj=1P_j = 1, then ZjZ_j is 'B' or 'W' (1≤j≤Q1 \le j \le Q)
  • If Pj=2P_j = 2, then 1≤Lj≤Rj≤N1 \le L_j \le R_j \le N (1≤j≤Q1 \le j \le Q)
  • If Pj=2P_j = 2, then 0≤Mj≤Rj−Lj+10 \le M_j \le R_j - L_j + 1 (1≤j≤Q1 \le j \le Q)
  • N,Q,Pj,Xj,Lj,Rj,MjN, Q, P_j, X_j, L_j, R_j, M_j are all integers.

Subtasks

  1. (6 points) N≤6N \le 6.
  2. (10 points) N≤100N \le 100, and Pj=2P_j = 2 (1≤j≤Q1 \le j \le Q).
  3. (9 points) N≤500N \le 500, and Pj=2P_j = 2 (1≤j≤Q1 \le j \le Q).
  4. (8 points) N≤1700N \le 1700, and Pj=2P_j = 2 (1≤j≤Q1 \le j \le Q).
  5. (23 points) N≤10 000N \le 10\,000, Q≤10 000Q \le 10\,000.
  6. (14 points) N≤100 000N \le 100\,000, Q≤100 000Q \le 100\,000.
  7. (30 points) No additional constraints.

Translated by DeepSeek.

Translated by ChatGPT 5