Sonya and Queries CodeForces - 714C 字典树

Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her tqueries, each of one of the following type:

  1.  +  ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.
  2.  -  ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset.
  3. ? s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left.

For example, if the pattern is s = 010, than integers 92221250 and 414 match the pattern, while integers 311025 and 1030 do not.

Input

The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform.

Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ciequals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18.

It's guaranteed that there will be at least one query of type '?'.

It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.

Output

For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.

Example
Input
12
+ 1
+ 241
? 1
+ 361
- 241
? 0101
+ 101
? 101
- 101
? 101
+ 4000
? 0
Output
2
1
2
1
1
Input
4
+ 200
+ 200
- 200
? 0
Output
1
Note

Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input.

  1. 1 and 241.
  2. 361.
  3. 101 and 361.
  4. 361.
  5. 4000.

用字典树判断01串是否匹配的问题。建树的时候我是从低位到高位建的。18位就够了。比如1在树中就是1后面17个0

删除和插入见代码

查询的时候想了一会。如果匹配的话肯定是18位都匹配上了,所以匹配上的数量就是匹配到最后一个节点那上面的num

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200005;

int tot;
int root;
struct node
{
    int son[2];
    int num;
}tree[40*maxn];
int newnode()
{
    tot++;
    tree[tot].son[0]=tree[tot].son[1]=0;
    return tot;
}
void inser(int x)
{
    int now=root;
    for(int i=31;i>=0;i--)
    {
        int tmp=!!(x&(1<<i));
        if(tree[now].son[tmp]==0)tree[now].son[tmp]=newnode();
        tree[tree[now].son[tmp]].num++;
        now=tree[now].son[tmp];
    }
}
void delet(int x)
{
    int now=root;
    int tmp;
    for(int i=31;i>=0;i--)
    {
         tmp=!!(x&(1<<i));
        tree[tree[now].son[tmp]].num--;
        now=tree[now].son[tmp];
    }

}
long long  query(int x)
{
    long long  ans=0;
    int now=root;
    for(int i=31;i>=0;i--)
    {
        int tmp=!!!(x&(1<<i));
        if(tree[now].son[tmp]!=0&&tree[tree[now].son[tmp]].num>0)
        {
            ans+=tmp*(1<<i);
            now=tree[now].son[tmp];
        }
        else
        {
            ans+=(!tmp)*(1<<i);
            now=tree[now].son[!tmp];
        }
    }
        return ans;

}
int main()
{
    int q;
    while(cin>>q)
    {

        tot=-1;
        root=newnode();
        memset(tree,0,sizeof(tree));
        inser(0);
        for(int i=0;i<q;i++)
        {
            char x;
            long long  y;
            cin>>x>>y;
           // cout<<x<<y<<endl;
            if(x=='+')inser(y);
            else if(x=='-')delet(y);
            else
            {
                long long z=y^query(y);
                cout<<z<<endl;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mayuqing98/article/details/79594477