前言

暴力跑的比正解快。

以下暴力(循环展开+fread读入输出优化)

#include<cstdio>
#pragma GCC optimize(3, "Ofast") int a[200010]; namespace fast_IO{
const int IN_LEN = 10000000, OUT_LEN = 10000000;
char ibuf[IN_LEN], obuf[OUT_LEN], *ih = ibuf + IN_LEN, *oh = obuf, *lastin = ibuf + IN_LEN, *lastout = obuf + OUT_LEN - 1;
inline char getchar_(){return (ih == lastin) && (lastin = (ih = ibuf) + fread(ibuf, 1, IN_LEN, stdin), ih == lastin) ? EOF : *ih++;}
inline void putchar_(const char x){if(oh == lastout) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; *oh ++= x;}
inline void flush(){fwrite(obuf, 1, oh - obuf, stdout);}
inline int read(){
int x = 0; char ch = ' ';
while (ch < '0' || ch > '9') ch = getchar_();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar_(); return x;
}
inline void write(int x){
if (x < 0) putchar_('-'), x = -x;
if (x > 9) write(x / 10);
putchar_(x % 10 + 48);
}
} using namespace fast_IO; #define max(a,b) (a>(b)?a:(b)) int main(){
const int n = read(); int q = read();
for (register int i = 1; i <= n; ++i)
a[i] = read();
while (q--){
const int x = read(), l = read(), rr = read();
register int ans = 0;
const int lft = ((rr - l) >> 2 << 2) + l;
for (register int j = l; j <= lft - 4; j += 4){
(ans < (x ^ a[j + 1])) && (ans = x ^ a[j + 1]);
(ans < (x ^ a[j + 2])) && (ans = x ^ a[j + 2]);
(ans < (x ^ a[j + 3])) && (ans = x ^ a[j + 3]);
(ans < (x ^ a[j + 4])) && (ans = x ^ a[j + 4]);
}
for (register int j = lft; j <= rr; ++j)
ans = max(ans, x ^ a[j + 1]);
write(ans), putchar_('\n');
}
flush(); return 0;
}

题目

给定一个长度为 \(n(1 \leq n \leq 2 \times 10^5)\) 的数列,

给定 \(q(1 \leq q \leq 2 \times 10^5)\) 个询问,

每个询问给定三个整数 \(x, l, r\) 。

在区间 \([l,r]\) 中选定一个数,使它与 \(x\) 的异或值最大。

查询

题解

如果是查询区间是一个固定区间,很容易想到,使用trie树。

那么如果查询不是固定区间,那么显然珂以使用可持久化trie树切掉此题,

预计时间复杂度\(\Theta(n\ logn)\)

但是博主才学疏浅,不会这个算法。

就这么结束了吗?

并没有,显然我们可以用类线段树结构维护trie树,复杂度\(\Theta(n\ log^2n)\)

考场上常数过大 一不小心 TLE了

后来一边query一边建树,就AC了。

代码

#pragma GCC optimize(3, "Ofast")
#include <cstdio> namespace fast_IO{
const int IN_LEN = 10000000, OUT_LEN = 10000000;
char ibuf[IN_LEN], obuf[OUT_LEN], *ih = ibuf + IN_LEN, *oh = obuf, *lastin = ibuf + IN_LEN, *lastout = obuf + OUT_LEN - 1;
inline char getchar_(){return (ih == lastin) && (lastin = (ih = ibuf) + fread(ibuf, 1, IN_LEN, stdin), ih == lastin) ? EOF : *ih++;}
inline void putchar_(const char x){if(oh == lastout) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; *oh ++= x;}
inline void flush(){fwrite(obuf, 1, oh - obuf, stdout);}
int read(){
int x = 0; char ch = ' ';
while (ch < '0' || ch > '9') ch = getchar_();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar_(); return x;
}
void write(int x){
if (x < 0) putchar_('-'), x = -x;
if (x > 9) write(x / 10);
putchar_(x % 10 + '0');
}
} using namespace fast_IO; struct Trie{
int c[2];
} trie[57108864];
int cnt = 0; void insert(int x, int rt){
for (register int i = 1 << 29; i; i >>= 1){
bool to = x & i;
if (trie[rt].c[to] == -1){
trie[rt].c[to] = ++cnt;
trie[cnt].c[0] = trie[cnt].c[1] = -1;
}
rt = trie[rt].c[to];
}
} int query(int x, int rt){
int res = 0;
for (register int i = 1 << 29; i; i >>= 1){
bool to = (x ^ i) & i;
if (~trie[rt].c[to]){
res += i;
rt = trie[rt].c[to];
}
else rt = trie[rt].c[to ^ 1];
}
return res;
} int a[200005]; struct Seg{
int root;
int lc, rc;
} seg[800005]; inline int max(int a, int b){
return ((a > b) ? a : b);
} int querySeg(int pos, int l, int r, int x, int y, int v){
if (x <= l && r <= y){
if (!seg[pos].root){
seg[pos].root = ++cnt;
trie[cnt].c[0] = trie[cnt].c[1] = -1;
for (register int i = l; i <= r; ++i)
insert(a[i], seg[pos].root);
}
return query(v, seg[pos].root);
}
int mid = l + r >> 1, res = 0;
if (x <= mid)
res = querySeg(pos << 1, l, mid, x, y, v);
if (y > mid)
res = max(res, querySeg(pos << 1 | 1, mid + 1, r, x, y, v));
return res;
} int main(){
int n = read(), q = read();
trie[0].c[0] = trie[0].c[1] = -1;
for (register int i = 1; i <= n; ++i)
a[i] = read();
while (q--){
int x = read(), l = read() + 1, r = read() + 1;
write(querySeg(1, 1, n, l, r, x)), putchar_('\n');
}
flush(); return 0;
}

[HG]腿部挂件 题解的更多相关文章

  1. [HG]子树问题 题解

    前言 模拟赛赛时SubtaskR3没开long long丢了20分. 题意简述 题目描述 对于一棵有根树(设其节点数为 \(n\) ,则节点编号从 \(1\) 至 \(n\) ),如果它满足所有非根节 ...

  2. [HG]走夜路 题解

    前言 整个机房就我一个人在想动态规划. 想了半天发现一堆性质,结果由于DP中出现折线挂了. 题目描述 某NOIP普及组原题加强版. \(Jim\) 非常怕黑,他有一个手电筒,设手电筒的电量上限为 \( ...

  3. [HG]提高组 题解

    首先很容易想到暴力DP 设状态f[i][j]表示当前放了第i个数,最大的数为j的方案数. 然后根据转移推出实际上是在下图走路的方案数 \[ \left( \left( \begin{matrix} x ...

  4. HGOI 20191030am 题解

    Problem A 腿部挂件 给出$n$个数的序列$a_i$,支持$T$次操作. 每次操作形如$x , l , r$,计算$\max_{i = l}^{r} (a_i \oplus x)$的值. 对于 ...

  5. Tarjan无向图的割点和桥(割边)全网详解&算法笔记&通俗易懂

    更好的阅读体验&惊喜&原文链接 感谢@yxc的腿部挂件 大佬,指出本文不够严谨的地方,万分感谢! Tarjan无向图的割点和桥(割边) 导言 在掌握这个算法前,咱们有几个先决条件. [ ...

  6. [HG]Market 题解

    题目描述 在比特镇一共有 \(n\) 家商店,编号依次为 \(1\) 到 \(n\). 每家商店只会卖一种物品,其中第 \(i\) 家商店的物品单价为 \(c_i\),价值为 \(v_i\),且该商店 ...

  7. [HG]小G坐电梯 题解

    C 小G坐电梯 题目描述 小G来到了著名的某大厦.大厦一共有n层,初始的时候小G在第 A 层. 小G特别想去B层小 M 的办公室看一看,然而因为安保原因,B层已经被封锁无法进入. 但是小G既然来了,就 ...

  8. [HG]walk 题解

    前言 学长博客划水,抄题解,差评. 于是我来重新写一篇正常的题解,虽然解法跟标程不一样,但是复杂度是一样的. 题面 题目描述 在比特镇一共有\(n\)个街区,编号依次为\(1\)到\(n\),它们之间 ...

  9. [HG]钻石游戏diamond 题解

    题面 钻石游戏(diamond) 问题描述: 一个\(M\)行\(N\)列的棋盘,里面放了\(M \times N\)个各种颜色的钻石. 每一次你可以选择任意两个相邻的颜色不同的钻石,进行交换.两个格 ...

随机推荐

  1. 【Python】【demo实验2】【打印乘法口诀表】

    打印乘法口诀表 源代码: # encoding=utf-8 for i in range(1,10): print("\n") for j in range(1,10): if i ...

  2. 300英雄的危机(heroes)

    题面 正解与图书馆馆长的考验一致,都是分层图SPFA: #include <iostream> #include <cstdio> #include <cstring&g ...

  3. python面向对象的多态-类相关内置函数-类内置魔法函数-迭代器协议-上下文管理-04

    多态 一种事物具备不同的形态 例如:水 --> 固态.液态.气态 多态:# 多个不同对象可以相应同一个对象,产生不同的结果 首先强调,多态不是一种特殊的语法,而是一种状态,特性(多个不同对象可以 ...

  4. ELK视频下载

    Elasticsearch , Logstash, Kibana 相关视频下载地址:Beats.Elastic Stack.ElasticSearch.Kibana.Logstash下载地址:链接:h ...

  5. RBAC(基于角色的访问控制)用户权限管理数据库设计

    RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...

  6. LeetCode——全排列

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3]输出:[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3 ...

  7. mysql5.7主从复制及相关注意点!

    首先在两台不同IP的服务器安装相同版本的mysql(也可以docker 用 network模式).例如 主数据库(master) 178.18.0.2 my.cnf的设置(一般在/etc/mysql/ ...

  8. PIL:处理图像的好模块

    介绍 PIL是一个专门用来处理图像的模块,可以对图象进行各种各样的变换 打开一张图片 from PIL import Image # 调用Image下的open方法,即可打开一张图片 # 得到的im便 ...

  9. deep_learning_LSTM长短期记忆神经网络处理Mnist数据集

    1.RNN(Recurrent Neural Network)循环神经网络模型 详见RNN循环神经网络:https://www.cnblogs.com/pinard/p/6509630.html 2. ...

  10. linux内核配置与编译

    配置内核:配置硬件和软件需的部分. make config:基于文本模式的交互式配置.(一问一答) make menuconfig:基于文本模式菜单性配置.(直观简单高效) <*>会产生b ...