当前位置:网站首页>[sdoi2013] forest
[sdoi2013] forest
2022-06-26 13:58:00 【__ LazyCat__】
Chairman tree on the tree + Heuristic merging
link :[P3302 SDOI2013] The forest - Luogu | New ecology of computer science education (luogu.com.cn)
The question : Given a forest , There are multiple operations , You can query x To y The second on the path k Small , You can also connect two trees into one .
Answer key : First interval No k Small , Chairman tree maintenance . The third in the tree k The little empathy chairman tree , Select to inherit from the parent node when inheriting . Query at the same time 4 A little bit x , y , l c a , f a [ l c a ] x,y,lca,fa[lca] x,y,lca,fa[lca]. Now there are more connection operations , Due to the maximum number of connections n-1 After that, it finally becomes a tree , So consider heuristic merging , Each time we merge, we violently pull down the small trees and put them under the big trees , The average allocation complexity is log Level , Each point will rebuild the multiplier array, which is also complex log, The whole is revised to O ( n l o g 2 n ) O(nlog^2n) O(nlog2n). Maintain the set of the lower tree with the union search set to facilitate the modification and calculation of the tree size .
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
char op;
int a[maxn],b[maxn],n,m,q,mx,u,v,w,ans;
int dep[maxn],fa[maxn][18],lg[maxn],ft[maxn],sz[maxn];
int ls[maxn<<7],rs[maxn<<7],sum[maxn<<7],rt[maxn],now;
vector<int>ed[maxn];
int find(int x)
{
return ft[x]==x?x:ft[x]=find(ft[x]);
}
void update(int u,int&v,int l,int r,int pos)
{
v=++now,ls[v]=ls[u],rs[v]=rs[u],sum[v]=sum[u]+1;
if(l==r)return;
int mid=l+r>>1;
if(pos<=mid)update(ls[u],ls[v],l,mid,pos);
else update(rs[u],rs[v],mid+1,r,pos);
return;
}
int query(int u1,int u2,int v1,int v2,int l,int r,int k)
{
if(l==r)return b[l];
int mid=l+r>>1,num=sum[ls[v1]]+sum[ls[v2]]-sum[ls[u1]]-sum[ls[u2]];
if(num>=k)return query(ls[u1],ls[u2],ls[v1],ls[v2],l,mid,k);
else return query(rs[u1],rs[u2],rs[v1],rs[v2],mid+1,r,k-num);
}
int LCA(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
while(dep[x]>dep[y])
{
x=fa[x][lg[dep[x]-dep[y]]-1];
}
if(x==y)return x;
for(int i=17;i>=0;i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i],y=fa[y][i];
}
}
return fa[x][0];
}
void dfs(int x,int f,int t)
{
dep[x]=dep[f]+1,fa[x][0]=f,sz[t]++,ft[x]=t;
update(rt[f],rt[x],1,mx,a[x]);
for(int i=1;i<=17;i++)
{
fa[x][i]=fa[fa[x][i-1]][i-1];
}
for(auto y:ed[x])
{
if(y!=f)dfs(y,x,t);
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T; cin>>T;
for(int i=1;i<=1e5;i++)lg[i]=lg[i-1]+(!(i&i-1));
cin>>n>>m>>q;
for(int i=1;i<=n;i++)cin>>a[i],b[i]=a[i],ft[i]=i;
sort(b+1,b+1+n); mx=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+1+mx,a[i])-b;
for(int i=1;i<=m;i++)
{
cin>>u>>v;
ed[u].push_back(v);
ed[v].push_back(u);
}
for(int i=1;i<=n;i++)if(ft[i]==i)dfs(i,0,i);
for(int i=1;i<=q;i++)
{
cin>>op;
if(op=='Q')
{
cin>>u>>v>>w;
u^=ans,v^=ans,w^=ans;
int lca=LCA(u,v);
ans=query(rt[lca],rt[fa[lca][0]],rt[u],rt[v],1,mx,w);
cout<<ans<<"\n";
}
else
{
cin>>u>>v;
u^=ans,v^=ans;
int x=find(u),y=find(v);
if(sz[x]<sz[y])swap(x,y),swap(u,v);
dfs(v,u,x),sz[y]=0;
ed[v].push_back(u);
ed[u].push_back(v);
}
}
return 0;
}
边栏推荐
- Ring queue PHP
- Hands on data analysis unit 3 model building and evaluation
- 12 SQL optimization schemes summarized by old drivers (very practical)
- Range of types
- Bug STL string
- ES基于Snapshot(快照)的数据备份和还原
- Luogu p3426 [poi2005]sza-template solution
- Taishan Office Technology Lecture: four cases of using bold font
- 【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
- Included angle of 3D vector
猜你喜欢

CloudCompare——泊松重建

Here document interaction free and expect automatic interaction

Zero basics of C language lesson 8: Functions

Use of wangeditor rich text editor

Use performance to see what the browser is doing

ICML 2022 | limo: a new method for rapid generation of targeted molecules

8. Ribbon load balancing service call

Calculate the distance between two points (2D, 3D)

计算两点之间的距离(二维、三维)

shell脚本详细介绍(四)
随机推荐
Hands on data analysis unit 3 model building and evaluation
李航老师新作《机器学习方法》上市了!附购买链接
MySQL configuration improves data insertion efficiency
Svn commit error after deleting files locally
character constants
mysql配置提高数据插入效率
Wechat applet - bind and prevent event bubble catch
There are many contents in the widget, so it is a good scheme to support scrolling
虫子 内存管理 上
CloudCompare——泊松重建
ES基於Snapshot(快照)的數據備份和還原
Zero basics of C language lesson 8: Functions
Detailed introduction to shell script (4)
【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
Use performance to see what the browser is doing
Original code, inverse code and complement code
7-2 the cubic root of a number
Input text to automatically generate images. It's fun!
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
33、使用RGBD相机进行目标检测和深度信息输出