Choosing The Commander CodeForces - 817E (01字典树+思维)
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 pj and leadership lj (both are integer numbers). Warrior i respects commander j only if (
is the bitwise excluding OR of x and y).
Initially Vova's army is empty. There are three different types of events that can happen with the army:
- 1 pi — one warrior with personality pi joins Vova's army;
- 2 pi — one warrior with personality pi leaves Vova's army;
- 3 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:
- 1 pi (1 ≤ pi ≤ 108) — one warrior with personality pi joins Vova's army;
- 2 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);
- 3 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
5
1 3
1 4
3 6 3
2 4
3 6 3
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.
题意:
给定Q个操作,操作有以下三种:
1. 加入一个数值为pi的士兵
2.删除一个数值为pi的士兵
3. 如果让一个数值为L和P的将军,询问有多少个士兵尊敬他。
一个士兵尊重这个将军,当且仅当pi^P<L , (^为异或运算)
思路:
建立01字典树,将加入每一个士兵从高位到低位的二进制插入到字典树中,沿途的节点加1,如果删除就是沿途的节点减一。
当询问有多少个士兵尊敬将军得时候,
把将军的P值也从二进制的高位到低位遍历,
如果P在这一位为0,
当L也在这一位为0,那么如果有这一位为1的士兵,那么这些士兵一定不尊重他,下一步进入这一位为0的找。
如果P这一位为1
当L也在这一位为1,那么如果这一位为1的士兵一定会尊敬他,直接加上数量,然后进入这一位为0的找,
其他情况也这样推就好了。
细节见我的代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ struct node
{
int num;
node *Next[];
node()
{
num=;
Next[]=Next[]=nullptr;
}
}; void Add(node *head , int num)
{
node *p=head;
for(int i=;i>=;i--)
{
int k=((num>>i)&);
if(p->Next[k]== nullptr)
{
node *q =new node();
p->Next[k]= q;
}
p = p->Next[k];
p->num++;
}
// p->num=num;
}
void Del(node *head ,int num)
{
node *p=head;
for(int i=;i>=;i--)
{
int k=((num>>i)&);
if(p->Next[k]==NULL)
{
node * q=new node();
p->Next[k] = q;
}
p = p->Next[k];
p->num--;
} }
int Find(node *head, int num)
{
node *p = head;
for(int i=;i>=;i--)
{
int k=((num>>i)&);
if(p->Next[k^]!=NULL)
{
p = p->Next[k^]; }else
{
p = p->Next[k];
}
}
return p->num;
}
void query(node * root,int x,int y)
{
node *p = root;
int res=;
for(int i=;i>=;--i)
{
int tx=(x&(<<i));
int tl=(y&(<<i));
if(tx)
{
if(tl)
{
if(p->Next[])
res+=p->Next[]->num;
p=p->Next[];
}else
{
p=p->Next[];
}
}else
{
if(tl)
{
if(p->Next[])
res+=p->Next[]->num;
p=p->Next[];
}else
{ p=p->Next[];
}
}
if(p==nullptr)
break;//
}
printf("%d\n",res);
}
int n;
int main()
{
gg(n);
node *head = new node();
repd(i,,n)
{
int op,x,y;
gg(op);
if(op==)
{
gg(x);
Add(head,x);
}else if(op==)
{
gg(x);
Del(head,x);
}else
{
gg(x),gg(y);
query(head,x,y);
}
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Choosing The Commander CodeForces - 817E (01字典树+思维)的更多相关文章
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- Vasiliy's Multiset CodeForces -706D || 01字典树模板
就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 #include<cstdio> #include<algorithm> using namespace st ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- Codeforces 948D Perfect Security 【01字典树】
<题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...
- Codeforces 979 字符串强制N变换最多出现字母 DFS子树 暴力01字典树
A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ...
- Codeforces 948 数论推导 融雪前缀和二分check 01字典树带删除
A. 全部空的放狗 B. 先O(NLOGNLOGN)处理出一个合数质因数中最大的质数是多少 因为p1 x1 x2的关系是 x2是p在x1之上的最小倍数 所以x1的范围是[x2-p+1,x2-1]要使最 ...
- Chip Factory---hdu5536(异或值最大,01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
随机推荐
- 以太坊之——golang以太坊接口调用
Go语言具有简单易学.功能强大,可跨平台编译等众多优势,所以这里选择以Go语言切入以太坊. 开始之前需要以下环境: Ubuntu(这里以ubuntu16.04为例) geth Ubuntu16.04安 ...
- Android Studio连接天天模拟器
方法:安装两者后,打开天天模拟器的adb.exe所在目录,我的是C:\Users\ Android\sdk\platform-tools,在打开的文件夹下使用“shift+鼠标右键”打开cmd终端. ...
- HttpHandler实现网页图片防盗链
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...
- burp抓取手机模拟器流量
这里安装的是夜神模拟器,网络模式设置为桥接: 然后从burp suite 导出证书,存为.cer格式.将其导入手机模拟器(我用百度网盘导入的,自带的文件导入,不太会用,找不到放哪了) 然后给手机模拟器 ...
- Ubuntu 12.04上安装HBase并运行
Ubuntu 12.04上安装HBase并运行 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 一.HBase的安装 在官网上下载HBase-1.1.2 ...
- redis学习笔记(二)-五种数据类型
string hash hget ks k hset ks k v hgetall ks hdel ks k del ks hmset ks k v k v list set zset 通用命令 快 ...
- 提高git下载速度(非代理或修改HOST)
1. 利用开源中国提供的代码仓库 标题已经说的很清楚了,我想对于经常使用git的人来讲,很可能已经知道了.对于新手刚接触git的人来讲,可能你只知道github. 实际上,国内也有很多代码仓库提供方, ...
- 解决Nginx+Tomcat下客户端https请求跳转成http的问题
Nginx上开启https, 后端使用Tomcat, 两者间走http协议, 但发现如果tomcat应用存在跳转时, 则客户端浏览器会出现400 Bad Request的错误, 通过抓包发现原因是 ...
- GT常见问题
1.需要root? 大部分核心功能都需要root权限.需要root的功能有:FPS.Mem Assistant.Logcat日志.抓包.流畅度获取(未开放). 2.root工具总弹出提示框要确认roo ...
- package-info.java的使用
一.引入 上文中,提到了注解类JyzTargetPackage可以定义为@Target(ElementType.PACKAGE),可是在被注解类里我无论怎么加,编译器都报错,于是引入了package- ...