[HG]腿部挂件 题解
前言
暴力跑的比正解快。
以下暴力(循环展开+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]腿部挂件 题解的更多相关文章
- [HG]子树问题 题解
前言 模拟赛赛时SubtaskR3没开long long丢了20分. 题意简述 题目描述 对于一棵有根树(设其节点数为 \(n\) ,则节点编号从 \(1\) 至 \(n\) ),如果它满足所有非根节 ...
- [HG]走夜路 题解
前言 整个机房就我一个人在想动态规划. 想了半天发现一堆性质,结果由于DP中出现折线挂了. 题目描述 某NOIP普及组原题加强版. \(Jim\) 非常怕黑,他有一个手电筒,设手电筒的电量上限为 \( ...
- [HG]提高组 题解
首先很容易想到暴力DP 设状态f[i][j]表示当前放了第i个数,最大的数为j的方案数. 然后根据转移推出实际上是在下图走路的方案数 \[ \left( \left( \begin{matrix} x ...
- HGOI 20191030am 题解
Problem A 腿部挂件 给出$n$个数的序列$a_i$,支持$T$次操作. 每次操作形如$x , l , r$,计算$\max_{i = l}^{r} (a_i \oplus x)$的值. 对于 ...
- Tarjan无向图的割点和桥(割边)全网详解&算法笔记&通俗易懂
更好的阅读体验&惊喜&原文链接 感谢@yxc的腿部挂件 大佬,指出本文不够严谨的地方,万分感谢! Tarjan无向图的割点和桥(割边) 导言 在掌握这个算法前,咱们有几个先决条件. [ ...
- [HG]Market 题解
题目描述 在比特镇一共有 \(n\) 家商店,编号依次为 \(1\) 到 \(n\). 每家商店只会卖一种物品,其中第 \(i\) 家商店的物品单价为 \(c_i\),价值为 \(v_i\),且该商店 ...
- [HG]小G坐电梯 题解
C 小G坐电梯 题目描述 小G来到了著名的某大厦.大厦一共有n层,初始的时候小G在第 A 层. 小G特别想去B层小 M 的办公室看一看,然而因为安保原因,B层已经被封锁无法进入. 但是小G既然来了,就 ...
- [HG]walk 题解
前言 学长博客划水,抄题解,差评. 于是我来重新写一篇正常的题解,虽然解法跟标程不一样,但是复杂度是一样的. 题面 题目描述 在比特镇一共有\(n\)个街区,编号依次为\(1\)到\(n\),它们之间 ...
- [HG]钻石游戏diamond 题解
题面 钻石游戏(diamond) 问题描述: 一个\(M\)行\(N\)列的棋盘,里面放了\(M \times N\)个各种颜色的钻石. 每一次你可以选择任意两个相邻的颜色不同的钻石,进行交换.两个格 ...
随机推荐
- Python学习【day02】- 运算符与基本类型
Python语言支持以下类型的运算符: 算术运算符 操作符 描述 示例(a=10.b=21) + 加法 相加运算两侧的值 a + b = 31 - 减法 操作符右侧数减去左侧操作数 a – b = - ...
- ZooKeeper原理及介绍
Zookeeper简介 1.1 什么是Zookeeper ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是大数据生态中的重要组件.它是 ...
- C# 面向对象6 之前的复习
复习练习 THIS:调用当前类的构造函数
- 权限(rwx)对于目录与文件的意义
1-权限对于目录的意义 首先要明白的是目录主要的内容是记录文件名列表和子目录列表,而不是实际存放数据的地方. r权限:拥有此权限表示可以读取目录结构列表,也就是说可以查看目录下的文件名和子目录名,注意 ...
- Vue 路由(对路由页面编写做规范)
前言 上一篇写了“Vue 路由拦截(对某些页面需要登陆才能访问)” 的博客,此篇是续上篇对路由页面模块化,可以看到之前的路由配置都写在main.js中,真正开发肯定不能都写在main.js,所以我们要 ...
- vue学习【一】vue引用封装echarts并展示多个echarts图表
大家好,我是一叶,经过一段时间对vue的学习,我打算把vue做一个系列,把踩过的坑和大家分享一下. 现在开始第一章:vue引用并封装echarts 在文章开始前,我先舔波echarts(真香).阿里的 ...
- Delphi 子界类型
- 18、nginx优化
一.性能优化概述 基询imm能优化,那么在性能优化这一章,我们将分为如下几个方面做介绍 1.首先我们需要了解性能优化要考虑哪些方面. 2.然后我们需要了解性能优化必须要用到的压力测试工具ab. 3.最 ...
- Linux grep命令 -- 三剑客老三
常用选项 -E :开启扩展(Extend)的正则表达式. -i :忽略大小写(ignore case). -v :反过来(invert),只打印没有匹配的,而匹配的反而不打印. -n :显示行号 -w ...
- 记一次部署PHP遇到的编码问题故障
php开发给我项目和数据库,我按正常部署流程部署,开始发现之梦的后台登陆不了,后发现是属主属组不对,代码直接解压后是root的,更改后,后台能登陆,但部分显示乱码.后将正常的数据库文件重新导入后,显示 ...