[Codeforces 817E] Choosing The Commander Trie树

E. Choosing The Commander
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires.

Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors.

Each warrior is represented by his personality — an integer number pi. Each commander has two characteristics — his personality pjand leadership lj (both are integer numbers). Warrior i respects commander j only if  ( is the bitwise excluding OR ofx and y).

Initially Vova's army is empty. There are three different types of events that can happen with the army:

  • pi — one warrior with personality pi joins Vova's army;
  • pi — one warrior with personality pi leaves Vova's army;
  • pi li — Vova tries to hire a commander with personality pi and leadership li.

For each event of the third type Vova wants to know how many warriors (counting only those who joined the army and haven't left yet)respect the commander he tries to hire.

Input

The first line contains one integer q (1 ≤ q ≤ 100000) — the number of events.

Then q lines follow. Each line describes the event:

  • pi (1 ≤ pi ≤ 108) — one warrior with personality pi joins Vova's army;
  • pi (1 ≤ pi ≤ 108) — one warrior with personality pi leaves Vova's army (it is guaranteed that there is at least one such warrior in Vova's army by this moment);
  • pi li (1 ≤ pi, li ≤ 108) — Vova tries to hire a commander with personality pi and leadership li. There is at least one event of this type.
Output

For each event of the third type print one integer — the number of warriors who respect the commander Vova tries to hire in the event.

Example
input
5
1 3
1 4
3 6 3
2 4
3 6 3
output
1
0
Note

In the example the army consists of two warriors with personalities 3 and 4 after first two events. Then Vova tries to hire a commander with personality 6 and leadership 3, and only one warrior respects him (, and 2 < 3, but , and 5 ≥ 3). Then warrior with personality 4 leaves, and when Vova tries to hire that commander again, there are no warriors who respect him.

题解:

博主很菜啊,并没有什么码力,只能写写小水题辣!

xor相关,没有什么别的好的性质,于是考虑xor的原始定义-------->按位异或----------->于是可以想到01Trie树.

建立出01Trie树后,可以对于每个节点记录这个以这个节点为根的子树上的士兵总数,

从而可以O(logp)进行添加,删除和询问操作    (话说trie树把递归变成循环能快不少呢)

时间复杂度:O(qlogp).

Code:

#include <bits/stdc++.h>
using namespace std;
inline int read()
{int x=0;
char c=getchar();
while (c<'0'||c>'9') {c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
return x;
}
struct trie
{int s,ch[2];
}t[3000005];
int cnt=1,rt=1;
inline void add(int x)
{int pos=1,i,tg;
t[pos].s++;
for (i=26;i>=0;i--)
{if (x&(1<<i)) {tg=1;}
else {tg=0;}
if (!t[pos].ch[tg])
{t[pos].ch[tg]=++cnt;}
pos=t[pos].ch[tg];
t[pos].s++;
}
return;
}
inline void del(int x)
{int pos=1,i,tg;
t[pos].s--;
for (i=26;i>=0;i--)
{if (x&(1<<i)) {tg=1;}
else {tg=0;}
pos=t[pos].ch[tg];
t[pos].s--;
}
return;
}
inline void ask(int x,int tar)
{int ans=0,pos=1,i,nt=0,ok;
for (i=26;i>=0;i--)
{if (nt+(1<<i)-1<tar) 
{ok=1;nt=(nt+(1<<i));}
else 
{ok=0;}
int p=x&(1<<i),tag;
if (p>0) {tag=0;}
else {tag=1;}
if (!ok) {tag^=1;}
else {ans+=t[t[pos].ch[tag^1]].s;}
pos=t[pos].ch[tag];
}
printf ("%d\n",ans);
return;
}
int main (){
	int p,q,l,opt;
	q=read();
	while (q--)
	{opt=read();
	if (opt==1) {p=read();add(p);}
	if (opt==2) {p=read();del(p);}
	if (opt==3) {p=read();l=read();ask(p,l);}
	}
	return 0;
}
	




          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值