This page is still under construction.

Parts of this page are still being built. What you see may change.

Questions

Time limit1sMemory limit128 MB

Summary
Simulate a logic puzzle where princes and a sorcerer reason about a system of variable constraints over time; answer what each prince or the sorcerer knows.
Level

Hard9 of 10

Topics
Brute force, Simulation, Combinatorics, Implementation
Solved
No attempts yet

Problem

Long ago, King Bytour invented a game for his three sons and described it to his counsellor, the sorcerer Bytelean:

"I told my three sons (numbered 1, 2, 3) to stand in a row, and I placed a golden or a silver crown on each of their heads. Son 1 could see the crowns of sons 2 and 3, and son 2 could see the crown of son 3. Every son knew that there were at most two golden crowns in total. I then asked son 1 whether he knew the colour of his own crown; he answered that he did not. I asked son 2 the same question, and he also answered that he did not."

At this moment Bytelean interrupted the king and said that he already knew which crown prince 3 had received. Asked how, he explained:

"If son 1 had seen two golden crowns, he would have known that his own was silver (there are at most two golden crowns). He answered NO, so that situation is impossible. Now, if son 2 had seen a golden crown on son 3's head, he would have known that his own was silver, because otherwise son 1 would have answered YES. Son 2 did not know either, so prince 3 must have a silver crown."

Your task is to implement a general simulator of such situations. The facts the king may ask the princes and the sorcerer about (the crown colour, in the story) are encoded as a sequence of variables. Some variables are derived from earlier ones; for the rest, only a range of possible values is known. The exact format is described in the Input section.

Write a program that reads a description of a situation from standard input, computes the answers the sorcerer should give, and writes them to standard output.

Input

The first line contains three integers PP, VV, and AA, separated by single spaces. PP is the number of princes (numbered 11 to PP), VV is the number of variables (numbered 11 to VV), and AA is the number of actions. The constraints 1≤P≤101 \le P \le 10, 1≤V≤6001 \le V \le 600, and 1≤A≤6001 \le A \le 600 hold.

The next VV lines describe the variables v1,v2,…,vVv_1, v_2, \ldots, v_V. Each line has the form Z A B (single spaces), where ZZ is one of the characters =, +, -, *, /, %, >, and AA, BB are integers. The meaning depends on ZZ:

LineMeaning
= A Bviv_i is an integer with A≤vi≤BA \le v_i \le B (here −106≤A≤B≤106-10^6 \le A \le B \le 10^6).
+ A Bvi=vA+vBv_i = v_A + v_B (for this row and every row below, 1≤A,B<i1 \le A, B < i).
- A Bvi=vA−vBv_i = v_A - v_B.
* A Bvi=vA⋅vBv_i = v_A \cdot v_B.
/ A Bvi=vA/vBv_i = v_A / v_B (the integer part of the division).
% A Bvi=vA mod vBv_i = v_A \bmod v_B (the remainder).
> A Bvi=1v_i = 1 if vA>vBv_A > v_B, and vi=0v_i = 0 otherwise.

This information is given at the start of the game to all princes and to the sorcerer.

The next AA lines describe the actions (single spaces):

  • S g n: The value of vnv_n is revealed to prince gg. The fact that this value was revealed to prince gg becomes known to all princes and to the sorcerer, but the value itself does not.
  • T g n: The king asks prince gg whether he knows the value of vnv_n. The answer is YES, and the king passes it to the sorcerer. The other princes learn this answer only when an A action is performed, so several princes can answer "at the same time" without their answers influencing one another.
  • N g n: The same as above, but the answer is NO.
  • X g n: The same question is asked, but the king does not pass the prince's answer to the sorcerer. Instead he asks the sorcerer to guess it. The sorcerer answers YES, NO, or I DON'T KNOW (I DON'T KNOW meaning the sorcerer cannot tell whether prince gg knew the answer). During this action the king does not reveal prince gg's real answer to the sorcerer, and does not pass the sorcerer's guess to the princes. (The sorcerer's answers are written in Polish, see Output.)
  • A 0 0: Every prince is told the answers that all other princes gave to every king's question asked since the previous A action (the T, N, and X questions). During this action the king does not tell the sorcerer the answers he received in X actions.
  • M w n: The king tells the sorcerer that variable vnv_n has value ww.
  • Q 0 n: The king asks the sorcerer which values vnv_n may take, according to his current knowledge. The sorcerer's answer is not passed to the princes.

Both the princes and the sorcerer reason perfectly: at every moment each of them can deduce every fact implied by the initial ranges and by everything that has happened so far. Moreover, each of them knows that all of them reason perfectly.

Additional guarantees: the number of possible valuations (the product of Bi−Ai+1B_i - A_i + 1 over all variables of type =) does not exceed 600600. In every valuation that is theoretically possible, the absolute value of each variable is at most 10610^6; for the operations / and %, vAv_A is non-negative and vBv_B is positive. A variable vXv_X may appear in the definition of vYv_Y only if X<YX < Y.

Output

For every X action, print one line containing TAK (YES), NIE (NO), or NIE WIEM (I DON'T KNOW).

For every Q action, print one line containing all values that vnv_n may take, from the smallest to the largest, separated by single spaces.

The lines must appear in the same order as the actions they correspond to, independently of the action types.

Examples3

  1. Example 1

    Input
    3 7 16
    = 0 1
    = 0 1
    = 0 1
    + 1 2
    + 3 4
    = 3 3
    > 6 5
    S 1 7
    S 2 7
    S 3 7
    M 1 7
    S 1 3
    S 2 3
    S 1 2
    X 3 3
    X 1 1
    N 1 1
    A 0 0
    N 2 2
    X 3 3
    A 0 0
    X 3 3
    Q 0 3
    
    Expected output
    NIE
    NIE WIEM
    NIE
    TAK
    0
    
  2. Example 2

    Input
    1 1 4
    = 5 8
    X 1 1
    S 1 1
    X 1 1
    Q 0 1
    
    Expected output
    NIE
    TAK
    5 6 7 8
    
  3. Example 3

    Input
    1 1 3
    = 1 10
    Q 0 1
    M 7 1
    Q 0 1
    
    Expected output
    1 2 3 4 5 6 7 8 9 10
    7