[BZOJ5361]/[HDU6291]对称数

题目大意:

一个\(n(n\le2\times10^5)\)个结点的树,每个结点有一个权值\(a_i(a_i\le2\times10^5)\),\(m(m\le2\times10^5)\)次询问,每次询问\((u,v)\)路径上最小的出现偶数次的数。

思路:

对每个权值随机一个unsigned long long作为一个新的权值,树上主席树维护区间异或和。

询问时在主席树上二分即可。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef unsigned long long uint64;
const int N=2e5+1,logN=40;
int a[N],v[N],anc[N][logN],dep[N],mex;
uint64 w[N],hsum[N];
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
inline int lg2(const float &x) {
return ((unsigned&)x>>23&255)-127;
}
class FotileTree {
#define mid ((b+e)>>1)
private:
struct Node {
uint64 val;
int left,right;
};
Node node[N*logN];
int sz,new_node(const int &p) {
node[++sz]=node[p];
return sz;
}
public:
void reset() {
sz=0;
}
int root[N];
void insert(int &p,const int &b,const int &e,const int &x) {
p=new_node(p);
node[p].val^=w[v[x]];
if(b==e) return;
if(x<=mid) insert(node[p].left,b,mid,x);
if(x>mid) insert(node[p].right,mid+1,e,x);
}
int query(const int &p,const int &q,const int &r,const int &s,const int &b,const int &e) const {
if((node[p].val^node[q].val^node[r].val^node[s].val)==(hsum[e]^hsum[b-1])) return mex;
if(b==e) return v[b];
const int tmp=query(node[p].left,node[q].left,node[r].left,node[s].left,b,mid);
if(tmp==mex) return query(node[p].right,node[q].right,node[r].right,node[s].right,mid+1,e);
return tmp;
}
#undef mid
};
FotileTree t;
void dfs(const int &x,const int &par) {
anc[x][0]=par;
dep[x]=dep[par]+1;
for(register int i=1;i<=lg2(dep[x]);i++) {
anc[x][i]=anc[anc[x][i-1]][i-1];
}
const int p=std::lower_bound(&v[1],&v[v[0]]+1,a[x])-v;
t.insert(t.root[x]=t.root[par],1,v[0],p);
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs(y,x);
}
}
inline int get_lca(int x,int y) {
if(dep[x]<dep[y]) std::swap(x,y);
for(register int i=lg2(dep[x]-dep[y]);i>=0;i--) {
if(dep[anc[x][i]]>=dep[y]) x=anc[x][i];
}
if(x==y) return x;
for(register int i=lg2(dep[x]);i>=0;i--) {
if(anc[x][i]!=anc[y][i]) {
x=anc[x][i];
y=anc[y][i];
}
}
return anc[x][0];
}
inline int query(const int &x,const int &y) {
const int lca=get_lca(x,y);
return std::min(t.query(t.root[x],t.root[y],t.root[lca],t.root[anc[lca][0]],1,v[0]),mex);
}
int main() {
srand(19961993);
for(register int T=getint();T;T--) {
memset(anc,0,sizeof anc);
const int n=getint(),m=getint();
for(register int i=1;i<=n;i++) {
v[i]=a[i]=getint();
while(w[a[i]]==0) {
w[a[i]]=(uint64)rand()*rand()*rand();
}
}
std::sort(&v[1],&v[n]+1);
v[0]=std::unique(&v[1],&v[n]+1)-v-1;
for(register int i=mex=1;i<=v[0];i++) {
hsum[i]=hsum[i-1]^w[v[i]];
if(v[i]==mex) mex++;
}
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
dfs(1,0);
for(register int i=0;i<m;i++) {
printf("%d\n",query(getint(),getint()));
}
t.reset();
for(register int i=1;i<=n;i++) {
e[i].clear();
}
}
return 0;
}

[BZOJ5361]/[HDU6291]对称数的更多相关文章

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

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

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

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

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

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

  4. [LeetCode] Strobogrammatic Number 对称数

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

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

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

  6. [LeetCode] 246. Strobogrammatic Number 对称数

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

  7. [LeetCode] 247. Strobogrammatic Number II 对称数II

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

  8. [LeetCode] 248. Strobogrammatic Number III 对称数III

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

  9. Python找对称数——纪念第一次自主编写代码

    2021-01-17 题目: [问题描述]已知10个四位数输出所有对称数及个数 n,例如1221.2332都是对称数[输入形式]10个四位数,以空格分隔开[输出形式]输入的四位数中的所有对称数,对称数 ...

随机推荐

  1. idea中tomcat乱码问题解决

    在idea中经常遇到jsp的乱码问题,原因是编码不是UTF-8的问题,这次来彻底解决idea的编码问题 首先设置idea编辑器的编码: File-Setting设置如下 然后配置tomcat的编码问题 ...

  2. 工作中常用的Git操作--------(一)

    今天主要记录一下平常工作当中使用的git操作: 1.git的安装这里省略: 2.git的操作指令: 在项目开发中,经常是拉去经理已经搭建好的一个项目,也就是给我们一个git地址.比如:http://g ...

  3. wget下载整个网站或特定目录

    下载整个网站或特定目录 wget -c -k -r -np -p http://www.yoursite.com/path -c, –continue 断点下载 -k, –convert-links ...

  4. python开发规范(转载)

    转载自http://www.cnblogs.com/wangcp-2014/p/4838952.html 目录 代码布局 1.1 缩进 1.2 表达式和语句中的空格 1.3 行的最大长度 1.4 空行 ...

  5. go语言基本介绍

    Golang发展历史 1. 诞生历史a. 诞生与2006年1月2号下午15点4分5秒b. 2009发布并正式开源c. 2012年第一个正式版本Go 1.0发布d. 截至到2017年8月24号Go 1. ...

  6. offset宏的讲解【转】

    转自:http://blog.csdn.net/tigerjibo/article/details/8299584 1.offset宏讲解 #define offsetof(TYPE, MEMBER) ...

  7. Nginx部署部分https与部分http【转】

    转自 Nginx部署部分https与部分http - na_tion的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/na_tion/article/details/ ...

  8. pip安装模块时:error: command 'gcc' failed with exit status 1

    用安装python模块出现error: command 'gcc' failed with exit status 1 问题: gcc编译缺少模块 解决方法: yum install gcc libf ...

  9. Git & GitHub 学习

    学习资料: Git版本控制软件结合GitHub从入门到精通常用命令学习手册:http://www.ihref.com/read-16369.html 官方中文手册:http://git-scm.com ...

  10. C# 6.0 新特性 (一)

    概述 尽管 C# 6.0 尚未完成,但现在这些功能正处于接近完成的关键时刻.自 2014 年 5 月发布文章“C# 6.0 语言预览版”(msdn.microsoft.com/magazine/dn6 ...