http://www.lydsy.com/JudgeOnline/problem.php?id=3123

启发式合并主席树

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define N 80001 int n,testcase,m,T;
int val[N];
int front[N],to[N<<],nxt[N<<],tot; int has[N],all; int fa[N][],dep[N],lim; int F[N],siz[N]; int id,root[N]; struct node
{
int lc,rc,cnt;
}tr[N*]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
} int find(int i) { return F[i]==i ? i : F[i]=find(F[i]); } void unionn(int u,int v)
{
u=find(u); v=find(v);
siz[u]+=siz[v];
F[v]=u;
} void init()
{
read(testcase);
read(n); read(m); read(T);
lim=log(n)/log();
for(int i=;i<=n;++i)
{
read(val[i]);
F[i]=i;
siz[i]=;
}
int u,v;
for(int i=;i<=m;++i)
{
read(u); read(v);
add(u,v);
unionn(u,v);
}
}
void insert(int &x,int y,int l,int r,int pos)
{
if(!x) x=++id;
tr[x].cnt=tr[y].cnt+;
if(l==r) return;
int mid=l+r>>;
if(pos<=mid)
{
tr[x].rc=tr[y].rc;
insert(tr[x].lc,tr[y].lc,l,mid,pos);
}
else
{
tr[x].lc=tr[y].lc;
insert(tr[x].rc,tr[y].rc,mid+,r,pos);
}
} int query(int u,int v,int lca,int flca,int l,int r,int k)
{
if(l==r) return l;
int mid=l+r>>,tmp=tr[tr[u].lc].cnt+tr[tr[v].lc].cnt-tr[tr[lca].lc].cnt-tr[tr[flca].lc].cnt;
if(k<=tmp) return query(tr[u].lc,tr[v].lc,tr[lca].lc,tr[flca].lc,l,mid,k);
return query(tr[u].rc,tr[v].rc,tr[lca].rc,tr[flca].rc,mid+,r,k-tmp);
} void discrete()
{
for(int i=;i<=n;++i) has[i]=val[i];
sort(has+,has+n+);
all=unique(has+,has+n+)-has-;
for(int i=;i<=n;++i) val[i]=lower_bound(has+,has+all+,val[i])-has;
} void dfs(int x)
{
dep[x]=dep[fa[x][]]+;
insert(root[x],root[fa[x][]],,all,val[x]);
for(int i=;i<=lim;++i) fa[x][i]=fa[fa[x][i-]][i-];
for(int i=front[x];i;i=nxt[i])
if(to[i]!=fa[x][])
{
fa[to[i]][]=x;
dfs(to[i]);
}
} int get_lca(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);
int x=dep[u]-dep[v];
for(int i=lim;i>=;--i)
if(x&(<<i)) u=fa[u][i];
if(u==v) return u;
for(int i=lim;i>=;--i)
if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][];
} void solve()
{
for(int i=;i<=n;++i)
if(!fa[i][]) dfs(i);
char ch[];
int u,v,k,last=;
int lca,fu,fv;
while(T--)
{
scanf("%s",ch);
if(ch[]=='Q')
{
read(u); read(v); read(k);
u^=last; v^=last; k^=last;
lca=get_lca(u,v);
last=has[query(root[u],root[v],root[lca],root[fa[lca][]],,all,k)];
printf("%d\n",last);
}
else
{
read(u); read(v);
u^=last; v^=last;
fu=find(u); fv=find(v);
if(siz[fu]<siz[fv]) swap(fu,fv);
unionn(u,v);
add(u,v);
fa[v][]=u;
dfs(v);
}
}
} int main()
{
init();
discrete();
solve();
}

bzoj千题计划258:bzoj3123: [Sdoi2013]森林的更多相关文章

  1. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  2. bzoj千题计划267:bzoj3129: [Sdoi2013]方程

    http://www.lydsy.com/JudgeOnline/problem.php?id=3129 如果没有Ai的限制,就是隔板法,C(m-1,n-1) >=Ai 的限制:m减去Ai &l ...

  3. bzoj千题计划134:bzoj3124: [Sdoi2013]直径

    http://www.lydsy.com/JudgeOnline/problem.php?id=3124 第一问: dfs1.dfs2 dfs2中记录dis[i]表示点i距离最长链左端点的距离 第二问 ...

  4. bzoj千题计划133:bzoj3130: [Sdoi2013]费用流

    http://www.lydsy.com/JudgeOnline/problem.php?id=3130 第一问就是个最大流 第二问: Bob希望总费用尽量大,那肯定是把所有的花费加到流量最大的那一条 ...

  5. bzoj千题计划268:bzoj3131: [Sdoi2013]淘金

    http://www.lydsy.com/JudgeOnline/problem.php?id=3131 如果已知 s[i]=j 表示有j个<=n数的数码乘积=i 那么就会有 s[a1]*s[a ...

  6. bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器

    http://www.lydsy.com/JudgeOnline/problem.php?id=3122 等比数列求和公式+BSGS #include<map> #include<c ...

  7. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  8. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

  9. bzoj千题计划177:bzoj1858: [Scoi2010]序列操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...

随机推荐

  1. 6、Docker图形化管理(Portainer)

    一.Portainer简介 Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控 ...

  2. 记录Appium-desktop踩过的坑could not find devices

    最近了解到一个自动化入门的新工具appium-desktop,看网上各种文章,感觉这个工具是很简单的一个入门级工具,下载试用了一下. 官网下载,输入网址appium.io,点击下载. 一路傻瓜式安装, ...

  3. PBFT_拜占庭容错算法

    根据论文<Practical Byzantine Fault Tolerance and Proactive Recovery>整理 Practical byzantine fault t ...

  4. MSSQL批量写入数据方案

    近来有一个项目Feature需要有批量写入数据的场景,正巧整理资料发现自己以前也类似实现的项目,在重构的同时把相关资料做了一个简单的梳理,方便大家参考. 循环写入(简单粗暴,毕业设计就这样干的)(不推 ...

  5. 互联网寒冬,Python 程序员如何准备面试

    虽说年年都在喊互联网寒冬,最难就业季,但是今年确实有点不一样,年前年后一波又一波互联网公司宣布『人员调整,结构优化』, 这是往年没发生过的. 是不是面试机会就少了很多呢?不是的. 搜索招聘网站我们可以 ...

  6. PAT甲题题解-1025. PAT Ranking (25)-排序

    排序,求整体的排名和局部的排名整体排序,for循环一遍同时存储整体目前的排名和所在局部的排名即可 #include <iostream> #include <cstdio> # ...

  7. 从零开始学Kotlin-类和对象(5)

    定义一个类 定义一个类,使用关键字class声明,后面跟类名(不使用new) class demo5 {//定义一个类,使用关键字class声明,后面跟类名 fun test() {//类中定义方法 ...

  8. Postgresql 简单安装过程. Study From https://www.cnblogs.com/stulzq/p/7766409.html

    CentOS 下面安装 Postgresql. 的简要学习记录 1. 卸载CentOS上面自带的postgresql版本 rpm -e $(rpm -qa|grep postgre) 2. postg ...

  9. Ubuntu18.04 安装后的简单实用设置[未完成]

    1. 安装完成. 2. 更新 sudo apt-get update 3. 修改vi 放置键盘错位的问题 编辑文件/etc/vim/vimrc.tiny 将“compatible”改成“nocompa ...

  10. Delphi实现DBGrid全选和反选功能

    Delphi实现Dbgrid全选和反选.清除全选的功能,不管是在Delphi下,还是在WEB开发中,这种功能都是很实用的,是进行数据批量操作的基础.本模块就是实现了为Delphi的DBGrid数据列表 ...