hdu5390 tree
先求出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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- App Previews操作经验
App Previews操作经验 http://www.cocoachina.com/ios/20140924/9741.html http://www.cocoachina.com/bbs/read ...
- Android源码剖析之Framework层实战版(Ams管理Activity启动)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 讲到实战,就不得不拿两个例子来说明,本篇想拿的是应用最广泛的两个:Ams和Wms,一个管理activ ...
- Mysql 按行dump出数据
mysqldump -u${user} -p${passwd} --skip-extended-insert --database ${dbname} --table ${tablename} > ...
- EF6 CodeFirst 启用Migration,常用命令
Enable-Migrations –EnableAutomaticMigrationsAdd-Migration [MigrationName] [-Force]Update-Database –T ...
- 用angularJS实现Bootstrap的“手风琴”
主页面代码(发现Bootstrap官网上手风琴的实例样式有问题,在这里依然使用3.0.~版本) <!DOCTYPE html> <html ng-app="ct" ...
- RTSP协议详解
RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议.RTSP对流媒体提 ...
- WPFFontCache_v0400.exe CPU使用率过高的问题
最近的电脑很慢 CPU超过50%了 任务管理器显示是WPFFontCache_v0400.exe 的问题 每次强制终止后不就又重新启动很是麻烦, 在MSDN中找到了解决办法: 禁用Windows Pr ...
- LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...
- LightOj1074 - Extended Traffic(SPFA最短路)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每个城市有一个拥堵值a[i],m条单向路u到v,从u到v所需时 ...
- SQL Server 2008 R2,显示SQL语句执行窗口。 编辑前200行,可以执行SQL语句