Fast Debugger

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

요약
중첩된 repeat 블록으로 이루어진 8비트 비트 연산 프로그램이 주어질 때, 실행한 명령 수가 k개일 때의 레지스터 값을 여러 질의에 대해 구한다.
난이도

어려움10점 중 8점

유형
비트 연산, 시뮬레이션, 세그먼트 트리, 이분 탐색
정답자
아직 제출이 없습니다

문제

Recently you received an old computer. This old computer has an 8-bit CPU with four registers: ax, bx, cx, dx, and only supports some simple instructions. For those who are not familiar with assembly language, here is a programming guide.

This CPU has four 8-bit registers: ax, bx, cx, dx. You can treat them as variables storing integers within \[0,255]\[0,255].

Only three kinds of bitwise operation are supported by this CPU, bitwise-or/and/xor. Each bitwise operation has two kinds of instructions.

Type 1: Both operands are registers, written as "or r1 r2", where both r1 and r2 are one of the register names ax, bx, cx, dx (r1 and r2 may refer to the same register). This instruction will set the value of r1 to the result of r1 bitwise-or r2. (Similarly, bitwise-and and bitwise-xor instructions are written as "and r1 r2" and "xor r1 r2").

Type 2: One of the operands is immediate, written as "ori r imm", where r is one of the register names ax, bx, cx, dx, and imm is a constant in \[0,255]\[0,255] given in the instruction. This instruction will set the value of r to the result of r bitwise-or imm. (Similarly, bitwise-and and bitwise-xor instructions are written as "andi r imm" and "xori r imm").

Loops are not supported by the CPU, but the assembler implemented an easy loop for programmers. If the assembler sees a "repeat $m$" statement, it will automatically repeat the contents of the repeat block, a total of mm times. The format is shown below.

repeat m
<repeat block>
end

Here, mm is a constant in \[2,255]\[2,255] given in the statement, and <repeat block> consists of one or more statements that can be either bitwise instructions or repeat--end statements.

Now you want to write a simulator on your new laptop which is much faster than the old computer.

Your simulator will be given a valid program and qq queries. Each query consists of five integers kk, a_0a\_0, b_0b\_0, c_0c\_0, d_0d\_0. Initially, the registers are set to the given values: ax=a_0 = a\_0, bx=b_0 = b\_0, cx=c_0 = c\_0, dx=d_0 = d\_0. You should output the values of registers ax, bx, cx, dx after the program executes kk bitwise instructions.

입력

The first line of input contains two integers nn and qq (1≤n≤12,0001 \leq n \leq 12\\,000; 1≤q≤10,0001 \leq q \leq 10\\,000), denoting the number of instructions and the number of queries.

Then follow nn lines. Each line is an instruction. The format is described above.

Each of the following qq lines contains five integers kk, a_0a\_0, b_0b\_0, c_0c\_0, d_0d\_0 (1≤k≤1091 \leq k \leq 10^9; 0≤a_0,b_0,c_0,d_0≤2550 \leq a\_0, b\_0, c\_0, d\_0 \leq 255), denoting a query for the value of registers after evaluating kk bitwise operations. It is guaranteed that the program does not terminate before executing kk bitwise instructions.

출력

Output qq lines. The ii-th line must contain four integers a_ka\_k, b_kb\_k, c_kc\_k, d_kd\_k, denoting the values of registers ax, bx, cx, dx after the program executes kk bitwise instructions.

예제1

  1. 예제 1

    입력
    6 2
    repeat 5
    xor ax bx
    xori ax 3
    and cx ax
    xor cx dx
    end
    10 1 2 4 3
    8 4 1 2 3
    
    예상 출력
    0 2 2 3
    4 1 3 3