Choosing The Commander
Contest: Educational Codeforces Round 23
Difficulty rating: 2000
Tags: bitmasks, data structures, trees

As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires.

Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors.

Each warrior is represented by his personality — an integer number pi. Each commander has two characteristics — his personality pj and leadership lj (both are integer numbers). Warrior i respects commander j only if $$p_i \oplus p_j < l_j$$ ($$x \oplus y$$ is the bitwise excluding OR of x and y).

Initially Vova's army is empty. There are three different types of events that can happen with the army:

- 1 pi — one warrior with personality pi joins Vova's army;
- 2 pi — one warrior with personality pi leaves Vova's army;
- 3 pi li — Vova tries to hire a commander with personality pi and leadership li.

For each event of the third type Vova wants to know how many warriors (counting only those who joined the army and haven't left yet) respect the commander he tries to hire.

Input format:
The first line contains one integer q (1 ≤ q ≤ 100000) — the number of events.

Then q lines follow. Each line describes the event:

- 1 pi (1 ≤ pi ≤ 108) — one warrior with personality pi joins Vova's army;
- 2 pi (1 ≤ pi ≤ 108) — one warrior with personality pi leaves Vova's army (it is guaranteed that there is at least one such warrior in Vova's army by this moment);
- 3 pi li (1 ≤ pi, li ≤ 108) — Vova tries to hire a commander with personality pi and leadership li. There is at least one event of this type.

Output format:
For each event of the third type print one integer — the number of warriors who respect the commander Vova tries to hire in the event.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the example the army consists of two warriors with personalities 3 and 4 after first two events. Then Vova tries to hire a commander with personality 6 and leadership 3, and only one warrior respects him ($$4 \oplus 6 = 2$$, and 2 < 3, but $$3 \oplus 6 = 5$$, and 5 ≥ 3). Then warrior with personality 4 leaves, and when Vova tries to hire that commander again, there are no warriors who respect him.

Example input:
5
1 3
1 4
3 6 3
2 4
3 6 3

Example output:
1
0

Editorial:
Let's use binary trie to store all personalities of warriors (that is, just use the trie data structure on binary representations of all pi). For each subtree of this trie you have to maintain the number of pi's currently present in this subtree — when inserting a value of pi, we increase the sizes of subtrees on the path from the node with pi to the root by 1, and when removing pi, we decrease the sizes of subtrees on this path by 1.
How can it help us with answering the events of third type? We will descend the trie. When descending, we will try to find the number $$p_i \oplus l_i$$ in the structure. When we go to some subtree, we determine whether we add the quantity of numbers in the subtree we are not going into by checking if the current bit in li is equal to 1 (if so, then for all numbers from this subtree their bitwise xor with the current commander's personality is less than li). The answer to the event is the sum of sizes of all subtrees we "added" while descending into the trie.