tree

Time Limit: 8000ms
Memory Limit: 262144KB

This problem will be judged on HDU. Original ID: 5390
64-bit integer IO format: %I64d      Java class name: Main

 
Given a rooted tree(node 1 is the root) with n nodes. The ithnode has a positive value vi at beginning.
We define the universal set S includes all nodes.
There are two types of Memphis's operation.
First, Memphis may change the value of one node. It's the first type operation:

0  u  v   (u∈S,0≤v≤109)

What's more, Memphis wants to know what's the maxinum of vu⊗vt(t∈path(u,root),⊗  means  xor) . It's the second type operation:

1  u   (u∈S)
 

Input

This problem has multi test cases(no more than 3). 
The first line contains a single integer T, which denotes the number of test cases.
For each test case,the first line contains two non-negative integer n,m(1≤n,m≤100000) - the number of nodes and operations.
The second line contains n−1 non-negative integer f2∼fn(fi<i) - the father of ithnode.
The third line contains n non-negative integer v1∼vn(0≤vi≤109) - the value of nodes at beginning.
Follow m lines describe each operation.

 

Output

For each test cases,for each second operation print a non-negative integer.

 

Sample Input

1
10 10
1 1 2 2 3 1 2 3 5
23512 460943 835901 491571 399045 97756 413210 800843 283274 106134
0 7 369164
0 7 296167
0 6 488033
0 7 187367
0 9 734984
1 6
0 5 329287
1 5
0 7 798656
1 10

Sample Output

766812
351647
431641

Source

 
解题:看到这位大神的代码后,看了一上午,终于看明白了,真的很神,离线做法,统一查询
 
先说说为什么要用线段树,因为我们发现是树,树任意两点之间只有唯一的路径,dfs序列,然后更新以后,为什么没有放到叶子结点?因为这是父节点,其代表区间内的任意节点到根的路径,必须经过父节点
 
所以当然是挂在区间上,而不是放到叶子,那么查询情况呢?我们是从根一直到叶子结点,一直路径上经过的结点,都把查询加入了,因为要查询的结点到根,也是必须经过这些结点的
 
那么字典树所谓何用,字典树是用来快速求解异或最大值的,利用的是贪心的思想,优先让高位变成1
 
好啦!代码已经灰常灰常灰常清晰了。。。。
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = ;
struct arc{
int to,next;
arc(int x = ,int y = -){
to = x;
next = y;
}
}e[maxn<<];
struct node{
int val,foo,op;
node(int x = ,int y = ,int z = ){
val = x;
foo = y;
op = z;
}
};
struct trie{
int tot,root,b[maxn*][],cnt[maxn*];
int newnode(){
b[tot][] = b[tot][] = cnt[tot] = ;
return tot++;
}
void init(){
tot = ;
root = newnode();
}
void insert(int val,int f,int root){
for(int i = ; i >= ; --i){
int bt = ((val>>i)&);
int &son = bt?b[root][]:b[root][];
if(!son) son = newnode();
root = son;
cnt[root] += f;
}
}
int query(int val,int root,int ret = ){
for(int i = ; i >= ; --i){
int bt = ((val>>i)&);
int son[] = {b[root][],b[root][]};
if((!son[] || !cnt[son[]]) && (!son[] || !cnt[son[]])) return ;
if(son[bt] && cnt[son[bt]]){
ret |= (<<i);
root = son[bt];
}else root = son[bt^];
}
return ret;
}
}T;
int head[maxn],L[maxn],R[maxn],ans[maxn],tot,times;
vector<node>g[maxn<<];
void add(int u,int v){
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void dfs(int u){
L[u] = ++times;
for(int i = head[u]; ~i; i = e[i].next) dfs(e[i].to);
R[u] = times;
}
void build(int L,int R,int v){
g[v].clear();
if(L == R) return;
int mid = (L + R)>>;
build(L,mid,v<<);
build(mid+,R,v<<|);
}
void update(int L,int R,int lt,int rt,int val,int f,int v){
if(lt <= L && rt >= R){
g[v].push_back(node(val,f,));
return;
}
int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,val,f,v<<);
if(rt > mid) update(mid+,R,lt,rt,val,f,v<<|);
}
void query(int L,int R,int p,int val,int id,int v){
g[v].push_back(node(val,id,));
if(L == R) return;
int mid = (L + R)>>;
if(p <= mid) query(L,mid,p,val,id,v<<);
else query(mid+,R,p,val,id,v<<|);
}
int val[maxn];
void query(int L,int R,int v){
T.init();
for(int i = ,sz = g[v].size(); i < sz; ++i){
if(g[v][i].op == ) T.insert(g[v][i].val,g[v][i].foo,T.root);
else ans[g[v][i].foo] = max(ans[g[v][i].foo],T.query(g[v][i].val,T.root));
}
if(L == R) return;
int mid = (L + R)>>;
query(L,mid,v<<);
query(mid+,R,v<<|);
}
int main(){
int kase,n,m,fa,op,w;
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&n,&m);
memset(head,-,sizeof head);
times = tot = ;
for(int i = ; i <= n; ++i){
scanf("%d",&fa);
add(fa,i);
}
dfs();
build(L[],R[],);
for(int i = ; i <= n; ++i){
scanf("%d",val+i);
update(,n,L[i],R[i],val[i],,);
}
for(int i = ; i <= m; ans[i++] = -){
scanf("%d",&op);
if(op){
scanf("%d",&fa);
query(,n,L[fa],val[fa],i,);
}else{
scanf("%d%d",&fa,&op);
update(,n,L[fa],R[fa],val[fa],-,);
val[fa] = op;
update(,n,L[fa],R[fa],op,,);
}
}
query(,n,);
for(int i = ; i <= m; ++i)
if(ans[i] != -) printf("%d\n",ans[i]);
}
return ;
}

2015 Multi-University Training Contest 8 hdu 5390 tree的更多相关文章

  1. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  2. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  3. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  7. 2015 Multi-University Training Contest 6 hdu 5362 Just A String

    Just A String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence

    Easy Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2015 Multi-University Training Contest 7 hdu 5378 Leader in Tree Land

    Leader in Tree Land Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

随机推荐

  1. 面试宝典之基本的C#面试问答

    下文是100个基本的C#面试问答清单.这些面试问题简单.直接了当,涵盖了C#最基本的概念,大部分和面向对象的概念相关.所以如果你在准备C#面试,我建议你必须掌握这100个基本的C#面试问答来复习你的C ...

  2. 一种基于Qt的可伸缩的全异步C/S架构server实现(五) 单层无中心集群

    五.单层无中心集群 对40万用户规模以内的server.使用星形的无中心连接是较为简便的实现方式.分布在各个物理server上的服务进程共同工作.每一个进程承担若干连接.为了实现这个功能,须要解决几个 ...

  3. WPF 基础到企业应用系列2——WPF前世今生

    1.开篇前言       非常多时候了解一项新技术的历史和趋势往往比这项技术的本身价值还要重要.WPF作为一项新技术(已经三年多了.或者应该叫老技术了).我们都有必要了解它的来龙去脉,尤其是公司的CT ...

  4. HDFS学习笔记(1)初探HDFS

    Hadoop分布式文件系统(Hadoop Distributed File System, HDFS) 分布式文件系统是一种同意文件通过网络在多台主机上分享的文件系统.可让多机器上的多用户分享文件和存 ...

  5. Cocos2d-x 3.0 红孩儿私家必修 - 第一章 初识Cocos2d-x 3.0project

    第一章    初识Cocos2d-x 3.0project Cocos2d-x 3.0出来了,听说与之前版本号相比修改较大 做为一个游戏开发人员.我们应该欢迎Cocos2d-x持续的更新和强大,Coc ...

  6. 开源ext2read代码走读之-在windows下怎样推断有几个硬盘设备?

    int get_ndisks() {     HANDLE hDevice;               // handle to the drive to be examined     int n ...

  7. HMM XSS检测

    HMM XSS检测 转自:http://www.freebuf.com/articles/web/133909.html 前言 上篇我们介绍了HMM的基本原理以及常见的基于参数的异常检测实现,这次我们 ...

  8. poj--3207--Ikki's Story IV - Panda's Trick(2-sat)

    Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %I64d ...

  9. 微软认证Hyper-V咨询工程师认证课程

    课程链接:http://www.microsoftvirtualacademy.com/colleges/hyper-V-Certificate STEP 1:完成课程链接内的认证课程. STEP 2 ...

  10. 个人对于React的Diff算法的一点疑问(待更新)

    本人对于Diff算法也并未做深入研究,只是大概的看过一些博文了解了些原理,但依然有了如下疑问 : 对于vdom所表示的对象中,若在该oldObj和newObj之间,发现一个元素节点所表示的子对象不见了 ...