先求出dfs序,然后建立线段树,线段树每个节点套一个set。

  修改操作只需要改被子树区间完全覆盖的线段树节点,将其节点中set的原来的值删除,加入新值。

  询问操作查询单点到根的所有节点上的set中与查询值异或起来最大的那个。

  查询set中的数与x异或的最大值,可以从高位到低位枚举二进制位,根据x的二进制位,查询一些set中数值的存在情况,例如加入x的二进制第y位为1,那么如果set中存在第y位为0的数字,明显可以使得答案更大。下面代码中这段的代码类似于二分。

  时间复杂度O(nlogn^3)

  

  代码

 #include<cstdio>
#include<set>
#include<algorithm>
#include<cstring>
#define N 500010
using namespace std;
int n,m,i,a,b,typ;
int dp,p[N],pre[N],tt[N];
int tot,L[N],R[N],l[N],r[N],v[N],stack[N],deep,flag;
multiset<int> Set[N];
void link(int x,int y)
{
dp++;pre[dp]=p[x];p[x]=dp;tt[dp]=y;
}
void dfs()
{
int i;
while (deep)
{
if (L[stack[deep]]==)
{
tot++;
L[stack[deep]]=tot;
}
if (p[stack[deep]])
{
i=p[stack[deep]];
deep++;stack[deep]=tt[i];
p[stack[deep-]]=pre[i];
}
else
{
R[stack[deep]]=tot;
deep--;
}
}
}
void build(int x,int a,int b)
{
int m;
l[x]=a;r[x]=b;
Set[x].clear();
Set[x].insert(-);
Set[x].insert(<<);
if (b-a>)
{
m=(a+b)>>;
build(*x,a,m);
build(*x+,m,b);
}
}
void change(int x,int a,int b,int c,int d)
{
if ((a<=l[x])&&(r[x]<=b))
{
Set[x].insert(d);
if (flag)
Set[x].erase(c);
return;
}
int m=(l[x]+r[x])>>;
if (a<m) change(*x,a,b,c,d);
if (m<b) change(*x+,a,b,c,d);
}
int Q(int x,int y)
{
int l,r,m,p,q;
if (Set[x].size()==) return ;
l=;r=(<<)-;
while (l!=r)
{
m=(l+r)>>;
multiset<int>::iterator it=Set[x].upper_bound(m);
p=*it;
q=*(--it);
if (p>r)
r=m;
else
if (q<l)
l=m+;
else
{
if ((l^y)>((m+)^y))
r=m;
else
l=m+;
}
}
return l^y;
}
int query(int x,int a,int b,int c)
{
int ans=Q(x,c);
if ((a<=l[x])&&(r[x]<=b))
return ans;
int m=(l[x]+r[x])>>;
if (a<m) ans=max(ans,query(*x,a,b,c));
if (m<b) ans=max(ans,query(*x+,a,b,c));
return ans;
}
int main()
{
int test;
scanf("%d",&test);
while (test--)
{
scanf("%d%d",&n,&m);
dp=;
for (i=;i<=n;i++)
{
L[i]=;
p[i]=;
}
for (i=;i<=n;i++)
{
scanf("%d",&a);
link(a,i);
}
tot=;
deep=;stack[]=;
dfs(); build(,,n);
flag=;
for (i=;i<=n;i++)
{
scanf("%d",&v[i]);
change(,L[i]-,R[i],,v[i]);
}
flag=;
for (i=;i<=m;i++)
{
scanf("%d",&typ);
if (typ==)
{
scanf("%d%d",&a,&b);
change(,L[a]-,R[a],v[a],b);
v[a]=b;
}
else
{
scanf("%d",&a);
printf("%d\n",query(,L[a]-,L[a],v[a]));
}
}
}
}

hdu5390 tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. autowire异常的三个情况

    2010-3-11 16:06:00 net.sf.ehcache.config.ConfigurationFactory parseConfiguration 警告: No configuratio ...

  2. 低功耗蓝牙4.0BLE编程-nrf51822开发(10)-描述符

    特性中的属性有两种:属性值或描述符. 支持通知或指示的特性中默认有一个描述符:客户端特性配置描述符(Client Characteristic Configuration Descriptor,CCC ...

  3. C++内嵌汇编代码,简单文件加密

    #include <iostream> #include <fstream> using namespace std; int main(int argc, char* arg ...

  4. LightOj1366 - Pair of Touching Circles(求矩形内圆的对数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1366 题意:一个H*W的矩形,现在要放入两个外切的圆,问能放多少对这样的圆,其中圆心和 ...

  5. DHT(Distributed Hash Table) Translator

    DHT(Distributed Hash Table) Translator What is DHT? DHT is the real core of how GlusterFS aggregates ...

  6. Windows7下 配置 Apache + PHP + MySQL + Zend Studio配置

    相关软件下载: Apache                               版本:(httpd-2.2.25) PHP                                   ...

  7. Android 关于ExpandableListView控件setOnChildClickListener无效问题

    其实很简单,在适配器里面重写isChildSelectable的时候返回值切记为true,这样才能使得二级监听有响应. 其次注意继承的是BaseExpandableListAdapter

  8. axis2_1.6.2之构建web端和客户端 .

    参考资料: http://blog.csdn.net/apei830/article/details/5448897 axis2的官网 http://axis.apache.org/axis2/jav ...

  9. window.location.href和window.open的几种用法和区别

    使用js的同学一定知道js的location.href的作用是什么,但是在js中关于location.href的用法究竟有哪几种,究竟有哪些区别,估计很多人都不知道了. 一.location.href ...

  10. 第八篇 Replication:合并复制-How it works

    本篇文章是SQL Server Replication系列的第八篇,详细内容请参考原文. 在这一系列的前几篇你已经学习了如何在多服务器环境中配置合并复制.这一篇将介绍合并代理并解释它在复制过程中扮演的 ...