Educational Codeforces Round 23 E. Choosing The Commander (trie)

题目链接: Educational Codeforces Round 23 E. Choosing The Commander

题意:

一共有n个操作。

1.  插入一个数p

2.  删除一个数p

3.  询问有多少个数 使得 x^p<l

题解:

对于前两种操作用01trie就能解决。

对于对三个操作,我们考虑在trie上搜索。

1.  当l的bit位是1时,那边bit位是p的字数全部的数都会小于l,(因为p^p=0)

2.  当l的bit为是0时,那边只能向bit位是p的子树中搜。

这样算下来 三种操作的时间复杂度都为log(1e8)。总复杂度为nlog(1e8)

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=a;i<=b;++i)
 3 using namespace std;
 4 
 5 const int N=2e6+7;
 6 int n,tr[N][2],cnt[N],ed=1,type,p,l;
 7 
 8 void add(int x,int v)
 9 {
10     int now=1;
11     for(int i=30;i>=0;i--)
12     {
13         int tp=bool(x&(1<<i));
14         if(!tr[now][tp])tr[now][tp]=++ed;
15         cnt[now]+=v,now=tr[now][tp];
16     }
17     cnt[now]+=v;
18 }
19 
20 int ask(int p,int l,int now=1,int bit=30)
21 {
22     if(!now||bit<0)return 0;
23     int pp=bool(p&(1<<bit));
24     if(l&(1<<bit))return cnt[tr[now][pp]]+ask(p,l,tr[now][pp^1],bit-1);
25     else return ask(p,l,tr[now][pp],bit-1);
26 }
27 
28 int main(){
29     scanf("%d",&n);
30     while(n--)
31     {
32         scanf("%d%d",&type,&p);
33         if(type==1)add(p,1);
34         else if(type==2)add(p,-1);
35         else scanf("%d",&l),printf("%d\n",ask(p,l));
36     }
37     return 0;
38 }
View Code

 

posted @ 2017-06-21 19:48  bin_gege  阅读(146)  评论(0编辑  收藏  举报