题目链接




解题思路:

  1. 首先要异或第kkk大那么肯定是和Trie树有关!!
  2. 而且每个区间的询问,那么肯定是可持久化的Trie树啦
  3. 这道题有个小TipsTipsTips就是那个n∈[1,1000]和q∈[1,500]n\in[1,1000]和q\in[1,500]n∈[1,1000]和q∈[1,500]
  4. 那么我们就可以去枚举nnn对于每次询问
  5. 首先我们从高位往低位看
  6. 就对于高位bbb看,我们枚举nnn,看每个n的第bbb位在Trie树上看与之相反的有多少相反的(就是这nnn个数异或有多少种高位为1,因为高位权值大),那么就有点像权值线段树一样跳了,假如当前位有cntkcnt_kcntk​个异或为1
  7. 看看cntkcnt_kcntk​和kkk的关系,如果比kkk大就往异或为111的跳
  8. 如果比kkk小就,往异或为0跳,并且k=k−cntkk=k-cnt_kk=k−cntk​
  9. 注意是nnn个点一起跳

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 = 500010;
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 root[maxn];
struct Trie {int tr[maxn * 40][2], sum[maxn * 40];int cnt;void insert(int &now, int pre, int val) {now = ++ cnt;int poi = now;for(int i = 31; i >= 0; -- i) {tr[poi][0] = tr[pre][0];tr[poi][1] = tr[pre][1];sum[poi] = sum[pre] + 1;int c = (val >> i) & 1;pre = tr[pre][c];tr[poi][c] = ++ cnt;poi = tr[poi][c];}sum[poi] = sum[pre] + 1;}
}trie;int X[maxn], Y[maxn];
int out[105][2];
inline int work(int u,int d,int l,int r, int k) {for(int i = u; i <= d; ++ i) {    out[i][0] = root[l-1];out[i][1] = root[r];}int res;int ans = 0;for(int i = 31; i >= 0; -- i) {int cnt = 0;for(int j = u; j <= d; ++ j) { int c = (X[j] >> i) & 1;int p1 = trie.sum[ trie.tr[out[j][0]][c^1] ];int p2 = trie.sum[ trie.tr[out[j][1]][c^1] ];cnt += p2 - p1;// 求有多少个数异或出来第i位为1}if(cnt >= k) res = 1;// 决定往异或为1跳还是为0跳else k -= cnt, res = 0;ans = (ans<<1)|res;for(int j = u; j <= d; ++ j) {int c = (((X[j] >> i) & 1) ^ res);int &now0 = out[j][0], &now1 = out[j][1];now0 = trie.tr[now0][c];now1 = trie.tr[now1][c];}}return ans;
}int main() {int n, m;read(n,m);for(int i = 1; i <= n; ++ i) read(X[i]);for(int j = 1; j <= m; ++ j) read(Y[j]), trie.insert(root[j],root[j-1],Y[j]);int q;read(q);while(q --) {int u, d, l, r, k;read(u,d,l,r,k);printf("%d\n",work(u,d,l,r,k));}return 0;
}/*
31 31
1 2 4
7 6 5
1
2 31 2 31 4
*/

可持久化Trie +枚举 ---- P5795 [THUSC2015]异或运算相关推荐

  1. Nikitosh 和异或 —— 一道 trie 树的题用可持久化 trie 水 然后翻车了...

    题意简介 题目就是叫你找两个不重合的非空区间,使得这两个区间里的数异或后相加的和最大 (看到异或,没错就决定是你了可持久化trie!) 思路 水一波字典树,莫名觉得这题可持久化能过,于是水了一发挂了, ...

  2. 【bzoj3261】最大异或和 可持久化Trie树

    题目描述 给定一个非负整数序列 {a},初始长度为 N.        有M个操作,有以下两种操作类型: 1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1. 2.Q l r x: ...

  3. 可持久化Trie+堆优化 OR Trie树上求XOR第K大 ---- P5283 [十二省联考2019]异或粽子

    题目大意 题目大意: 考虑先做个 prefix xor 前缀异或bi=⨁j=1iaj(1≤i≤n)b_i=\bigoplus_{j=1}^{i}a_j(1\leq i \leq n)bi​=j=1⨁i ...

  4. P4735 最大异或和(可持久化trie树、求最大区间异或和)

    P4735 最大异或和 我们维护一个前缀异或和:s[i]=a[1]xora[2]xor-a[i−1]xora[i]s[i] = a[1] \ xor\ a[2]\ xor\ - a[i-1] \ xo ...

  5. [BZOJ3261] 最大异或和 (异或前缀和,可持久化Trie)

    Description 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x:询问操作, ...

  6. 【BZOJ 4103】 4103: [Thu Summer Camp 2015]异或运算 (可持久化Trie)

    4103: [Thu Summer Camp 2015]异或运算 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 474  Solved: 258 D ...

  7. bzoj 3261 最大异或和【可持久化trie】

    因为在后面加数字又求后缀和太麻烦,所以xor[p...n]=xor[1...n]^xor[p-1...n]. 首先处理出来区间异或前缀和,对前缀和建trie树(在最前面放一棵0表示最开始的前缀和 然后 ...

  8. BZOJ3261: 最大异或和(可持久化trie树)

    题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...

  9. BZOJ 3261: 最大异或和 [可持久化Trie]

    3261: 最大异或和 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 1513  Solved: 657 [Submit][Status][Disc ...

最新文章

  1. javascript 权威指南第7版_免费领书 | 气相色谱与质谱实用指南(原著第2版)
  2. WebService怎样在IIS上部署
  3. 洛谷P3296 刺客信条
  4. 关于是否在C#中加入不可空引用类型的争论
  5. stability condition in queueing system
  6. Drawable Resources
  7. TortoiseSVN的安装和使用
  8. java-多线程安全问题
  9. xEasyApp之后端的介绍
  10. 算法编程-不定时更新
  11. 【数据结构 严蔚敏版】 顺序栈 基本操作
  12. webstorm js没有代码提示
  13. 计算机考试表格函数应用题,2017年职称计算机考试Excel练习题2
  14. RunningCheese Firefox 71.0 正式版 [1225]
  15. 调用支付宝第三方支付接口详解(沙箱环境)
  16. Win11将输入法的繁体改为简体
  17. CSDN 空间:“迷你博客”很迷人(1)
  18. 考研备考建议篇——论文怎么投稿
  19. 表达矩阵melt之后 画图 reshape gaochao ards 1: Unknown or uninitialised column: `p`. 2: Computation failed i
  20. 不是所有的战略规划都可以用BLM

热门文章

  1. 数据库中数据类型和约束(整数类型、字符串、时间类型)
  2. attr,abbr,addr三个常见的单词意思
  3. DEV为什么不能输出小数 浮点数?
  4. Mask-RCNN论文解读
  5. 十六、curator recipes之DistributedIdQueue
  6. Zookeeper工作原理
  7. 前端那些事之Bootstrap篇
  8. 给你的网站穿上外衣 - HTTPS 免费部署指南
  9. 开启Mac充电提示音
  10. GBK和UTF-8之间的战争,websphere6.1乱码解决方案