BZOJ_5338_ [TJOI2018]xor_可持久化trie
BZOJ_5338_ [TJOI2018]xor_可持久化trie
Description
Input
Output
Sample Input
1 2 3
1 5 1
2 3 1
2 4 2
Sample Output
【样例解释】
将点1,2染黑就能获得最大收益。
HINT
2017.9.12新加数据一组 By GXZlegend
博客里没有几道可持久化trie的题,还是更一篇吧。
同时要求子树和两点路径上的信息,
只能用两个序维护一下,然后可持久化一下随便搞搞。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define N 200050
int head[N],to[N],nxt[N],val[N],cnt,n,m,be[N],ed[N],tot,root[N],t[N*33],sanae,ch[N*33][2],son[N],siz[N],fa[N],dep[N],top[N];
int t2[N*33],ch2[N*33][2],dfn[N],marisa,reimu,root2[N];
inline void add(int u,int v) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
void update(int x,int c,int &y,int q) {
y=++sanae; int p=y; t[p]=t[q]+c;
int i;
for(i=30;i>=0;i--) {
int k=(x>>i)&1;
ch[p][k]=++sanae; ch[p][!k]=ch[q][!k];
p=ch[p][k]; q=ch[q][k]; t[p]=t[q]+c;
}
}
void upd(int x,int c,int &y,int q) {
y=++reimu; int p=y; t2[p]=t2[q]+c;
// printf("upd---%d %d %d %d\n",p,q,t2[p],t2[q]);
int i;
for(i=30;i>=0;i--) {
int k=(x>>i)&1;
ch2[p][k]=++reimu; ch2[p][!k]=ch2[q][!k];
p=ch2[p][k]; q=ch2[q][k]; t2[p]=t2[q]+c;
}
}
void dfs(int x,int y) {
dfn[x]=++marisa; upd(val[x],1,root2[marisa],root2[marisa-1]);
// printf("dfs---%d %d %d %d\n",root2[marisa],root2[marisa-1],t2[root2[marisa]],t2[root2[marisa-1]]);
// printf("%d %d %d %d\n",t2[root2[1]],t2[root2[2]],t2[root2[3]],t2[root2[4]]);
int i; be[x]=++tot; update(val[x],1,root[tot],root[tot-1]); siz[x]=1; fa[x]=y; dep[x]=dep[y]+1;
for(i=head[x];i;i=nxt[i]) {
if(to[i]!=y) {
dfs(to[i],x); siz[x]+=siz[to[i]]; if(siz[son[x]]<siz[to[i]]) son[x]=to[i];
}
}
ed[x]=++tot; update(val[x],-1,root[tot],root[tot-1]);
}
void dfs2(int x,int t) {
top[x]=t;
if(son[x]) dfs2(son[x],t);
int i;
for(i=head[x];i;i=nxt[i])if(to[i]!=fa[x]&&to[i]!=son[x]) dfs2(to[i],to[i]);
}
int lca(int x,int y) {
while(top[x]!=top[y]) {
if(dep[top[x]]>dep[top[y]]) swap(x,y);
y=fa[top[y]];
}
return dep[x]<dep[y]?x:y;
}
int solve1(int x,int y,int v) {
// printf("%d %d %d %d\n",x,y,t2[x],t2[y]);
int i,re=0;
for(i=30;i>=0;i--) {
int k=!((v>>i)&1);
if(t2[ch2[y][k]]-t2[ch2[x][k]]) {
re+=(1<<i); x=ch2[x][k]; y=ch2[y][k];
}else x=ch2[x][!k],y=ch2[y][!k];
}
return re;
}
int solve2(int x,int y,int z,int w,int v) {
int i,re=0;
for(i=30;i>=0;i--) {
int k=!((v>>i)&1);
if(t[ch[x][k]]+t[ch[y][k]]-t[ch[z][k]]-t[ch[w][k]]>0) re+=(1<<i),x=ch[x][k],y=ch[y][k],z=ch[z][k],w=ch[w][k];
else x=ch[x][!k],y=ch[y][!k],z=ch[z][!k],w=ch[w][!k];
}
return re;
}
int main() {
scanf("%d%d",&n,&m);
int i,x,y,opt,z;
for(i=1;i<=n;i++) scanf("%d",&val[i]);
for(i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x);
dfs(1,0); dfs2(1,1);
// printf("%d %d %d %d\n",t2[root2[1]],t2[root2[2]],t2[root2[3]],t2[root2[4]]);
for(i=1;i<=m;i++) {
scanf("%d%d%d",&opt,&x,&y);
if(opt==1) {
// printf("%d\n",root2[dfn[x]-1]);
printf("%d\n",solve1(root2[dfn[x]-1],root2[dfn[x]+siz[x]-1],y));
}else {
scanf("%d",&z);
int l=lca(x,y);
printf("%d\n",solve2(root[be[x]],root[be[y]],root[be[l]],root[be[fa[l]]],z));
}
}
}
BZOJ_5338_ [TJOI2018]xor_可持久化trie的更多相关文章
- BZOJ.5338.[TJOI2018]xor(可持久化Trie)
BZOJ LOJ 洛谷 惊了,18年了还有省选出模板题吗= = 做这题就是练模板的,我就知道我忘的差不多了 询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分. 注意求询问二的 ...
- [BZOJ5338][TJOI2018]xor(可持久化Trie)
可持久化Trie模板题. 建两种可持久化Trie,每个点两棵,一棵对DFS求前缀和,一棵对祖先求前缀和. 或者树剖,不好写多少还多个log. #include<cstdio> #inclu ...
- BZOJ 5338: [TJOI2018]xor 可持久化trie+dfs序
强行把序列问题放树上,好无聊啊~ code: #include <bits/stdc++.h> #define N 200005 #define setIO(s) freopen(s&qu ...
- BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】
题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...
- 可持久化trie(BZOJ5338: [TJOI2018]xor)
题面 BZOJ Sol 显然是要维护一个区域的 \(trie\) 树,然后贪心 区间 \(trie\) 树??? 可持久化 \(trie\) 树??? 直接参考主席树表示出区间的方法建立 \(trie ...
- 洛谷P4592 [TJOI2018]异或 【可持久化trie树】
题目链接 BZOJ4592 题解 可持久化trie树裸题 写完就A了 #include<algorithm> #include<iostream> #include<cs ...
- [TJOI2018] Xor 异或 (可持久化Trie,树链剖分)
题目描述 现在有一颗以 1 为根节点的由 n 个节点组成的树,树上每个节点上都有一个权值 \(v_i\).现在有 Q 次操作,操作如下: 1 x y :查询节点 x 的子树中与 y 异或结果的最大值. ...
- HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Su ...
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
随机推荐
- View的滚动原理简单解析
一直对View的滚动了解的不深,说明确了吧也能说出个所以然来,所以我就花了点时间做了一个小小的总结,言归正传,view的滑动分为下面三种: 1)View本身不滚动,指滚动View的内容,这也是View ...
- 《textanalytics》课程简单总结(2):topic mining
coursera上的公开课<https://www.coursera.org/course/textanalytics>系列,讲的很不错哦. 1."term as topic&q ...
- Pandoc PDF 中文
最近终于又决定(^_^)使用reStructuredText写文档了,输出PDF时的中文问题必须要解决下. 安装环境 sudo apt install texlive texlive-latex-ex ...
- PHP面向对象——构造函数、析构函数
在PHP中构造函数和析构函数是固定的,如下: // 构造函数 function __construct([argument1,argument2,...,argumentN]){ /* Class i ...
- eolinker开源版接口管理
eolinker开源版接口管理 想找一个API接口管理的软件,为了安全性和扩展性考虑,希望是开源的,而且可以在内网独立部署.网上翻找了资料,经过一份比对之后,最终采用eolinker.过去有使用过RA ...
- Unable to save settings: Failed to save settings. Please restart PyCharm解决
将工程的.ideas目录删掉,重启pycharm即可.
- ejabberd日志分析客户端登录流程
通过ejabberd的日志,整理了下客户端登录流程. 1. 通过TCP连接5222端口的流程: (1) 客户端向服务器发送stream流 <stream:stream to="nba. ...
- linux memcached php 整合
http://blog.csdn.net/liruxing1715/article/details/8269563
- Java带标签的break 和带标签的continue
最开始没有学习java 学习的是C语言然后工作开始用java,但当时并没有仔细看过java的书籍,也是大致一翻就看跟C语言很像,了解了基本语法就没有深究了,今天看书开始发现之前没有了解过的语法 带标签 ...
- 解决UICollectionView的Cell复用引起的布局混乱问题
解决UICollectionView的Cell复用引起的布局混乱问题 问题复现.gif 查了一下度娘发现没有好的解决办法,于是发动自己的聪明才智,终于找到如下解决办法(充分证明了自己动手丰衣足食啊