This page is still under construction.

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

Color Palette

Time limit3sMemory limit1024 MB

Summary
Maintain a set of K-bit colors under insertions and, for each query color, return the stored color with the maximum number of matching bit positions, breaking ties by smallest value.
Level

Medium7 of 10

Topics
Trie, Bit manipulation, Greedy, Brute force
Solved
No attempts yet

Problem

A company that makes graphics software is developing a new graphics program. One of its modules manages a color palette. When the program starts, the palette is empty. The user can add new colors to the palette, or ask which of the palette's colors is most similar to a given color.

Colors are represented as KK-bit integers (values from 00 to 2K−12^K-1), and the similarity of two colors is the number of bit positions where their binary representations agree. For example, when K=5K = 5, the similarity of colors 00110 and 10101 is 22, because only the second and third bits from the left have the same value.

The program behaves like a palette-management module built from the following operations, and it prints a log in the order the operations are called.

OperationDescription
init(int k, int n)Initialize the palette to use kk-bit colors. Called once at the start, followed by a total of nn add and find calls.
add(int c)Add color cc to the palette.
find(int c)Find the best match for color cc in the palette. Returns the palette color whose similarity to cc is largest. If several colors are equally most similar, return the smallest such color value. This operation is called only when the palette already holds at least one color.
done()End of work. Called once at the very end.

Input

The first line of the input contains the number of bits KK (1≤K≤201 \le K \le 20) used to represent colors and the number of operations NN (1≤N≤1061 \le N \le 10^6). Each of the following NN lines contains two integers TiT_i and CiC_i (0≤Ci<2K0 \le C_i < 2^K). Ti=1T_i = 1 means "add color CiC_i to the palette", and Ti=2T_i = 2 means "find the best match for color CiC_i in the palette".

Output

Print the log of the operations the module processed. First print init(K, N). Then process each operation in the given order and print the following.

  • For an operation that adds color CC, print add(C).
  • For an operation that looks up color CC, print find(C) = R, where RR is the palette color whose similarity to CC (the number of matching bits) is largest. If several colors are equally similar, RR is the smallest such color value.

Finally print done(). A find operation is given only when the palette already contains at least one color.

Examples2

  1. Example 1

    Input
    2 3
    1 1
    2 0
    2 1
    
    Expected output
    init(2, 3)
    add(1)
    find(0) = 1
    find(1) = 1
    done()
    
  2. Example 2

    Input
    3 5
    1 0
    1 7
    2 1
    1 1
    2 1
    
    Expected output
    init(3, 5)
    add(0)
    add(7)
    find(1) = 0
    add(1)
    find(1) = 1
    done()