[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=5338

[算法]

首先对这棵树进行树链剖分

那么我们就将一个树上的问题转化为一个序列上的问题

建立可持久化字典树维护最大异或值即可

时间复杂度 : O(NlogN ^ 2)

[代码]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5 + ;
const int MAXLOG = ; struct edge
{
int to , nxt;
} e[N << ]; int n , tot , timer , q;
int head[N] , size[N] , top[N] , a[N] , perm[N] , l[N] , r[N] , son[N] , father[N] , depth[N] , rt[N]; struct Presitent_Trie
{
int sz;
int child[N * MAXLOG][] , latest[N * MAXLOG];
Presitent_Trie()
{
sz = ;
}
inline void modify(int bit , int &now , int old , int x , int loc)
{
now = ++sz;
child[now][] = child[old][] , child[now][] = child[old][];
latest[now] = loc;
if (bit < ) return;
int value = ;
if (x & ( << bit)) value = ;
modify(bit - , child[now][value] , child[old][value] , x , loc);
}
inline int query(int bit , int now , int lft , int x)
{
if (bit < )
return ;
int value = ;
if (x & ( << bit)) value = ;
if (latest[child[now][value]] >= lft) return ( << bit) + query(bit - , child[now][value] , lft , x);
else return query(bit - , child[now][value ^ ] , lft , x);
}
} PT; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v)
{
++tot;
e[tot] = (edge){v , head[u]};
head[u] = tot;
}
inline void dfs1(int u , int par)
{
size[u] = ;
depth[u] = depth[par] + ;
father[u] = par;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == par) continue;
dfs1(v , u);
size[u] += size[v];
if (size[v] > size[son[u]]) son[u] = v;
}
}
inline void dfs2(int u , int t)
{
top[u] = t;
l[u] = ++timer;
if (son[u]) dfs2(son[u] , t);
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == father[u] || v == son[u]) continue;
dfs2(v , v);
}
r[u] = timer;
}
inline bool cmp(int x , int y)
{
return l[x] < l[y];
}
inline int query(int x , int y , int z)
{
int ans = ;
while (top[x] != top[y])
{
if (depth[top[x]] > depth[top[y]]) swap(x , y);
chkmax(ans , PT.query(MAXLOG - , rt[l[y]] , l[top[y]] , z));
y = father[top[y]];
}
if (depth[x] > depth[y]) swap(x , y);
chkmax(ans , PT.query(MAXLOG - , rt[l[y]] , l[x] , z));
return ans;
} int main()
{ read(n); read(q);
for (int i = ; i <= n; ++i) read(a[i]);
for (int i = ; i < n; ++i)
{
int x , y;
read(x); read(y);
addedge(x , y);
addedge(y , x);
}
dfs1( , );
dfs2( , );
for (int i = ; i <= n; ++i) perm[i] = i;
sort(perm + , perm + n + , cmp);
for (int i = ; i <= n; ++i) PT.modify(MAXLOG - , rt[i] , rt[i - ] , a[perm[i]] , i);
while (q--)
{
int type;
read(type);
if (type == )
{
int x , y;
read(x); read(y);
printf("%d\n" , PT.query(MAXLOG - , rt[r[x]] , l[x] , y));
} else
{
int x , y , z;
read(x); read(y); read(z);
printf("%d\n" , query(x , y , z));
}
} return ; }

[TJOI 2018] XOR的更多相关文章

  1. 「TJOI 2018」教科书般的亵渎

    「TJOI 2018」教科书般的亵渎 题目描述 小豆喜欢玩游戏,现在他在玩一个游戏遇到这样的场面,每个怪的血量为 \(a_i\) ,且每个怪物血量均不相同, 小豆手里有无限张"亵渎" ...

  2. 「TJOI 2018」游园会 Party

    「TJOI 2018」游园会 Party 题目描述 小豆参加了 \(NOI\) 的游园会,会场上每完成一个项目就会获得一个奖章,奖章只会是 \(N, O, I\) 的字样. 在会场上他收集到了 \(K ...

  3. [TJOI 2018]智力竞赛

    Description 题库链接 给出一张 \(m\) 个点的有向图.问可重最小路径覆盖是否 \(\leq n+1\) .若不,求最多用 \(n+1\) 条路径去覆盖,最大化未覆盖点点权最小值. \( ...

  4. 【TJOI 2018】数学计算

    [题目链接] 点击打开链接 [算法] 线段树维护区间乘积 [代码] #include<bits/stdc++.h> using namespace std; #define MAXQ 10 ...

  5. [TJOI 2018]游园会

    题意:求NOI的合法串... 思路: 首先这个似乎和后缀自动机没关系(话说TJ不考后缀自动机??),其实就是一个\(DP\)套\(DP\),考虑如果不看兑奖串就是一个LCS,当出现时多记一维即可. # ...

  6. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries

    地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...

  7. 2018.08.22 hyc的xor/mex(线段树/01trie)

    hyc的xor/mex 描述 NOIP2017就要来了,备战太累,不如做做hyc的新题? 找回自信吧! 一句话题意:n个数,m个操作 操作具体来讲分两步 1.读入x,把n个数全部xor上x 2.询问当 ...

  8. 【线性基】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem A. XOR

    题意:给你一些数,问你是否能够将它们划分成两个集合,使得这两个集合的异或和之差的绝对值最小. 设所有数的异或和为S,集合A的异或和为A. 首先,S的0的位对答案不造成影响. S的最高位1,所对应的A的 ...

  9. The 2018 ACM-ICPC Asia Qingdao Regional Contest K XOR Clique

    K XOR Clique BaoBao has a sequence a​1​​,a​2​​,...,a​n​​. He would like to find a subset S of {1,2,. ...

随机推荐

  1. 2.SOAP 语法

    SOAP 消息的基本结构 <?xml version="1.0"?> <soap:Envelope xmlns="http://www.w3.org/2 ...

  2. Linux基础(2)- 用户、群组和权限

    一.用户.群组和权限 1)  新建用户natasha,uid为1100,gid为555,备注信息为“master” 2)  修改natasha用户的家目录为/Natasha 3)  查看用户信息配置文 ...

  3. Libx264 编码错误 Input picture width(320) is greater than stride (0)

    Ffmpeg libx264编码出现 Input picture width(320) is greater than stride (0),问题出在视频格式不正确. libx264 编码要求输入源的 ...

  4. Logical Volume Manager (Linux)

    http://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux) Logical Volume Manager (Linux) From Wiki ...

  5. python--函数嵌套 命名空间

    函数的嵌套调用 def func(): print(123) def func2(): func() print(345) func2() #结果:123 345 函数的嵌套定义 def func() ...

  6. python的id()函数的一个小方面(转载)

    >>> a = 2 >>> b = 2 >>> id(a) 21132060 >>> id(b) 21132060 >&g ...

  7. java线程中断[interrupt()函数] (转载)

    一个正常的线程中断: 从运行到真正的结束,应该有三个阶段: 正常运行. 处理结束前的工作,也就是准备结束. 结束退出. Java曾经提供过抢占式限制中断,但问题多多,例如的Thread.stop.另一 ...

  8. storm是怎样保证at least once语义的

    背景 本篇看看storm是通过什么机制来保证消息至少处理一次的语义的. storm中的一些原语 要说明上面的问题,得先了解storm中的一些原语,比方: tuple和message 在storm中,消 ...

  9. javaweb开发之jsp

    一.WEB应用的目录结构 通常我们是在IDE中创建web应用程序,IDE自动为我们实现了WEB的目录结构,下面来看如何徒手创建一个WEB程序. 首先来看一下Tomcat自带的一个web应用的目录结构 ...

  10. [自动化平台系列] - 初次使用 Macaca-前端自动化测试(3)

    1. 如果是一个列表页面,当要触发编辑页面是如何做的呢?其实我测试只要点击第一条数据去编辑就好啦!如果页面结构如下 <li class="myatc-li"> < ...