[Lydsy1805月赛] 对称数
挺不错的一道数据结构题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月赛] 对称数的更多相关文章
- 【主席树上二分】bzoj5361: [Lydsy1805月赛]对称数
随机化选讲例题 题目大意 小 Q 认为,偶数具有对称美,而奇数则没有.给定一棵 n 个点的树,任意两点之间有且仅有一条直接或间接路径.这些点编号依次为 1 到 n,其中编号为 i 的点上有一个正整数 ...
- BZOJ5361[Lydsy1805月赛]对称数——主席树+随机化
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5361 好神的一道题啊! 容易看出来是要用维护权值的数据结构,因此树链剖分首先pass掉. ...
- [Lydsy1805月赛]对称数 BZOJ5361
分析: 这个题,还是蛮有趣的.考虑,如果l,r区间内的所有数出现奇数次,那么[l-1,r]的抑或和等于所得抑或和. 之后怎么维护呢,主席树维护区间抑或和,记得将每个点附上一个ull级别的随机数,之后抑 ...
- [BZOJ5361][Lydsy1805月赛]对称数
bzoj Description 给你一棵树,每个点有一个编号\(a_i\).\(Q\)组询问,每次问一条路径上最小的出现了偶数次的编号是多少(包括零次). 多组数据,\(T\le10,n,Q,a_i ...
- [Bzoj5358][Lydsy1805月赛]口算训练(预处理+动态开点线段树)
5358: [Lydsy1805月赛]口算训练 Time Limit: 5 Sec Memory Limit: 512 MBSubmit: 318 Solved: 105[Submit][Stat ...
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [BZOJ5361]/[HDU6291]对称数
[BZOJ5361]/[HDU6291]对称数 题目大意: 一个\(n(n\le2\times10^5)\)个结点的树,每个结点有一个权值\(a_i(a_i\le2\times10^5)\),\(m( ...
随机推荐
- pb_ds
#include<ext/pb_ds/priority_queue.hpp>#define ll long long#define pa pair<ll,int>using n ...
- 7月17号day9总结
今天学习过程和小结 今天学习了如何使用idea操作hdfs. public class HDFSTest { Configuration configuration; FileSystem ...
- JAX-WS 注解
一.概述 “基于 XML 的 Web Service 的 Java API”(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注 ...
- iebackground+icon图标兼容
<!DOCTYPE > <html> <head> <title>zepto</title> <meta name="nam ...
- [目前未找到题目]扩展KMP模板
procedure build_next; begin lena:=length(a);lenb:=length(b); next[]:=lenb;next[]:=lenb-; to lenb- ] ...
- centos6.4 nginx+mysql+php整合phpmyadmin出错解决方案
今天在centos下整合phpmyadmin出错,错误提示如下: Error during session start; please check your PHP and/or webserver ...
- [bzoj3306]树——树上倍增+dfs序+线段树
Brief Description 您需要写一种数据结构,支持: 更改一个点的点权 求一个子树的最小点权 换根 Algorithm Design 我们先忽略第三个要求. 看到要求子树的最小点权,我们想 ...
- Swift中的类型属性(静态变量)
http://blog.haohtml.com/archives/15098 Swift中的类型属性(静态变量) Posted on 2014/06/13 类型属性语法 在 C 或 Objective ...
- HDU3336(KMP + dp)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU3664 Permutation Counting
Permutation Counting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...