可持久化trie。考场上我脑补了一个trie树合并也A了

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, k, m, dfnl[100005], dfnr[100005], idx, rot[100005], v[100005], uu, vv, cnt;
int hea[100005], fan[100005], tot;
struct Edge{
int too, nxt, val;
}edge[200005];
struct Node{
int l, r, sum;
}nd[3333333];
void add_edge(int fro, int too){
edge[++cnt].nxt = hea[fro];
edge[cnt].too = too;
hea[fro] = cnt;
}
void dfs(int x, int f){
dfnl[x] = ++idx;
fan[idx] = x;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f) dfs(t, x);
}
dfnr[x] = idx;
}
int update(int pre, int x, int p){
int re=++tot;
nd[re] = nd[pre];
nd[re].sum++;
if(p<0)
return re;
int tmp=(x>>p)&1;
if(tmp) nd[re].r = update(nd[pre].r, x, p-1);
else nd[re].l = update(nd[pre].l, x, p-1);
return re;
}
int query(int pre, int now, int x, int p){
if(p<0) return 0;
int tmp=(x>>p)&1;
if(tmp){
int sum=nd[nd[now].l].sum-nd[nd[pre].l].sum;
if(sum)
return query(nd[pre].l, nd[now].l, x, p-1)+(1<<p);
else
return query(nd[pre].r, nd[now].r, x, p-1);
}
else{
int sum=nd[nd[now].r].sum-nd[nd[pre].r].sum;
if(sum)
return query(nd[pre].r, nd[now].r, x, p-1)+(1<<p);
else
return query(nd[pre].l, nd[now].l, x, p-1);
}
}
int main(){
while(scanf("%d %d %d", &n, &k, &m)!=EOF){
memset(hea, 0, sizeof(hea));
idx = tot = cnt = 0;
for(int i=1; i<=n; i++)
scanf("%d", &v[i]);
for(int i=1; i<n; i++){
scanf("%d %d", &uu, &vv);
add_edge(uu, vv);
add_edge(vv, uu);
}
dfs(k, 0);
for(int i=1; i<=n; i++)
rot[i] = update(rot[i-1], v[fan[i]], 30);
while(m--){
scanf("%d %d", &uu, &vv);
printf("%d\n", query(rot[dfnl[uu]-1], rot[dfnr[uu]], vv, 30));
}
}
return 0;
}

最新文章

  1. 僵尸进程的产生和避免,如何kill杀掉linux系统中的僵尸defunct进程
  2. cocos2dx一个场景添加多个层
  3. Codeforces Round #268 (Div. 1) A. 24 Game 构造
  4. zz-rtl8188eu的linux-usb-wifi调试及驱动编译150210
  5. SAP HANA SLT 将Oracle表 数据同步到HANA数据库
  6. php:修改NetBeans默认字体
  7. HDU 1728 逃离迷宫(DFS)
  8. jetty和tomcat的区别
  9. TCP/IP概述
  10. Java虚拟机学习笔记——JVM垃圾回收机制
  11. windows下更新node环境
  12. Vue + Element UI 实现权限管理系统
  13. 用JavaScript写的动态表格
  14. Notification 浏览器的消息推送
  15. python性能测试大致计划
  16. js 固定表头及固定列的js
  17. HTTP协议详解之请求篇
  18. Enable Notepad++ 666 support both SCLEX_FORTRAN and SCLEX_F77
  19. 9.27下午考试(Nescaf&#233; 29杯模拟赛)
  20. 【转】LDA-linear discriminant analysis

热门文章

  1. 从零开始的全栈工程师——html篇1.6
  2. 根据要求完成表单以及使用servlet处理表单 任务要求 掌握Servlet输出表单和接收表单数据(多值组件的读取)。
  3. 重置 file input
  4. safenet 超级狗 加密狗
  5. 签证-L1/L2
  6. javascript面向对象继承和原型
  7. System.FormatException: GUID 应包含带 4 个短划线的 32 位数(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。解决办法
  8. python_输出100:200内的素数
  9. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第三节
  10. java基础编程——获取栈中的最小元素