挺不错的一道数据结构题QWQ。

一开始发现这个题如果不看数据范围的话,妥妥的树上莫队啊23333,然鹅10组数据是不可能让你舒舒服服的树上莫队卡过的23333

于是想了想,这个题的模型就是,把u到v链上的权值出现奇偶次的01串搞出来,然后第一个0的位置就是所求。。。。。

但是这个01串并不是很好搞,因为每一位都得异或啊。。。。这样复杂度就要乘上一个 200000/32了(bitset压位),还不如树上莫队呢QWQ

不过有一种套路,叫做hash异或,我们就给每一个权值随机一个unsined long long范围的hash值,大概率保证询问涉及的子集的异或和不为0(这个主要看人品了。。。因为总的来说肯定会有很多子集的异或和为0,但是因为子集总数太过庞大,询问涉及的只是小部分,所以出错概率还是很小的2333)

这样,如果一个区间所有数都出现奇数次,那么区间的hash异或起来就和 路径权值线段树在这个区间异或起来的值一样了,直接在主席树上二分即可。。。

(注意lca是要被算上的,但是如果直接用到根前缀的两个主席树异或的话lca是会被消去的)

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int maxn=200005;
#define mid (l+r>>1)
int T,n,a[maxn],m,to[maxn*2],ne[maxn*2],dep[maxn],le,w,A,B,ans;
int hd[maxn],num,siz[maxn],son[maxn],cl[maxn],f[maxn],ri,lca;
inline void add(int x,int y){ to[++num]=y,ne[num]=hd[x],hd[x]=num;}
ll val[maxn],Xor[maxn],now;
struct node{
ll sum;
node *lc,*rc;
}nil[maxn*37],*rot[maxn],*cnt; inline void init(){
nil->sum=0;
nil->lc=nil->rc=rot[0]=cnt=nil;
fill(hd+1,hd+n+1,0),num=0;
memset(son,0,sizeof(son));
} node *update(node *u,int l,int r){
node *ret=++cnt;
*ret=*u,ret->sum^=val[le];
if(l==r) return ret; if(le<=mid) ret->lc=update(ret->lc,l,mid);
else ret->rc=update(ret->rc,mid+1,r); return ret;
} void query(node *u,int l,int r){
if(l>=le&&r<=ri){ now^=u->sum; return;}
if(le<=mid) query(u->lc,l,mid);
if(ri>mid) query(u->rc,mid+1,r);
} void Fdfs(int x,int fa){
f[x]=fa,siz[x]=1,dep[x]=dep[fa]+1;
le=a[x],rot[x]=update(rot[fa],1,200001); for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
Fdfs(to[i],x),siz[x]+=siz[to[i]];
if(!son[x]||siz[to[i]]>siz[son[x]]) son[x]=to[i];
}
} void Sdfs(int x,int tp){
cl[x]=tp;
if(!son[x]) return; Sdfs(son[x],tp);
for(int i=hd[x];i;i=ne[i]) if(to[i]!=f[x]&&to[i]!=son[x]) Sdfs(to[i],to[i]);
} int LCA(int x,int y){
while(cl[x]!=cl[y]){
if(dep[cl[x]]>dep[cl[y]]) x=f[cl[x]];
else y=f[cl[y]];
}
return dep[x]<dep[y]?x:y;
} void query(node *u,node *v,int l,int r){
if(l==r){ ans=l; return;} if((u->lc->sum^v->lc->sum^((a[lca]>=l&&a[lca]<=mid)?val[a[lca]]:0))==(Xor[mid]^Xor[l-1])) query(u->rc,v->rc,mid+1,r);
else query(u->lc,v->lc,l,mid);
} inline void solve(){
Fdfs(1,0),Sdfs(1,1); while(m--){
scanf("%d%d",&A,&B),lca=LCA(A,B);
query(rot[A],rot[B],1,200001);
printf("%d\n",ans);
}
} int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout); val[1]=1;
for(int i=2;i<=200001;i++) val[i]=val[i-1]*233ll;
for(int i=1;i<=200001;i++) Xor[i]=val[i]^Xor[i-1]; scanf("%d",&T);
while(T--){
init(),scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i);
int uu,vv;
for(int i=1;i<n;i++) scanf("%d%d",&uu,&vv),add(uu,vv),add(vv,uu); solve();
} return 0;
}

[Lydsy1805月赛] 对称数的更多相关文章

  1. 【主席树上二分】bzoj5361: [Lydsy1805月赛]对称数

    随机化选讲例题 题目大意 小 Q 认为,偶数具有对称美,而奇数则没有.给定一棵 n 个点的树,任意两点之间有且仅有一条直接或间接路径.这些点编号依次为 1 到 n,其中编号为 i 的点上有一个正整数 ...

  2. BZOJ5361[Lydsy1805月赛]对称数——主席树+随机化

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5361 好神的一道题啊! 容易看出来是要用维护权值的数据结构,因此树链剖分首先pass掉. ...

  3. [Lydsy1805月赛]对称数 BZOJ5361

    分析: 这个题,还是蛮有趣的.考虑,如果l,r区间内的所有数出现奇数次,那么[l-1,r]的抑或和等于所得抑或和. 之后怎么维护呢,主席树维护区间抑或和,记得将每个点附上一个ull级别的随机数,之后抑 ...

  4. [BZOJ5361][Lydsy1805月赛]对称数

    bzoj Description 给你一棵树,每个点有一个编号\(a_i\).\(Q\)组询问,每次问一条路径上最小的出现了偶数次的编号是多少(包括零次). 多组数据,\(T\le10,n,Q,a_i ...

  5. [Bzoj5358][Lydsy1805月赛]口算训练(预处理+动态开点线段树)

    5358: [Lydsy1805月赛]口算训练 Time Limit: 5 Sec  Memory Limit: 512 MBSubmit: 318  Solved: 105[Submit][Stat ...

  6. [LeetCode] Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  7. [LeetCode] Strobogrammatic Number II 对称数之二

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  8. [LeetCode] Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  9. [BZOJ5361]/[HDU6291]对称数

    [BZOJ5361]/[HDU6291]对称数 题目大意: 一个\(n(n\le2\times10^5)\)个结点的树,每个结点有一个权值\(a_i(a_i\le2\times10^5)\),\(m( ...

随机推荐

  1. 【BZOJ 4007】[JLOI2015]战争调度 DP+搜索+状压

    又是一道思路清新的小清晰. 观察题目,如果我们确定了平民或者贵族的任意一方,我们便可以贪心的求出另一方,至此20分:我们发现层数十分小,那么我们就也是状压层数,用lca转移,线性dp,至此50分(好像 ...

  2. Substrings Sort string 基本操作

    You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...

  3. 使用babel把es6代码转成es5代码

    第一步:创建一个web项目 使用命令:npm init 这个命令的目的是生成package.json. 执行第二步中的命令后生成的package.json的文件的内容是: { "name&q ...

  4. Bash 实例,第二部分

    我们先看一下处理命令行自变量的简单技巧,然后再看看 bash 基本编程结构. 接收自变量 在 介绍性文章 中的样本程序中,我们使用环境变量 "$1" 来引用第一个命令行自变量.类似 ...

  5. VS2010 VC Project的default Include设置

    在IDE中,打开View->Other Windows->Property Manager.展开树形后,你会发现一个名为“Microsoft.Cpp.Win32.user”的项目(如下图) ...

  6. OpenCV+Java环境搭建

    1.官网地址http://opencv.org/ 1.首先下载OpenCV2.4.6,下载的时候,选择windows版的.然后安装 2.其实安装的过程就是解压的过程,并没有什么安装向导之类的,安装完成 ...

  7. idea真不习惯啊

    http://blog.csdn.net/z69183787/article/details/41416189

  8. openstack 问题澄清

    1. neutron中plugin与agent是一一对应的吗? 在不使用ml2时,plugin与agent一一对应,如ovs-plugin与ovs-agent:当使用ml2 plugin时,该plug ...

  9. openstack中region、az、host aggregate、cell 概念

    1. region 更像是一个地理上的概念,每个region有自己独立的endpoint,regions之间完全隔离,但是多个regions之间共享同一个keystone和dashboard.(注:目 ...

  10. POJ 3617 Best Cow Line (模拟)

    题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...