link

题目大意:给一棵树,每个点有个权值,N<=2万

20万次询问,每次询问查询某两个点路径上所有点的权值xjb异或的最大值

首先看到xjb异或就可以断定是线性基了

并且由于这是树上问题我们可以通过树剖Dfs序之类的手段搞成序列问题

但是树剖+线段树的复杂度是 \(O(\log ^2N\log^2 2^{60})\) 的很明显会T

由于本题不需要修改,可以考虑维护一个倍增,bij代表点i向上跳2的j次方这段路上所有点权值的一个线性基

然后查询就是 \(O(\log N\log^2 2^{60})\) 的了,开O2勉强可过

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; int n, q;
long long init[20010];
vector<int> out[20010];
int fa[20010][16], depth[20010]; struct linear_basis
{
long long sb[61]; linear_basis() { memset(this, 0, sizeof(linear_basis)); } void insert(long long x)
{
for (int i = 60; i >= 0; i--) if (x & (1LL << i))
{
if (sb[i] == 0) { sb[i] = x; return; }
else x ^= sb[i];
}
} long long query()
{
long long ans = 0;
for (int i = 60; i >= 0; i--)
{
if ((ans ^ sb[i]) > ans) ans ^= sb[i];
}
return ans;
}
} b[20010][16]; linear_basis operator+(const linear_basis &a, const linear_basis &b)
{
linear_basis ans;
for (int i = 60; i >= 0; i--) if (a.sb[i]) ans.insert(a.sb[i]);
for (int i = 60; i >= 0; i--) if (b.sb[i]) ans.insert(b.sb[i]);
return ans;
} void dfs(int x) { for (int i : out[x]) if (fa[x][0] != i) fa[i][0] = x, depth[i] = depth[x] + 1, dfs(i); } linear_basis qlca(int x, int y)
{
if (depth[x] < depth[y]) swap(x, y);
linear_basis ans;
int sb = depth[x] - depth[y];
for (int i = 15; i >= 0; i--) if (sb & (1 << i)) ans = ans + b[x][i], x = fa[x][i];
if (x == y) { ans.insert(init[x]); return ans; }
for (int i = 15; i >= 0; i--) if (fa[x][i] != fa[y][i]) ans = ans + b[x][i] + b[y][i], x = fa[x][i], y = fa[y][i];
ans.insert(init[fa[x][0]]);
ans.insert(init[x]);
ans.insert(init[y]);
return ans;
} int main()
{
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%lld", &init[i]);
for (int x, y, i = 1; i < n; i++) scanf("%d%d", &x, &y), out[x].push_back(y), out[y].push_back(x);
dfs(1);
for (int i = 1; i <= n; i++) b[i][0].insert(init[i]);
for (int j = 1; j <= 15; j++) for (int i = 1; i <= n; i++)
{
fa[i][j] = fa[fa[i][j - 1]][j - 1];
b[i][j] = b[i][j - 1] + b[fa[i][j - 1]][j - 1];
}
for (int x, y, i = 1; i <= q; i++)
{
scanf("%d%d", &x, &y);
linear_basis fuck = qlca(x, y);
printf("%lld\n", fuck.query());
}
return 0;
}

另外观察到本题可以离线,于是我们可以考虑淀粉质?

淀粉质,每次淀粉质维护当前树上每个点到当前的根的线性基(每次只需要往他父亲的线性基里插入一个数然后粘过来,复杂度是log的),然后查询只需要合并两个线性基,复杂度比在线lca少一个log N。

不太好写所以没写

luogu3292 [SCOI2016]幸运数字的更多相关文章

  1. BZOJ 4568: [Scoi2016]幸运数字 [线性基 倍增]

    4568: [Scoi2016]幸运数字 题意:一颗带点权的树,求树上两点间异或值最大子集的异或值 显然要用线性基 可以用倍增的思想,维护每个点向上\(2^j\)个祖先这些点的线性基,求lca的时候合 ...

  2. [SCOI2016]幸运数字 树链剖分,线性基

    [SCOI2016]幸运数字 LG传送门 为了快乐,我们用树剖写这题. 强行树剖,线段树上每个结点维护一个线性基,每次查询暴力合并. 瞎分析一波复杂度:树剖两点之间\(\log n\)条重链,每条重链 ...

  3. bzoj 4568: [Scoi2016]幸运数字

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 848  Solved: 336[Submit][Status ...

  4. [洛谷P3292] [SCOI2016]幸运数字

    洛谷题目链接:[SCOI2016]幸运数字 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城 ...

  5. 【BZOJ 4568】 4568: [Scoi2016]幸运数字 (线性基+树链剖分+线段树)

    4568: [Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形 ...

  6. [BZOJ4568][Scoi2016]幸运数字 倍增+线性基

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1791  Solved: 685[Submit][Statu ...

  7. [BZOJ4568][SCOI2016]幸运数字(倍增LCA,点分治+线性基)

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2131  Solved: 865[Submit][Statu ...

  8. 【BZOJ4568】[Scoi2016]幸运数字 倍增+线性基

    [BZOJ4568][Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念 ...

  9. bzoj4568: [Scoi2016]幸运数字(LCA+线性基)

    4568: [Scoi2016]幸运数字 题目:传送门 题解: 好题!!! 之前就看过,当时说是要用线性基...就没学 填坑填坑: %%%线性基 && 神犇 主要还是对于线性基的运用和 ...

随机推荐

  1. eclipse 环境安装

    eclipse 安装SVN http://blog.csdn.net/rilaohn/article/details/70312827 eclipse server 不见了 http://www.jb ...

  2. 2015年2月编程语言排行榜:JavaScript排名达到历史最高

    JavaScript在赢得2014年最后一个月的TIOBE编程语言奖后,并且还在不断走强.在二月份JavaScript就超过了PHP,并 且达到它有史以来最高的位置,排行到TOP 6.另一方面,Obj ...

  3. Redis只作为缓存,不做持久化的配置

    #1.配置缓存内存限制和清理策略 #作为缓存服务器,如果不加以限制内存的话,就很有可能出现将整台服务器内存都耗光的情况,可以在redis的配置文件里面设置: #example: # 限定最多使用1.5 ...

  4. Qt5.7学习

    一 Qt简介(Build your world with Qt) 二 Qt5.7.0的安装 三 Qt系统构造库及常用类 四 信号(signal)与槽(slot)通信机制 五 QtDesigner开发工 ...

  5. duilib界面库

    xml编写界面库 notify控制程序 win32程序 winmain主函数 复杂控件自绘

  6. Use formatter to format your JAVA code

    In order to make the codes looks unified and make it easy to understand, it's better to use the same ...

  7. bbs3

    第三天 昨日回顾: 1 验证码刷新 -$("#img_code")[0].src+="?" -本质就是向这个地址又发了一次请求 2 js中字符串拼接 -es5之 ...

  8. [欣赏代码片段] (JavaScript) 你使用过getComputedStyle没有

    (function() { // IE8 ployfill for GetComputed Style (for Responsive Script below) if (!window.getCom ...

  9. Maven远程发布项目到tomcat

    向tomcat发布项目,每次都要打包传送再运行,非常麻烦.偶然一天发现maven有插件可以直接发布到tomcat.今天把大体过程介绍给大家. 首先在pom中配置tomcat插件: <plugin ...

  10. 关于解决百度sitemap1.0一直提示校验中问题

    实际原因是php设置问题,各个版本对应的设置有些不一样. php版本改成就好了. 修复方法:在插件里找到插件:\baidusubmit\inc.找到sitemap.php,查找curl_setopt( ...