51nod 1295 XOR key
来源:互联网 发布:电大和网络教育含金量 编辑:程序博客网 时间:2024/05/16 13:58
#include"iostream"#include"string.h"#include"vector"using namespace std;const int maxn=5e4+5;int W;struct Tree{ int l,r;};Tree tree[maxn*32];int a[maxn];int N,M;int tot;vector<int> root;void BuildTree(){ root.push_back(tot); int now=tot; for(int i=W;i>=0;i--) { int tp=(0>>i)&1; tree[now++].l=++tot; }}void Insert(int id1,int x){ tot++; root.push_back(tot); int now=tot; for(int i=W;i>=0;i--) { int tp=(x>>i)&1; if(tp==0) { tree[now].l=++tot; if(tree[id1].r!=-1) { tree[now].r=tree[id1].r; } id1=tree[id1].l; now=tree[now].l; } else { tree[now].r=++tot; if(tree[id1].l!=-1) { tree[now].l=tree[id1].l; } id1=tree[id1].r; now=tree[now].r; } }}int Query(int L,int R,int x){ int ans=0; L--; int id1=root[L]; int id2=root[R]; for(int i=W;i>=0;i--) { int tp=(x>>i)&1; int t=tp^1; if(t==1) { if(tree[id2].r!=-1&&(tree[id2].r!=tree[id1].r)) { id2=tree[id2].r; id1=tree[id1].r; ans|=(1<<i); } else { id2=tree[id2].l; id1=tree[id1].l; } } else { if(tree[id2].l!=-1&&(tree[id2].l!=tree[id1].l)) { id1=tree[id1].l; id2=tree[id2].l; } else { id1=tree[id1].r; id2=tree[id2].r; ans|=(1<<i); } } } ans^=x; return ans;}int main(){ W=30; while(cin>>N>>M) { memset(tree,-1,sizeof(tree)); tree[-1].l=tree[-1].r=-1; tot=0; BuildTree(); for(int i=1;i<=N;i++) { cin>>a[i]; Insert(root[i-1],a[i]); } for(int i=1;i<=M;i++) { int L,R,X; cin>>X>>L>>R; L++; R++; cout<<Query(L,R,X)<<"\n"; } }}
阅读全文
0 0
- 51Nod 1295 (XOR key)
- 51Nod-1295-XOR key
- 51nod 1295 XOR key
- 51nod 1295 XOR key
- 51nod 1295 XOR key(字典树)
- 51Nod——1295 XOR key(贪心+可持久化字典树)
- 51nod 1295 XOR key 01trie(可持久化字典树坑已填)
- 【51nod 6级题目】XOR key 问题
- 51nod1295 XOR key
- 51nod1295 XOR KEY
- 【51Nod1295】XOR key
- XOR key
- 51Nod-1310-Chandrima and XOR
- 51 nod 1310 Chandrima and XOR(规律)
- 可持久化Trie——51nod1295 XOR key
- xor
- XOR ^
- xor
- Java多线程之迭代器问题(四)
- 关于httppost状态码分析
- mysql 多字段关键词模糊查询
- javaweb基础
- LeetCode--Reverse String 反转字符串(Python)
- 51nod 1295 XOR key
- 吴恩达深度学习课程deeplearning.ai课程作业:Class 2 Week 1 2.Regularization
- Oracle12c连接问题ORA-28040:没有匹配的验证协议
- 【Linux】正则表达式
- 破解比特币secp256k1第一步,y^2=x^3+7 (mod p),已知x求y。用python语言编写。
- CSS——字体(font-family,font-style,font-size注意要点)
- docker常用命令
- Creating a video with OpenCV
- 深度学习和普通机器学习之间有何区别?