ACdream1063-平衡树

来源:互联网 发布:打开xlsx的软件 编辑:程序博客网 时间:2024/05/01 03:14

平衡树

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Statistic Next Problem

Problem Description

神奇的cxlove有一颗平衡树,其树之神奇无法用语言来描述 OrzOrz。 这棵树支持3种操作: 1、加入一个数到树中,维护平衡树的合法性; 2、给一个数X,用O(1)的时间求出来树中的数Y使得 Y ^ X 最大(异或操作, Pascal 写作 xor , 0 ^ 0 = 0 , 1 ^ 1 = 0 , 0 ^ 1 = 1 , 1 ^ 0 = 1 , 2 ^ 3 = 1) 3、给一个数X,用O(1)的时间求出来树中的数Y使得 Y ^ X 最小(异或操作, Pascal 写作 xor , 0 ^ 0 = 0 , 1 ^ 1 = 0 , 0 ^ 1 = 1 , 1 ^ 0 = 1 , 2 ^ 3 = 1) 请你帮忙实现这颗树。 cxlove由于过于牛,有些事情是无法做到的,你能在1s以内通过就好了。 最后补充一句,cxlove是世界冠军!

Input

第一行是数据组数T (T ≤ 100)

对于每组数据,第一行是操作数N (N ≤ 10000)

以下 行,每行一个字符串一个数字,分别代表:

  1. insert X ,加入数 X
  2. qmax X ,求第2种操作的答案
  3. qmin X ,求第3种操作的答案

输入保证不存在空树Query的情况 (1 ≤ X ≤ 1e9)

Output

对于操作 2 , 3 输出相应的答案。

Sample Input

14insert 1insert 2qmin 1qmax 1

Sample Output

03

Hint

Huge input and output. Please do not use: 1. cin ans cout of C++ 2. scanner of Java(Maybe BufferedReader is quicker)

Source

buaads

Manager

nanae

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <iostream>using namespace std;struct node{    node* next[2];    node()    {        memset(next,0,sizeof(next));    }}*root;void add(int x){    node *q=root;    for (int i=30; i>=0; i--)    {        int k=(x&(1<<i))?1:0;        if (!q->next[k])        {            q->next[k]=new node;        }        q=q->next[k];    }}int get(int flag,int x){    int ans=x;    node *q=root;    for (int i=30; i>=0; i--)    {        int k=(x&(1<<i))?1:0;        if (q->next[flag^k])        {            q=q->next[flag^k];            ans=ans^((flag^k)*(1<<i));        }        else        {            q=q->next[1^flag^k];            ans=ans^((1^flag^k)*(1<<i));        }    }    return ans;}void dfs(node *x){    for (int i=0; i<2; i++)        if (x->next[i]) dfs(x->next[i]);    delete x;}int main(){    int n,t,x;    char s[10];    scanf("%d",&t);    while (t--)    {        if (root) dfs(root);        root=new node;        scanf("%d",&n);        while (n--)        {            scanf("%s %d",s,&x);            if (s[0]=='i') add(x);            else printf("%d\n",get(s[2]=='i'?0:1,x));        }    }    return 0;}

0 0
原创粉丝点击