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. 钉钉开发笔记(5)android系统中html软键盘的适配

    最近项目中发现个别Android手机中存在弹出的软键盘会遮挡输入框的现象,最后自己写了一个方法(如下),问题基本解决. 记录下来,防止忘记.有什么不对的地方欢迎指正.O(∩_∩)O 1 //键盘适配 ...

  2. Vagrant 使用 samba 共享文件夹

    在windows下使用Vagrant时可以使用samba方式共享文件夹. 配置如下: config.vm.synced_folder "./", "/path/to/we ...

  3. thinkphp+memcache缓存例子

    public function dailyRelays() { $history = I('post.history'); $da = new \Home\Model\DailyrelayModel( ...

  4. Debug 时,执行语句

    Display View The Display View displays the result of evaluating an expression in the context of the ...

  5. Leaflet入门:添加点线面并导入GeoJSON数据|Tutorial of Leaflet: Adding Points, Lines, Polygons and Import GeoJSON File

    Web GIS系列: 1.搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 2.使用GeoServer+QGIS发布WMTS服务 3.使 ...

  6. Grails项目开发——前端请求跨域问题

    Grails项目开发--前端请求跨域问题 最近做项目采用前后端分离的思想,使用Grails作为后台开发Restful API供前端调用. 在项目开发的过程中,遇到前端没办法通过ajax访问到后台接口的 ...

  7. 51nod 1421 最大MOD值(高妙的调和级数复杂度)

    有一个a数组,里面有n个整数.现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai ≥ aj. Input 单组测试数据. 第一行包含一个整数n,表示数组a的 ...

  8. Reporting Service服务SharePoint集成模式安装配置(8、配置用于SharePoint 2010的Reporting service模式)

    从SQL Server 2012 起, SQL Server Reporting Service可以完全集成进SharePoint的场,直接作为SharePoint 的组件部分来运行,没有独立的Win ...

  9. 关于.net和java des加密

    在.net和java环境中对于des加密,有几点要协同的地方: 密钥和密钥向量Key IV 加密模式CipherMode 填充模式PaddingMode 密文编码方式 下面一段.net的des加密方式 ...

  10. IO--性能计数器

    --===================================================================== --在分析磁盘队列时,应参考数据库的其他计数器,如Che ...