前言

暴力跑的比正解快。

以下暴力(循环展开+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. Oracle表的Rowid字段

    Rowid 字段类型: Rowid 是一行数据的一个唯一标识. ROWID 是数据的详细地址,通过 rowid,oracle 可以快速的定位某行具体的数据的位置. ROWID 可以分为物理 rowid ...

  2. DLL的创建与使用

    一.动态链接库(DLL) 动态链接库提供了一种方法,使进程可以调用不属于其执行代码的函数.函数的可执行代码位于一个.dll文件中,该文件包含一个或多个已被编译.链接并使用它们的进程分开存储的函数. 优 ...

  3. 洛谷 P2633 Count on a tree 题解

    题面 对于每个点建立一颗主席树: 然后按照树上差分的思想统计主席树的前缀和: lca+主席树+前向星存表就可以了: #include <bits/stdc++.h> #define inc ...

  4. 2019中山纪念中学夏令营-Day4[JZOJ]

    Begin (题目的排序方式:难易程度) 什么是对拍: 对拍是一种在写完程序后,验证自己程序是不是正解的比较方便的方法. 实现过程: 对同一道题,再打一个暴力程序,然后用一些大数据等跑暴力程序来进行验 ...

  5. jquery的offset().top与javascript的offsetTop区别?

    offset().top是jquery的方法,需引入jquery,它获取你绑定元素上边框相对于html上边界的偏移量 offsetTop是原生js的方法,它获取你绑定元素上边框相对于离自己最近且pos ...

  6. 剑指offer-左旋转字符串-知识迁移能力-python

    题目描述汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abcX ...

  7. Centos7:dubbo监控中心安装,配置和使用

    制作dubbo-admin.war文件 下载dubbo-admin https://github.com/alibaba/dubbo 注:2.6版本后源码中不包含dubbo-admin工程 在dubb ...

  8. Vue 路由(对路由页面编写做规范)

    前言 上一篇写了“Vue 路由拦截(对某些页面需要登陆才能访问)” 的博客,此篇是续上篇对路由页面模块化,可以看到之前的路由配置都写在main.js中,真正开发肯定不能都写在main.js,所以我们要 ...

  9. 主流浏览器内核(IE、Chrome、Firefox、Safari、Opera)

    IE浏览器,使用Trident浏览器内核,又称为IE内核.只用于Windows平台,而且并不是开源的: chrome浏览器,目前使用的是Blink浏览器内核.浏览器内核的演进过程:Chromium  ...

  10. 并发编程系列:Java线程池的使用方式,核心运行原理、以及注意事项

    并发编程系列: 高并发编程系列:4种常用Java线程锁的特点,性能比较.使用场景 线程池的缘由 java中为了提高并发度,可以使用多线程共同执行,但是如果有大量线程短时间之内被创建和销毁,会占用大量的 ...