当前位置:网站首页>P3402 persistent and searchable
P3402 persistent and searchable
2022-07-03 19:14:00 【It's Alpaca duck】
Topic link : Click here
The main idea of the topic :
Given n n n A collection of , The first i i i There is only one number in the initial state in a set , by i i i.
Yes m m m operations . The operation is divided into 3 3 3 Kind of :
1 a b 1\ a\ b 1 a b Merge a , b a,b a,b In the collection ;
2 k 2\ k 2 k Back to the first k k k operations ( Performing any of the three operations is recorded as an operation ) The state after ;
3 a b 3\ a\ b 3 a b inquiry a , b a,b a,b Whether they belong to the same set , Output if yes 1 1 1 , Otherwise output 0 0 0.
For each operation 3 3 3, Output an integer on a line to represent the answer
Topic analysis :
Use the chairman tree to build a persistent array to serve as the array subscript of the original union search set , For the search operation that the search subscript directly corresponds to the persistent array
The idea of rank based merging is needed to perform the merging operation to ensure the time complexity , The reason for not using path compression is that using path compression may make it easy for updates to generate a lot of redundant space M L E MLE MLE
See code for details :
// Problem: P3402 Persistent and searchable
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3402
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<unordered_map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
//#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
struct node{
int l,r;
}nod[maxn*32];
int n,m,cnt,root[maxn],fa[maxn*32],dep[maxn*32];
void build(int &u,int l,int r)
{
nod[u = ++cnt];
if(l == r)
{
fa[u] = l;
return ;
}
int mid = l+r>>1;
build(nod[u].l,l,mid);
build(nod[u].r,mid+1,r);
}
int query(int u,int l,int r,int pos)
{
if(l == r) return u;
int mid = l+r>>1;
if(pos <= mid) return query(nod[u].l,l,mid,pos);
else return query(nod[u].r,mid+1,r,pos);
}
int find(int root,int x)
{
int pos = query(root,1,n,x);
if(fa[pos] == x) return pos;
return find(root,fa[pos]);
}
void update(int &u,int pre,int l,int r,int pos,int fax) // modify pos My father is fax
{
nod[u = ++cnt];
if(l == r)
{
fa[u] = fax;
dep[u] = dep[pre];
return ;
}
int mid = l+r>>1;
nod[u].l = nod[pre].l;
nod[u].r = nod[pre].r;
if(pos <= mid) update(nod[u].l,nod[pre].l,l,mid,pos,fax);
else update(nod[u].r,nod[pre].r,mid+1,r,pos,fax);
}
void add(int u,int l,int r,int pos) // Add one to the root depth of a union search set
{
if(l == r)
{
dep[u]++;
return ;
}
int mid = l+r>>1;
if(pos <= mid) add(nod[u].l,l,mid,pos);
else add(nod[u].r,mid+1,r,pos);
}
int main()
{
n = read(),m = read();
build(root[0],1,n);
for(int i = 1;i <= m;i++)
{
int opt = read();
if(opt == 1)
{
int x = read(),y = read();
root[i] = root[i-1];
int px = find(root[i],x),py = find(root[i],y);
if(fa[px] == fa[py]) continue;
if(dep[px] > dep[py]) swap(px,py);
update(root[i],root[i-1],1,n,fa[px],fa[py]);
if(dep[px] == dep[py]) add(root[i],1,n,fa[py]); // Note that fa[py], Because the new root at this time is fa[py], The depth is increased 1
}
else if(opt == 2)
{
int id = read();
root[i] = root[id];
}
else if(opt == 3)
{
int x = read(),y = read();
root[i] = root[i-1];
int fax = find(root[i],x),fay = find(root[i],y);
puts(fax==fay ? "1" : "0");
}
}
return 0;
}
边栏推荐
- SQL injection for Web Security (1)
- The most valuable thing
- 我们做了一个智能零售结算平台
- [water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
- The installation path cannot be selected when installing MySQL 8.0.23
- We have built an intelligent retail settlement platform
- [leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult
- ActiveMQ的基础
- Pan for in-depth understanding of the attention mechanism in CV
- Record: pymysql is used in pycharm to connect to the database
猜你喜欢

组策略中开机脚本与登录脚本所使用的用户身份

Flutter网络和数据存储框架搭建 -b1

In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life

Smart wax therapy machine based on STM32 and smart cloud

ActiveMQ的基础

Ego planner code parsing Bspline_ Optimizer section (3)

Counting from the East and counting from the West will stimulate 100 billion industries. Only storage manufacturers who dare to bite the "hard bone" will have more opportunities

我眼中真正优秀的CTO长啥样

Record: solve the problem that MySQL is not an internal or external command environment variable

【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】
随机推荐
leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
Help change the socket position of PCB part
EGO Planner代码解析bspline_optimizer部分(1)
01. Preparation for automated office (free guidance, only three steps)
【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
Simple solution of physical backup and restore of Damon database
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
硬盘监控和分析工具:Smartctl
Le changement est un thème éternel
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
Merge K ascending linked lists
Add control at the top of compose lazycolumn
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
EGO Planner代碼解析bspline_optimizer部分(1)
變化是永恒的主題
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
How does if ($variable) work? [repeat] - how exactly does if ($variable) work? [duplicate]
Analysis of dart JSON encoder and decoder
[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
Think of new ways