## DMOPC '17 Contest 3 P4 - Solitaire Logic

View as PDF

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

You have a deck containing cards. Each card contains a unique number from to on one side and cannot be identified from its other side. A friend of yours has placed your cards face down on a table such that the cards are split into two rows of cards each and each of the rows is sorted by its number from least to greatest. You do not know the number on any of the cards.

You are bored, so you decide to play a game with these cards. Every now and then, you perform one of two operations. You either flip and reveal one of the cards or you wonder to yourself, "How many of these cards could possibly be the card with value ?"

The first operation is in the form which indicates that the card in the row was flipped and the number on this card was . It is guaranteed that there will always be some configuration of cards such that it agrees with the cards shown as well as the rules in the problem statement. Also, a card will never be flipped twice.

The second operation is in the form which asks for the number of positions which could have on the card at that positon. For example, if you know that either the second card in the first row, the third card in the first row, or the third card in the second row could have that number, then the answer to the operation would be .

#### Input Specification

The first line contains two integers representing and .

The following lines contain one of the two operations in the format specified above.

#### Output Specification

For every one of the second operations, output the answer on a new line.

#### Sample Input 1

4 7
1 1 4 6
2 8
2 7
1 2 2 4
2 1
1 2 1 1
2 1

#### Sample Output 1

1
1
2
1

#### Explanation for Sample 1

After the first card is flipped, card can only be in the last spot of the second row. This is because, as the largest value, card must be in one of the two last spots and the last spot of the first row is already occupied by . A configuration which agrees with the information so far and also has card in that spot is

1 2 3 6
4 5 7 8

For the next query, note that card cannot be in the first row. Then card can only be the second last card of the second row since card is in the last spot and is larger than all the other values apart from .

For the third query, card must be in the first spot of one of the rows since it has the smallest value. It's simple to check that there do exist valid configurations where card is in each of these spots:

1 2 5 6
3 4 7 8

and

2 3 5 6
1 4 7 8

For the final query, since card 's position has been revealed, that position is the only one which can have card .

#### Sample Input 2

4 13
1 1 1 1
1 1 3 7
1 1 2 4
2 3
2 1
1 2 1 2
2 3
2 7
1 2 4 6
1 2 2 3
1 2 3 5
1 1 4 8
2 3

#### Sample Output 2

1
1
1
1
1