bzoj 3673 可持久化并查集
2024-10-21 15:58:21
本质上是维护两个可持久化数组,用可持久化线段树维护.
/**************************************************************
Problem: 3673
User: idy002
Language: C++
Result: Accepted
Time:76 ms
Memory:13780 kb
****************************************************************/ #include <cstdio>
#include <algorithm>
#define N 20010
using namespace std; struct Node {
int u, p, s;
Node *ls, *rs;
}pool[N*], *tail=pool, *root[N]; int n, m;
Node *build( int lf, int rg ) {
Node *nd = ++tail;
if( lf==rg ) {
nd->u = lf;
nd->p = lf;
nd->s = ;
return nd;
}
int mid=(lf+rg)>>;
nd->ls = build( lf, mid );
nd->rs = build( mid+, rg );
return nd;
}
Node *modify( Node *nd, int lf, int rg, int u, int p, int s ) {
Node *nn = ++tail;
if( lf==rg ) {
nn->u = u;
nn->p = p;
nn->s = s;
return nn;
}
int mid=(lf+rg)>>;
if( u<=mid ) {
nn->ls = modify( nd->ls, lf, mid, u, p, s );
nn->rs = nd->rs;
} else {
nn->ls = nd->ls;
nn->rs = modify( nd->rs, mid+, rg, u, p, s );
}
return nn;
}
Node *query( Node *nd, int lf, int rg, int u ) {
if( lf==rg ) return nd;
int mid=(lf+rg)>>;
if( u<=mid ) return query(nd->ls,lf,mid,u);
else return query(nd->rs,mid+,rg,u);
} Node *find( Node *r, int a ) {
Node *na = query(r,,n,a);
while( na->u!=na->p ) na=query(r,,n,na->p);
return na;
}
int main() {
scanf( "%d%d", &n, &m );
root[] = build(,n);
for( int i=,opt,a,b,k; i<=m; i++ ) {
scanf( "%d", &opt );
if( opt== ) {
scanf( "%d%d", &a, &b );
root[i] = root[i-];
Node *na = find(root[i],a);
Node *nb = find(root[i],b);
if( na==nb ) continue;
if( na->s > nb->s ) swap(na,nb);
root[i] = modify( root[i], , n, na->u, nb->u, na->s );
root[i] = modify( root[i], , n, nb->u, nb->u, na->s+nb->s );
} else if( opt== ) {
scanf( "%d", &k );
root[i] = root[k];
} else {
scanf( "%d%d", &a, &b );
root[i] = root[i-];
printf( "%d\n", find(root[i],a)->u==find(root[i],b)->u );
}
}
}
最新文章
- 同步机制 note
- 项目管理过程组和知识领域表(PMBOK2008)
- 算法设计和数据结构学习_5(BST&;AVL&;红黑树简单介绍)
- poj 2553 The Bottom of a Graph
- HW5.3
- cf C. Inna and Dima
- hdu 3037 Saving Beans(组合数学)
- Gentoo/Arch常用软件配置
- google和oracle闹掰,Java 会不会被抛弃?
- ZOJ2212 Argus 优先队列 结构体
- 《You dont know JS》类型篇总结
- python web开发-flask读取txt文件内容
- Python3的requests类抓取中文页面出现乱码的解决办法
- 【CSS 第五天】背景,边框
- C++标准库algorithm
- javascript20150124
- 无法连接到localhost.其他信息:用户“sa”登录失败。(MicroSoft Sql Server,错误:18456)
- linux安装experss搭建本地服务器
- NSSearchPathForDirectoriesInDomains用法(转)
- [opencv] 图像几何变换:旋转,缩放,斜切