题目大意:


解题思路:

首先我们知道区间查询异或最小值肯定是在trie是匹配
而且如果没有(aj+xi)(aj+xi)(aj+xi)就是一个可持久化Trie树上面的裸题了

但是很不幸有
怎么办呢?

  1. 首先我们把(aj+xi)(a_j+x_i)(aj​+xi​)看成一个整体,要想求异或最小值那么我们从高位看bib_ibi​

  2. 首先我们模仿在字典树上条就是看看tr[bit][0or1]tr[bit][0\;or\;1]tr[bit][0or1]是否存在

  3. 那么我们知道对于同样,01 字典树上的每一个结点都可以看作一个值域。假设一个结点 u 在 01 字典树上从根到它的前缀所对应的二进制数值为 sum,它的对应位数为 iii,那么它的值域为 [sum,sum+2i−1][sum,sum+2^i-1][sum,sum+2i−1],左儿子值域为 [sum,sum+2i−1−1][sum,sum+2^{i-1}-1][sum,sum+2i−1−1],右儿子值域为 [sum+2i−1,sum+2i−1][sum+2^{i-1},sum+2^i-1][sum+2i−1,sum+2i−1]
    关于查询一个结点是否存在,只需要查询在它的值域内是否有被插入过的数即可。就可以用主席树了!!

  4. 每次都要减掉xix_ixi​回到原来区间去插才对


AC code

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 200010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {read(first);read(args...);
}
int n, m;
int len = 2e5;
struct node {int  lson, rson;int val;
};
struct Segtree {node tr[maxn * 40];int root[maxn];int cnt;Segtree(){cnt=0;}void build(int &rt, int l, int r) {rt = ++ cnt;if(l == r) return;build(tr[rt].lson,l,mid);build(tr[rt].rson,mid+1,r);}void insert(int &rt, int l, int r, int pre, int pos) {rt = ++ cnt;tr[rt] = tr[pre];tr[rt].val ++;if(l == r) return;if(pos <= mid) insert(tr[rt].lson,l,mid,tr[pre].lson,pos);else insert(tr[rt].rson,mid+1,r,tr[pre].rson,pos);}bool query(int rt, int l, int r, int rtree, int posl, int posr) {if(posr < 0) return 0;if(posl <= l && posr >= r) return ((tr[rtree].val - tr[rt].val) > 0);bool res = 0;if(posl <= mid) res |= query(tr[rt].lson,l,mid,tr[rtree].lson,posl,posr);if(posr > mid) res |= query(tr[rt].rson,mid+1,r,tr[rtree].rson,posl,posr);return res;}
}sgt;int main() {IOS;cin >> n >> m;// sgt.build(sgt.root[0],0,len);for(int i = 1; i <= n; ++ i) {int x;cin >> x;sgt.insert(sgt.root[i],0,len,sgt.root[i-1],x);}while(m --) {int b, x, l, r;cin >> b >> x >> l >> r;int sum = 0, ans = 0;for(int i = 17; i >= 0; -- i) {int bit = (b >> i) & 1;int lson = sgt.query(sgt.root[l-1],0,len,sgt.root[r],max(sum-x,0),sum+(1<<i)-1-x);int rson = sgt.query(sgt.root[l-1],0,len,sgt.root[r],max(sum+(1<<i)-x,0),sum+(1<<(i+1))-1-x);if(bit) {if(lson) ans |= (1 << i);else sum |= (1 << i);} else {if(rson) sum |= (1 << i), ans |= (1 << i);}}cout << ans << "\n";}return 0;
}
/*
4 4
1 2 3 4
1 4 1 4
2 3 2 3
3 2 3 3
4 1 2 4
*/

trie树上值域化建主席树 查询异或平移最小值 ---- P3293 [SCOI2016]美味相关推荐

  1. hdu 2665(主席树查询区间k大值)

    先贴我自己写的代码做模板虽然跟原博主没什么两样.(一开始空间开的4*maxn,交到hdu上一直TLE很奇怪) #include<bits/stdc++.h> using namespace ...

  2. 主席树 + 树上倍增 ---- codeforces 587C[树上倍增或者主席树]

    题目链接 给定一棵n个点的树,给定m个人(m≤n)在哪个点上的信息,每个点可以有任意个人:然后给q个询问,每次问u到v上的路径有的点上编号最小的k(k≤10)个人(没有那么多人就该有多少人输出多少人) ...

  3. 中石油训练赛 - Russian Dolls on the Christmas Tree(树上启发式合并/主席树)

    题目链接:点击查看 题目大意:给出一棵 n 个节点的树,以点 1 为根,现在对于每个节点作为根的子树求解:子树中有多少个编号不相交的连续子段,如:1 2 4 5 7,共有三个连续的段,分别为 [ 1 ...

  4. P3293 [SCOI2016]美味 主席树 + 伪01trie

    传送门 文章目录 题意: 思路: 题意: 思路: 看到异或的话,很容易想到用01trie来贪心的搞,但是这个题涉及区间问题,直接搞的话需要将[l,r][l,r][l,r]的数都插入trie里面,这样的 ...

  5. array(2019CCPC网络预选赛 hdu6703主席树+set)主席树求大于等于k的最小值

    Problem Description You are given an array a1,a2,-,an(∀i∈[1,n],1≤ai≤n). Initially, each element of t ...

  6. 计蒜客 - Distance on the tree(LCA+主席树)

    题目链接:点击查看 题目大意:给出一颗含有n个节点的树,每条边都有权值,现在给出m个询问,每次询问的格式为u,v,w,我们需要求出在路径u-v上,边权小于等于w的边的个数 题目分析:多的不说了,上一个 ...

  7. 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树

    题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见-- 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...

  8. 洛谷P3567 KUR-Couriers 主席树

    给出 n ≤ 5 e 5 n\leq5e5 n≤5e5的序列,每次询问一个区间是否有超过一半的数,这里的一半是下取整.直接离散化建主席树,查询到区间的时候判断左子树个数是否大于一半的个数,如果是递归左 ...

  9. BZOJ3473:字符串(后缀数组,主席树,二分,ST表)

    Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串. Output 一 ...

最新文章

  1. Squid代理服务器基本配置(三)
  2. mongodb 主从配置,带auth验证模式
  3. 详解python的super()的作用和原理
  4. 瑞士电信vCPE商用落地 华三通信NFV方案成最大功臣
  5. 读《代码不朽:编写可维护软件的10大要则》C# 版
  6. droidbox官网
  7. yum install nload失败,提示No package nload available.Error: Nothing to do
  8. 《精通ArcGIS Server 应用与开发》——2.4 ArcGIS Server的安装与配置
  9. Flutter获取packageName和versionCode
  10. js 表格动态增加行通用函数
  11. python开发酷q插件gui_酷Q的SDK模块机器人个人开发插件
  12. Flutter diff: /../Podfile.lock: No such file or directory AndroidStudio上的解决
  13. JVM内存模型及CMS、G1和ZGC垃圾回收器详解
  14. 微信小程序的事件大全
  15. JS-获取视频总时长
  16. unity调用安卓手机物理返回键和home键
  17. [Excel]如何使Vlookup由右往左找? 或使Hlookup由下往上找?
  18. 经典再现交互设计那些事儿(二):交互常用的工具和方法
  19. matlab信号仿真模型,对Matlab单边带信号处理得到生动的仿真模型
  20. 大泥王怎么调时区_卡西欧大泥王手表性能怎么样 卡西欧手表为什么叫泥王

热门文章

  1. anaconda创建环境
  2. scrapy爬虫框架初相识
  3. Python 200个标准库汇总
  4. 深度学习七个实用技巧
  5. 【OpenCV 4开发详解】图像噪声的种类与生成
  6. 30天提升技术人的写作力-第二天
  7. Pod在多可用区worker节点上的高可用部署
  8. Js 向json对象中添加新元素
  9. JavaEE——JavaScript
  10. 【BZOJ】1711: [Usaco2007 Open]Dining吃饭