Пример #1
0
    public void insert(int s)
    {
        ++cnt;
        if (dep < 0)
        {
            return;
        }
        int c = (s >> dep) & 1;

        if (c == 0)
        {
            if (child0 == null)
            {
                child0 = new BinaryTrie(dep - 1);
            }
            child0.insert(s);
        }
        else
        {
            if (child1 == null)
            {
                child1 = new BinaryTrie(dep - 1);
            }
            child1.insert(s);
        }
    }
Пример #2
0
        public override void Solve(ConsoleReader cr, ConsoleWriter cw)
        {
            int N  = cr;
            var bt = new BinaryTrie(30);

            for (int q = 0; q < N; q++)
            {
                int  t = cr;
                uint x = cr;
                if (t == 0)
                {
                    if (bt.Count(x) == 0)
                    {
                        bt.Increment(x);
                    }
                }
                else if (t == 1)
                {
                    if (bt.Count(x) != 0)
                    {
                        bt.Decrement(x);
                    }
                }
                else
                {
                    cw.WriteLine(bt.MinElement(x).Num);
                }
            }
        }
Пример #3
0
        public void Simple()
        {
            var bt = new BinaryTrie();

            bt.Add(1, 2);
            bt.Increment(2);
            bt.Increment(5);

            bt.KthElement(0).Num.Should().Be(1);
            bt.KthElement(1).Num.Should().Be(1);
            bt.KthElement(2).Num.Should().Be(2);
            bt.KthElement(3).Num.Should().Be(5);

            bt.MinElement().Num.Should().Be(1);
            bt.MaxElement().Num.Should().Be(5);

            bt.Count(1).Should().Be(2);
            bt.Count(2).Should().Be(1);
            bt.Count(5).Should().Be(1);

            bt.CountLess(1).Should().Be(0);
            bt.CountLess(2).Should().Be(2);
            bt.CountLess(3).Should().Be(3);
            bt.CountLess(5).Should().Be(3);
            bt.CountLess(6).Should().Be(4);



            bt.KthElement(0, 4).Num.Should().Be(1);
            bt.KthElement(1, 4).Num.Should().Be(5);
            bt.KthElement(2, 4).Num.Should().Be(5);
            bt.KthElement(3, 4).Num.Should().Be(6);

            bt.MinElement(4).Num.Should().Be(1);
            bt.MaxElement(4).Num.Should().Be(6);

            bt.Count(1, 4).Should().Be(1);
            bt.Count(2, 4).Should().Be(0);
            bt.Count(5, 4).Should().Be(2);
            bt.Count(6, 4).Should().Be(1);

            bt.CountLess(1, 4).Should().Be(0);
            bt.CountLess(2, 4).Should().Be(1);
            bt.CountLess(3, 4).Should().Be(1);
            bt.CountLess(5, 4).Should().Be(1);
            bt.CountLess(6, 4).Should().Be(3);
            bt.CountLess(7, 4).Should().Be(4);

            bt.Decrement(2);
            bt.KthElement(0).Num.Should().Be(1);
            bt.KthElement(1).Num.Should().Be(1);
            bt.KthElement(2).Num.Should().Be(5);

            bt.MinElement().Num.Should().Be(1);
            bt.MaxElement().Num.Should().Be(5);

            bt.Count(1).Should().Be(2);
            bt.Count(5).Should().Be(1);

            bt.CountLess(1).Should().Be(0);
            bt.CountLess(2).Should().Be(2);
            bt.CountLess(5).Should().Be(2);
            bt.CountLess(6).Should().Be(3);
        }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int q = fs.NextInt();
             Dictionary <int, int> count = new Dictionary <int, int>();
             count.Add(0, int.MaxValue);
             BinaryTrie trie = new BinaryTrie('0', '1');
             trie.Add(DecToPaddedBin(0));
             while (q-- > 0)
             {
                 string[] toks = fs.ReadLine().Split();
                 int      x    = Convert.ToInt32(toks[1]);
                 if (toks[0] == "?")
                 {
                     string        path    = DecToPaddedBin(x ^ int.MaxValue);
                     var           current = trie.GetRoot();
                     StringBuilder sb      = new StringBuilder();
                     for (int i = 0; i < path.Length; i++)
                     {
                         if (path[i] == '0' && current.Left != null || path[i] == '1' && current.Right == null)
                         {
                             current = current.Left;
                             sb.Append('0');
                         }
                         else if (path[i] == '1' && current.Right != null || path[i] == '0' && current.Left == null)
                         {
                             current = current.Right;
                             sb.Append('1');
                         }
                     }
                     int ans = PaddedBinToDec(sb.ToString());
                     writer.WriteLine(ans ^ x);
                 }
                 else
                 {
                     if (toks[0] == "+")
                     {
                         if (!count.ContainsKey(x))
                         {
                             count.Add(x, 1);
                         }
                         else
                         {
                             count[x]++;
                         }
                         if (count[x] == 1)
                         {
                             trie.Add(DecToPaddedBin(x));
                         }
                     }
                     else if (toks[0] == "-")
                     {
                         count[x]--;
                         if (count[x] == 0)
                         {
                             trie.Remove(DecToPaddedBin(x));
                         }
                     }
                 }
             }
         }
 }