Minimum Xor Pair Query

题目大意

维护一个序列,支持动态插入,删除,查询最小异或对。

思路分析

看到查询最小异或对首先想到 01Trie,但 01Trie 不支持删除,考虑暴力套一个线段树分治。

需要预处理出每个元素的存活区间,这里使用了 map<int,vector<int>>。注意,有的元素会存活到最后,需要特判。

时间复杂度为 \(O(n\log n\log V)\),其中 \(V\) 是值域。

这个做法有较强的可扩展性,非常无脑。

代码

代码非常好写,只有 2k。

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map> using namespace std;
const int N=300300,M=10001000;
#define inf 2147483647 int n,op,in1;
int ans[N],type[N]; map<int,vector<int>> mp;//用于预处理元素存活区间 struct Trie{//递归版 Trie
#define c (x>>bit&1)
int a[M][2],num[M];
int tot;
void insert(int p,int bit,int x){
if(!~bit) return ;
if(!a[p][c]) a[p][c]=++tot;
insert(p=a[p][c],bit-1,x);
num[p]++;
}
void del(int p,int bit,int x){
if(!~bit) return ;
del(p=a[p][c],bit-1,x);
num[p]--;
}
int query(int p,int bit,int x){
if(!~bit) return 0;
bool f=!num[a[p][c]];
return query(a[p][c^f],bit-1,x)+f*(1<<bit);
}
}trie; struct ST{//线段树
#define mid ((l+r)>>1)
vector<int> a[N<<2];
void add(int p,int l,int r,int x,int y,int k){//增加元素
if(x<=l&&r<=y){a[p].push_back(k);return ;}
if(x<=mid) add(p<<1,l,mid,x,y,k);
if(y>mid) add(p<<1|1,mid+1,r,x,y,k);
}
void dfs(int p,int l,int r,int res){//遍历全树
for(auto it:a[p]){
res=min(res,trie.query(0,30,it));
trie.insert(0,30,it);
}
if(l==r) ans[l]=res;
else{
dfs(p<<1,l,mid,res);//答案下传
dfs(p<<1|1,mid+1,r,res);
}
for(auto it:a[p]) trie.del(0,30,it);
}
}tree; int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&op);
if(op==1){
scanf("%d",&in1);
mp[in1].push_back(i);
}
if(op==2){
scanf("%d",&in1);
tree.add(1,1,n,mp[in1].back(),i-1,in1);
mp[in1].pop_back();
}
if(op==3) type[i]=1;
}
for(auto it:mp)
for(auto it2:it.second)
tree.add(1,1,n,it2,n,it.first);//特判最后的元素
tree.dfs(1,1,n,inf);
for(int i=1;i<=n;i++)
if(type[i]) cout<<ans[i]<<'\n';
return 0;
}

[ABC308G] Minimum Xor Pair Query 题解的更多相关文章

  1. BZOJ2337:[HNOI2011]XOR和路径——题解

    +++++++++++++++++++++++++++++++++++++++++++ +本文作者:luyouqi233. + +欢迎访问我的博客:http://www.cnblogs.com/luy ...

  2. 【HDU5869】 Different GCD Subarray Query 题解 (线段树维护区间GCD)

    题目大意:求区间$[L,R]$中所有子区间产生的最大公因数的个数. ------------------------- 对于$gcd$,我们知道$gcd(a,b,c)=gcd(gcd(a,b),c)$ ...

  3. CodeForces 279D The Minimum Number of Variables 题解

    题目大意: 有一组n个不相同的数字组成数串:a1,a2,a3-an. 1.一个数组b. 2.第一个操作我们将b0的值赋为a1.之后我们有n-1个操作,第k次操作我们将by=bi+bj(y,i,j可能相 ...

  4. HDU 1385 Minimum Transport Cost 最短路径题解

    本题就是使用Floyd算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...

  5. [HNOI2011]XOR和路径 题解

    设 \(f(x)\) 表示从 \(x\) 节点走到 \(n\) 的期望.有 $$f(x)=\sum_{{x,y}}\frac{f(y)\oplus w(x,y)}{{\rm deg}(x)}$$ 由于 ...

  6. USACO 6.1 Cow XOR

    Cow XORAdrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All of ...

  7. GCD XOR(UVa 12716)

    题意:输入整数n(1<=n<=30000000),有多少对整数(a,b)满足1<=b<=a<=n,且gcd(a,b)=a xor b. 题解:设c=gcd(a,b),因为 ...

  8. BZOJ 2337 XOR和路径 | 高斯消元 期望 位运算

    BZOJ 2337 XOR和路径 题解 这道题和游走那道题很像,但又不是完全相同. 因为异或,所以我们考虑拆位,分别考虑每一位: 设x[u]是从点u出发.到达点n时这一位异或和是1的概率. 对于所有这 ...

  9. Minimum Window Substring leetcode java

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  10. 【leetcode刷题笔记】Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

随机推荐

  1. 【Python】Locust持续优化:InfluxDB与Grafana实现数据持久化与可视化分析

    前言 在进行性能测试时,我们需要对测试结果进行监控和分析,以便于及时发现问题并进行优化. Locust在内存中维护了一个时间序列数据结构,用于存储每个事件的统计信息. 这个数据结构允许我们在Chart ...

  2. Java扩展Nginx之五:五大handler(系列最核心)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<Java扩展Nginx> ...

  3. 【技术积累】JavaScript中的基础语法【一】

    Math对象 JavaScript中的Math对象是一个内置的数学对象,表示对数字进行数学运算的方法和属性的集合. Math对象不是一个构造函数,所以不能使用new关键字来创建一个Math对象的实例. ...

  4. PerfView专题 (第十四篇): 洞察那些 C# 代码中的短命线程

    一:背景 1. 讲故事 这篇文章源自于分析一些疑难dump的思考而产生的灵感,在dump分析中经常要寻找的一个答案就是如何找到死亡线程的生前都做了一些什么?参考如下输出: 0:001> !t T ...

  5. linux-服务操作和运行级别和关机重启

    服务操作: service  network   [] systemctl     [ disable(禁用)  enable(启用)]     network [] 中为操作命令 : 1.statu ...

  6. windows传输文件到linux

    PFSTP 打开该软件,在安装putty自带的 连接服务器 open 192.168.142.131 按提示输入账户密码 传送文件 put C:\Users\13662\nifi-1.13.2-bin ...

  7. Cesium SuperMap问题调研汇总

    https://segmentfault.com/a/1190000040577369?sort=newest

  8. P1941 [NOIP2014 提高组] 飞扬的小鸟 题解

    我们先不管障碍物. 设 \(f[i][j]\) 表示来到点 \((i,j)\) 的最少点击屏幕数. 因为每秒要不上升 \(k\times x[i]\),要么下降 \(y[i]\). 所以有: \[f[ ...

  9. 论文解读(LightGCL)《LightGCL: Simple Yet Effective Graph Contrastive Learning for Recommendation》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:LightGCL: Simple Yet Effective Graph Contrastive Lear ...

  10. win10右键添加打开cmd窗口的命令

    创建文本文档,复制如下内容: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\b ...