zoukankan      html  css  js  c++  java
  • Vasiliy's Multiset

    Vasiliy's Multiset
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Author has gone out of the stories about Vasiliy, so here is just a formal task description.

    You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries:

    1. "+ x" — add integer x to multiset A.
    2. "- x" — erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query.
    3. "? x" — you are given integer x and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.

    Multiset is a set, where equal elements are allowed.

    Input

    The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform.

    Each of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer xi (1 ≤ xi ≤ 109). It's guaranteed that there is at least one query of the third type.

    Note, that the integer 0 will always be present in the set A.

    Output

    For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.

    Example
    input
    10
    + 8
    + 9
    + 11
    + 6
    + 1
    ? 3
    - 8
    ? 3
    ? 8
    ? 11
    output
    11
    10
    14
    13
    Note

    After first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.

    The answer for the sixth query is integer  — maximum among integers  and .

    分析:01字典树;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=1e7+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,q,id;
    char a[10];
    struct node
    {
        int next[2],num,cnt;
    }p[maxn];
    void insert(int n)
    {
        int now=0;
        for(int i=31;i>=0;i--)
        {
            if(!p[now].next[n>>i&1])p[now].next[n>>i&1]=++id;
            now=p[now].next[n>>i&1],p[now].cnt++;
        }
        p[now].num=n;
    }
    void del(int n)
    {
        int now=0;
        for(int i=31;i>=0;i--)
        {
            now=p[now].next[n>>i&1],p[now].cnt--;
        }
    }
    int query(int n)
    {
        int now=0;
        for(int i=31;i>=0;i--)
        {
            if(p[p[now].next[n>>i&1^1]].cnt)now=p[now].next[n>>i&1^1];
            else now=p[now].next[n>>i&1];
        }
        return n^p[now].num;
    }
    int main()
    {
        int i,j;
        insert(0);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%s %d",a,&m);
            if(a[0]=='+')
            {
                insert(m);
            }
            else if(a[0]=='-')
            {
                del(m);
            }
            else
            {
                printf("%d
    ",query(m));
            }
        }
        //system("pause");
        return 0;
    }
  • 相关阅读:
    SIFT,SURF,ORB,FAST,BRISK 特征提取算法比较
    OpenCV 4.2.0 编译成功日志(Mac)
    Ceres Solver Bibliography
    Linux下重启就需要重新激活eth0的解决办法(ifup eth0)
    PS(光影魔术手)
    软件项目开发各阶段文档模板(参考)
    敏捷、瀑布开发模式
    QA
    QC
    会计人必知的实务基础知识
  • 原文地址:https://www.cnblogs.com/dyzll/p/5764269.html
Copyright © 2011-2022 走看看