分析:

这个题,还是蛮有趣的。考虑,如果l,r区间内的所有数出现奇数次,那么[l-1,r]的抑或和等于所得抑或和。

之后怎么维护呢,主席树维护区间抑或和,记得将每个点附上一个ull级别的随机数,之后抑或的结果冲突的概率就几乎没有了。

lca什么的,随便求。

剩下的,考虑二分答案,如果左区间的全为奇数个,就往右走,反之往左走。

附上代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
#define N 205005
#define lson l,m,tr[rt].ls
#define rson m+1,r,tr[rt].rs
#define ll unsigned long long
#define clear(rt) tr[rt].ls=tr[rt].rs=tr[rt].sum=0;
struct node
{
int ls,rs;
ll sum;
}tr[N*20];
struct no
{
int to,next;
}e[N<<1];
int dep[N],rot[N],fa[N],anc[N],siz[N],son[N],head[N],cnt,a[N],n,maxn,Q;ll val[N];
void add(int x,int y)
{
e[++cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt;
}
void insert(int x,int v,ll c,int l,int r,int &rt)
{
rt=++cnt;clear(rt);tr[rt].sum=tr[x].sum^c;
if(l==r)return;int m=(l+r)>>1;
if(m>=v)tr[rt].rs=tr[x].rs,insert(tr[x].ls,v,c,lson);
else tr[rt].ls=tr[x].ls,insert(tr[x].rs,v,c,rson);
}
void dfs1(int x,int from)
{
fa[x]=from,dep[x]=dep[from]+1,siz[x]=1;
insert(rot[from],a[x],val[a[x]],1,maxn,rot[x]);
for(int i=head[x];i!=-1;i=e[i].next)
{
int to1=e[i].to;
if(to1!=from)
{
dfs1(to1,x);
siz[x]+=siz[to1];
if(siz[to1]>siz[son[x]])son[x]=to1;
}
}
}
void dfs2(int x,int top)
{
anc[x]=top;if(son[x])dfs2(son[x],top);
for(int i=head[x];i!=-1;i=e[i].next)
{
int to1=e[i].to;
if(to1!=fa[x]&&to1!=son[x])dfs2(to1,to1);
}
}
int get_lca(int x,int y)
{
while(anc[x]!=anc[y])
{
if(dep[anc[x]]<dep[anc[y]])swap(x,y);
x=fa[anc[x]];
}
return dep[x]<dep[y]?x:y;
}
ll s[N];
int main()
{
int T;
scanf("%d",&T);
for(int i=1;i<=200001;i++)val[i]=(ll)rand()*rand()*rand(),s[i]=s[i-1]^val[i];
while(T--)
{
memset(son,0,sizeof(son));memset(head,-1,sizeof(head));cnt=0;maxn=0;
scanf("%d%d",&n,&Q);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),maxn=max(maxn,a[i]);
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
cnt=0;maxn++;
dfs1(1,0);dfs2(1,1);
while(Q--)
{
int x,y;scanf("%d%d",&x,&y);
int lca=get_lca(x,y),f=fa[lca];
x=rot[x],y=rot[y],lca=rot[lca],f=rot[f];
int l=1,r=maxn;
while(l<r)
{
int m=(l+r)>>1;
if((tr[tr[x].ls].sum^tr[tr[y].ls].sum^tr[tr[lca].ls].sum^tr[tr[f].ls].sum)==(s[m]^s[l-1]))
{
l=m+1,x=tr[x].rs,y=tr[y].rs,lca=tr[lca].rs,f=tr[f].rs;
}else
{
r=m,x=tr[x].ls,y=tr[y].ls,lca=tr[lca].ls,f=tr[f].ls;
}
}
printf("%d\n",l);
}
}
return 0;
}

  

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

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

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

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

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

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

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

  4. [Lydsy1805月赛] 对称数

    挺不错的一道数据结构题QWQ. 一开始发现这个题如果不看数据范围的话,妥妥的树上莫队啊23333,然鹅10组数据是不可能让你舒舒服服的树上莫队卡过的23333 于是想了想,这个题的模型就是,把u到v链 ...

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

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

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

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

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

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

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

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

  9. [LeetCode] Strobogrammatic Number 对称数

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

随机推荐

  1. 大数据时代,银行BI应用的方案探讨

    大数据被誉为21世纪发展创造的新动力,BI(商业智能)成为当下最热门的数据应用方案.据资料显示:当前中国大数据IT投资最高的为五个行业中,互联网最高.其次是电信.金融.政府和医疗.而在金融行业中,银行 ...

  2. 使用 jQuery Ajax 异步登录,并验证用户输入信息(maven)

    使用 jQuery Ajax 异步登录,并验证用户输入信息(maven) 本篇内容: (1)上一篇是使用同步的请求实现登录,并由 Servlet 决定登陆后下一步做哪些事情,本篇使用 jQuery A ...

  3. spring-quartz定时任务初探

    最近有关定时任务的需求还蛮多的,我这里呢用的是最简单的用法,后续了解更深层次的用法来优化目前的代码. 首先就是引入相关jar    quartz-1.6.4.jar  spring的jar就不说了 接 ...

  4. Scala包的使用

    package big.data.analyse.scala.classes /** * Created by zhen on 2018/9/15. */ object Packages { def ...

  5. SQLSERVER中的LOB页面简单研究

    SQLSERVER中的LOB页面简单研究 这篇文章和我另一篇文章是相辅相成的,在看<SQLSERVER2012 列存储索引的简单研究和测试>这篇文章之前希望大家先看一下这篇文章o(∩_∩) ...

  6. 如何使用 Azure PowerShell 在 Azure Marketplace 中查找 Windows VM 映像

    本主题介绍如何使用 Azure PowerShell 在 Azure Marketplace 中查找 VM 映像. 创建 Windows VM 时使用此信息来指定 Marketplace 映像. 确保 ...

  7. 如何监视 Azure 中的虚拟机

    通过收集.查看和分析诊断与日志数据,可以利用很多机会来监视 VM. 若要执行简单的 VM 监视,可以在 Azure 门户中使用 VM 的“概述”屏幕. 可以使用扩展在 VM 上配置诊断以收集更多指标数 ...

  8. ecplice 中添加JavaFX插件学习

    fxml文件使用SceneBuilder打开报错 解决方法:Window-->Preferences-->JavaFX-->browse 路径是可执行的JavaFX Scene Bu ...

  9. [Spark Core] Spark 核心组件

    0. 说明 [Spark 核心组件示意图] 1. RDD resilient distributed dataset , 弹性数据集 轻量级的数据集合,逻辑上的集合.等价于 list 没有携带数据. ...

  10. Mysql学习---面试基础知识点总结

    drop.truncate. delete区别 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 ...