随机化优化可持久化并查集
我在 114514 年前打开了:P3402 【模板】可持久化并查集,想要用 __gnu_cxx::rope 水过。但是被卡常了。我们考虑优化一下。
翻阅了网上的资料,发现这篇文章的代码并不是在线的,并且会被卡掉。
资料代码
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_cxx;
using namespace std;
const int N=2e5+5;
int f[N],r[N],n,m,op,a,b,cnt1,cnt2,cnt3;
struct node{
int op,a,b;
};
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
return s*w;
}
int find(rope<int> &fa,int &i)
{
int f=fa[i];
return f==i?i:find(fa,f);
}
void merge(rope<int> &fa,int &a,int &b)
{
a=find(fa,a),b=find(fa,b);
if(a==b) return;
if(r[a]>r[b]) fa.replace(b,a);
else
{
if(r[a]==r[b]) r[b]++;
fa.replace(a,b);
}
}
void prework(){for(int i=1;i<=n;i++) f[i]=i;f[0]=1;}
vector<node> qus;
signed main()
{
qus.emplace_back((node){-1,-1,-1});
n=read(),m=read();
rope<int> fa[m+1];
prework();
fa[0]=rope<int>(f);
for(int i=1;i<=m;i++)
{
op=read();
if(op==1)
{
a=read(),b=read();
qus.emplace_back((node){op,a,b});
cnt1++;
}
else if(op==2) a=read(),cnt2++,qus.emplace_back((node){op,a,-1});
else
{
a=read(),b=read();
qus.emplace_back((node){op,a,b});
cnt3++;
}
}
if(cnt1>=99999&&cnt2<=3)
{
printf("0");
return 0;
}
for(int i=1;i<=m;i++)
{
op=qus[i].op;
if(op==1)
{
fa[i]=fa[i-1];
a=qus[i].a,b=qus[i].b;
merge(fa[i],a,b);
}
else if(op==2) a=qus[i].a,fa[i]=fa[a];
else
{
fa[i]=fa[i-1];
a=qus[i].a,b=qus[i].b;
printf("%d",(find(fa[i],a)==find(fa[i],b)?1:0));
putchar('\n');
}
}
}同样,还有一篇题解的:
题解代码
#include <bits/stdc++.h>
#include <ext/rope>
//#define int long long
#define endl '\n'
#define rint register unsigned
using namespace __gnu_cxx;
using namespace std;
#define IN stdin->_IO_read_ptr < stdin->_IO_read_end ? *stdin->_IO_read_ptr++ : __uflow(stdin)
#define OUT(_ch) stdout->_IO_write_ptr < stdout->_IO_write_end ? *stdout->_IO_write_ptr++ = _ch : __overflow(stdout, _ch)
const int N = 2e5 + 5;
unsigned f[N];
unsigned r[N];
void read(unsigned &x)
{
x = 0;
char ch = IN;
while (ch < 47)
ch = IN;
while (ch > 47)
x = (x << 1) + (x << 3) + (ch & 15), ch = IN;
}
unsigned find(rope<unsigned> &fa, unsigned &i)
{
unsigned f = fa[i];
return f == i ? i : find(fa, f);
}
void merge(rope<unsigned> &fa, unsigned &a, unsigned &b)
{
a = find(fa, a);
b = find(fa, b);
if (a == b)
{
return ;
}
if (r[a] > r[b])
{
fa.replace(b, a);
}
else
{
if (r[a] == r[b])
{
r[b]++;
}
fa.replace(a, b);
}
}
signed main()
{
unsigned n, m, op, a, b;
read(n);
read(m);
rope<unsigned> fa[m + 1];
for (rint i = 1; i <= n; i++)
{
f[i] = i;
}
f[0] = 1;
fa[0] = rope<unsigned>(f);
for (rint i = 1; i <= m; i++)
{
read(op);
switch (op)
{
case 1:
fa[i] = fa[i - 1];
read(a);
read(b);
merge(fa[i], a, b);
break;
case 2:
read(a);
fa[i] = fa[a];
break;
default:
fa[i] = fa[i - 1];
read(a);
read(b);
OUT(find(fa[i], a) == find(fa[i], b) ? '1' : '0');
puts("");
}
}
return 0;
}
这写代码都使用了快读或离线特判等方式。
但是其实还有一种方法。随机进行路径压缩。
PS:这个帖子其实是我发的,我被【】了。 我解除禁言了。
代码
#include<bits/extc++.h>
using namespace std;
const int maxn=5e5+10;
int rk[maxn],T;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
return s*w;
}
int opt1,opt2,opt3;
struct DSU{
int ver=0;
__gnu_cxx::rope<int> fa[maxn];
void reset(int v,int n){
fa[v].push_back(1);
for(int i=1;i<n;i++)fa[v].push_back(i),rk[i]=1;
}
int find(int x){
if(fa[ver][x]==x)return x;
if(rand()%T==0)fa[ver].replace(x,find(fa[ver][x]));
return find(fa[ver][x]);
}
void merge(int x,int y){
ver++;
fa[ver]=fa[ver-1];
int fx=find(x),fy=find(y);
if(fx==fy)return;
//fa[fx]=fy
if(rk[fx]<=rk[fy])fa[ver].replace(fx,fy);
else fa[ver].replace(fy,fx);
if(rk[fx]==rk[fy]&&fx!=fy)rk[fy]++;
}
void back(int v){
ver++;
fa[ver]=fa[v];
}
bool group(int a,int b){
ver++;
fa[ver]=fa[ver-1];
return find(a)==find(b);
}
};
int main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
int n,m;
n=read(),m=read();
DSU d;
T=sqrt(n)*log2(n);
d.reset(0,n+1);
while(m--){
int opt;
opt=read();
if(opt==1){
int a,b;
a=read(),b=read();
d.merge(a,b);
}
else if(opt==2){
int k;
k=read();
d.back(k);
}
else{
int a,b;
a=read(),b=read();
putchar(d.group(a,b)^48);
putchar('\n');
}
}
return 0;
} 时非常快。此时相当于 的概率进行路径压缩。这正与我发的帖子中的 接近。我当时只尝试了 这三个数。
这样使得由原来的 变为【神秘】。我也不会证啊。
不加任何快读,最慢点的是 。这已经是吊打优于同学的主席树的解法了。
有实力的巨佬可以尝试证一下最优时间复杂度,并尝试把这种优化运用到主席树可持久化并查集上。
UPD:实际上,将这种技术运用到可持久化线段树维护的并查集上并没有较大的优化,因为并查集本身的复杂度 并没有优于数据结构的复杂度 。之所以使用 rope 可以优化,是因为数据结构本身的时间复杂度(包括单点查询,a[x] 也是)是 的,相比于并查集的 略劣。
随机化优化树剖 DDP
考虑用树剖进行卡常。
前置知识。
实际上,我并不知道怎么卡树剖。
我们首先想正常树剖只需要完全二叉树即可。
我们有一个优化:对于每条链开线段树。于是可以做到 。 是重链长。
完全二叉树
如图,。所以时间复杂度 。
链套链(根据 @035966_L3 的描述)
一个长为 的主链,第 个点上面串 个点这种形式。
这样的话,一直查询主链最下面那个点即可。这样会被卡到 。(顺带一提,如果每次查询的点是随机的,那么能获得 的期望复杂度)
就算不优化了,也有 的常数。
我们考虑进行随机化,对于重儿子 和次重儿子 的重量 ,如果 ,我们就随机选择一个进行剖分。
在这个数据上,可以做到期望 。凭借这个赢得了与 Inracle 的卡常大战。
链二叉树
把二叉树点数开到 ,然后把每个点改成 长度的链。
这样就是真的 了,但是考虑有 的常数。另外,由于随机化,出题人不可能通过选择特定点卡掉我们。这个时间复杂度期望我不会算,至少也有个 的常数,于是最后常数就是 。
