P3292 [SCOI2016]幸运数字 [线性基+倍增]
线性基+倍增
// by Isaunoya
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using ll = long long ;
const int _ = 1 << 21;
struct I {
char fin[_], *p1 = fin, *p2 = fin;
inline char gc() {
return (p1 == p2) && (p2 = (p1 = fin) + fread(fin, 1, _, stdin), p1 == p2) ? EOF : *p1++;
}
inline I& operator>>(int& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c & 15);
while ((c = gc()) > 47) x = (x << 1) + (x << 3) + (c & 15);
x = sign ? x : -x;
return *this;
}
inline I& operator>>(ll& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c & 15);
while ((c = gc()) > 47) x = (x << 1) + (x << 3) + (c & 15);
x = sign ? x : -x;
return *this;
}
} in;
struct O {
char st[100], fout[_];
signed stk = 0, top = 0;
inline void flush() {
fwrite(fout, 1, top, stdout), fflush(stdout), top = 0;
}
inline O& operator<<(ll x) {
if (top > (1 << 20)) flush();
if (x < 0) fout[top++] = 45, x = -x;
do
st[++stk] = x % 10 ^ 48, x /= 10;
while (x);
while (stk) fout[top++] = st[stk--];
return *this;
}
inline O& operator<<(char x) {
fout[top++] = x;
return *this;
}
inline O& operator<<(string s) {
if (top > (1 << 20)) flush();
for (char x : s) fout[top++] = x;
return *this;
}
} out;
#define pb emplace_back
#define fir first
#define sec second
template < class T > inline void cmax(T & x , const T & y) {
(x < y) && (x = y) ;
}
template < class T > inline void cmin(T & x , const T & y) {
(x > y) && (x = y) ;
}
int n , q ;
const int N = 2e4 + 10 ;
ll g[N] , d[N][22][65] ;
void ins(ll x , ll * p) {
for(int i = 63 ; ~ i ; i --)
if(x & (1ll << i)) {
if(! p[i]) p[i] = x ;
x ^= p[i] ;
}
}
void mergeto(ll * a , ll * b) { for(int i = 63 ; ~ i ; i --) if(a[i]) ins(a[i] , b) ; }
struct edge {int v , nxt ;} ; edge e[N << 1] ;
int cnt = 0 , head[N] ;
void add(int u , int v) { e[++ cnt] = { v , head[u] } ; head[u] = cnt ; }
int fa[N] , dep[N] , f[N][22] ;
void dfs(int u) {
for(int i = head[u] ; i ; i = e[i].nxt) {
int v = e[i].v ;
if(v ^ fa[u]) { fa[v] = u ; dep[v] = dep[u] + 1 ; dfs(v) ; }
}
}
ll ans[65] ;
inline int Lca(int x , int y) {
if(dep[x] < dep[y]) swap(x , y) ;
for(int i = 20 ; ~ i ; i --) if(dep[f[x][i]] >= dep[y]) x = f[x][i] ;
if(x == y) return x ;
for(int i = 20 ; ~ i ; i --) if(f[x][i] ^ f[y][i]) { x = f[x][i] ; y = f[y][i] ; }
return f[x][0] ;
}
inline int getdis(int lca , int y) { return dep[y] - dep[lca] + 1 ; }
inline ll query(int x , int y) {
memset(ans , 0 , sizeof(ans)) ;
int lca = Lca(x , y) , dis = getdis(lca , x) ;
for(int i = 20 ; ~ i ; i --) if(dis & (1 << i)) mergeto(d[x][i] , ans) , x = f[x][i] ;
dis = getdis(lca , y) ;
for(int i = 20 ; ~ i ; i --) if(dis & (1 << i)) mergeto(d[y][i] , ans) , y = f[y][i] ;
ll res = 0 ; for(int i = 63 ; ~ i ; i --) cmax(res , res ^ ans[i]) ;
return res ;
}
signed main() {
#ifdef _WIN64
freopen("testdata.in" , "r" , stdin) ;
#endif
in >> n >> q ;
rep(i , 1 , n) { in >> g[i] ; }
rep(i , 1 , n - 1) { int u , v ; in >> u >> v ; add(u , v) ; add(v , u) ; }
dfs(1) ; rep(i , 1 , n) f[i][0] = fa[i] ; rep(i , 1 , n) ins(g[i] , d[i][0]) ;
rep(j , 1 , 20)
rep(i , 1 , n) { f[i][j] = f[f[i][j - 1]][j - 1] ; mergeto(d[i][j - 1] , d[i][j]) ; mergeto(d[f[i][j - 1]][j - 1] , d[i][j]) ; }
while(q --) { int u , v ; in >> u >> v ; out << query(u , v) << '\n' ; }
return out.flush(), 0;
}
P3292 [SCOI2016]幸运数字 [线性基+倍增]的更多相关文章
- 洛谷P3292 [SCOI2016]幸运数字 线性基+倍增
P3292 [SCOI2016]幸运数字 传送门 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在 ...
- BZOJ 4568: [Scoi2016]幸运数字 [线性基 倍增]
4568: [Scoi2016]幸运数字 题意:一颗带点权的树,求树上两点间异或值最大子集的异或值 显然要用线性基 可以用倍增的思想,维护每个点向上\(2^j\)个祖先这些点的线性基,求lca的时候合 ...
- P3292 [SCOI2016]幸运数字 线性基
正解:线性基+倍增 解题报告: 先放下传送门QAQ 然后这题,其实没什么太大的技术含量,,,?就几个知识点套在一起,除了代码长以外没任何意义,主要因为想复习下线性基的题目所以还是写下,,, 随便写下思 ...
- 洛谷P3292 [SCOI2016] 幸运数字 [线性基,倍增]
题目传送门 幸运数字 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的 ...
- BZOJ4568: [Scoi2016]幸运数字(线性基 倍增)
题意 题目链接 Sol 线性基是可以合并的 倍增维护一下 然后就做完了?? 喵喵喵? // luogu-judger-enable-o2 #include<bits/stdc++.h> # ...
- BZOJ 4568 [Scoi2016]幸运数字 ——线性基 倍增
[题目分析] 考虑异或的最大值,维护线性基就可以了. 但是有多次的询问,树剖或者倍增都可以. 想了想树剖动辄数百行的代码. 算了,我还是写倍增吧. 注:被位运算和大于号的优先级坑了一次,QaQ [代码 ...
- bzoj4568 [Scoi2016]幸运数字 线性基+树链剖分
A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征.一些旅行者希望游览 A ...
- [SCOI2016]幸运数字 线性基
题面 题面 题解 题面意思非常明确:求树上一条链的最大异或和. 我们用倍增的思想. 将这条链分成2部分:x ---> lca , lca ---> y 分别求出这2个部分的线性基,然后合并 ...
- BZOJ.4516.[SCOI2016]幸运数字(线性基 点分治)
题目链接 线性基可以\(O(log^2)\)暴力合并.又是树上路径问题,考虑点分治. 对于每个点i求解 LCA(u,v)==i 时的询问(u,v),只需求出这个点到其它点的线性基后,暴力合并. LCA ...
随机推荐
- 【题解】P1020 导弹拦截
[题解]P1020 导弹拦截 从n^2到nlogn 第二问就是贪心,不多说 第一问: 简化题意:求最长不下降子序列 普通n^2: for (int i = 1; i <= n; i++) for ...
- CentOS安装-(CentOS7)最小化安装
镜像:CentOS-7-x86_64-DVD-1908.iso 1.将安装光盘插入服务器,开机会读取系统安装程序,选择 Install CentOS 7 2.安装过程是图形界面,可以选择熟悉的语言执行 ...
- Hexo搭建静态博客踩坑日记(一)
前言 博客折腾一次就好, 找一个适合自己的博客平台, 专注于内容进行提升. 方式一: 自己买服务器, 域名, 写前端, 后端(前后分离最折腾, 不分离还好一点)... 方式二: 利用Hexo, Hug ...
- zabbix-mysql迁移分离
io过高,迁移mysql 停掉zabbix 导出数据库的zabbix库 导入到新机器,并启动mysql 1:修改zabbix_server.conf文件里DB相关的地址,用户名和密码. vim /et ...
- 2019IT运维大会上海站 智和信通解析等保2.0支撑
2019IT运维大会上海站 智和信通解析等保2.0支撑 2019年11月14日上午8:30-12:10,上海锦荣国际大酒店二层锦荣厅
- pikachu-远程代码、命令执行漏洞(RCE)
一.RCE概述 1.1 什么是RCE? RCE漏洞,可以让攻击者直接向后台服务器远程注入操作系统命令或者代码,从而控制后台系统. 1.2 远程系统命令执行 一般出现这种漏洞,是因为应用系统从设计上需要 ...
- (1) Q#学习笔记 - 第一章 QDK安装
2020/2/22,开始学习Q#. 第1章 QDK的安装 第1节 简介 QDK是Microsoft Quantum 开发工具包,可以进行量子编程,主要包含: Q# 编程语言 在 Q# 中对复杂功能进行 ...
- 让$(window).scroll()监听事件只执行一次
可以用jQuery中的unbind()来进行事件解绑. $(window).scroll(function() { console.log("滚离顶部" + $(document) ...
- 获取域hash并破解
ntds.dit ntds.dit是主要的AD数据库,存放在C:\Windows\NTDS\NTDS.dit,包括有关域用户,组和组成员身份的信息.它还包括域中所有用户的密码哈希值.为了进一步保护密码 ...
- Linux报错:rm: cannot remove 'xxx': Is a directory
rm: cannot remove 'xxx': Is a directory表示这个文件是无法remove移除的,因此我们不能仅使用rm来将这个文件夹进行删除,需要使用: rm -rf 命令则可以将 ...