Description

n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0

0<n,m<=2*10^4

Input

 

Output

 

Sample Input

5 6
1 1 2
3 1 2
2 0
3 1 2
2 1
3 1 2

Sample Output

1
0
1

HINT

Source

出题人大S

【分析】

出题人给我滚出来!保证不打死你!

真是***了,你题意描述清楚点会死啊。。调了将近2个小时...结果是题目理解错了....尼玛返回也算作操作啊。

思路还是蛮简单的。

用主席树维护一下并查集的fa数组就行了。

按照这种说法树状数组也应该可以可持久化了

 /*
唐代李商隐
《无题·昨夜星辰昨夜风》 昨夜星辰昨夜风,画楼西畔桂堂东。
身无彩凤双飞翼,心有灵犀一点通。
隔座送钩春酒暖,分曹射覆蜡灯红。
嗟余听鼓应官去,走马兰台类转蓬。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = * * + ;
//const int MAXM = 20000 + 10;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
int n, m;//n为元素总个数
struct SEGTREE{
//路径压缩+启发式合并还要用主席树OAO
struct Node{
Node *ch[];
int l, r;
int num;//其实除了叶子节点其他都是打酱油的,num是该节点的fa值
}mem[MAXN], *root[ * + ];
int tot; void init(){
tot = ;
root[] = NULL;
for (int i = ; i <= * ; i++) root[i] = NULL;
build(root[], , n);
//printf("%d %d\n", root[0]->ch[0]->l, root[0]->ch[0]->r);
}
Node *NEW(int l, int r){
Node *p = &mem[tot++];
p->l = l;
p->r = r;
p->num = -;
p->ch[] = p->ch[] = NULL;
return p;
}
void build(Node *&t, int l, int r){
if (t == NULL){
t = NEW(l, r);
//不要返回
}
if (l == r) return;
int mid = (l + r) >> ;
build(t->ch[], l, mid);
build(t->ch[], mid + , r);
}
//t为现在的数将x的num改为val
void insert(Node *&t, Node *&last, int x, int val){
if (t == NULL){
t = NEW(last->l, last->r);
}
if (t->l == x && t->r == x) {t->num = val; return;}
int mid = (t->l + t->r) >>;
if (x <= mid){
insert(t->ch[], last->ch[], x, val);
t->ch[] = last->ch[];
}
if (x > mid){
insert(t->ch[], last->ch[], x, val);
t->ch[] = last->ch[];
}
}
//直接修改,不是可持久化的,节省空间
/*void change(Node *&t, int x, int val){
if (t->l == x && t->r == x) {t->num = val;return;}
int mid = (t->l + t->r) >> 1;
if (x <= mid) change(t->ch[0], x, val);
if (x > mid) change(t->ch[1], x, val);
}*/
int get(int k, int x){//查找k时刻x的fa值
Node *t = root[k];
while (){
if (t->l == x && t->r == x) break;
int mid = (t->l + t->r) >> ;
if (x <= mid) t = t->ch[];
else t = t->ch[];
}
return t->num;
}
}A;
int data[MAXN];//真正的操作次数
int cnt = ;//cnt记录现在的状态
int BIGCNT; int find(int x){
int tmp = A.get(cnt, x);
if (tmp < ) return x;
else{
int tmp2 = find(tmp);
//A.insert(A.root[cnt + 1], A.root[cnt], x, tmp2);
//cnt++;
return tmp2;
}
}
//启发式合并
void merge(int x, int y){
//分别代表真实数量
int x_num = -A.get(cnt, x);
int y_num = -A.get(cnt ,y);
if (x_num > y_num){//将y合并到x上
//这里可以可持久化了
//A.root[cnt + 1] = NULL;
A.insert(A.root[BIGCNT + ], A.root[cnt], x, -(x_num + y_num));
BIGCNT++;
//A.root[cnt + 1] = NULL;
A.insert(A.root[BIGCNT + ], A.root[cnt], y, x);
BIGCNT++;
}else{
//A.root[cnt + 1] = NULL;
A.insert(A.root[BIGCNT + ], A.root[cnt], y, -(x_num + y_num));
BIGCNT++;
//A.root[cnt + 1] = NULL;
A.insert(A.root[BIGCNT + ], A.root[cnt], x, y);
BIGCNT++;
//printf("%d %d %d\n", x, y, find(x));
}
}
void work(){
int z = ;//记录操作的
data[] = ;
cnt = ;
BIGCNT = ;
scanf("%d%d", &n, &m);
A.init();
for (int i = ; i <= m; i++){
int t;
scanf("%d", &t);
if (t == ){
int c;
scanf("%d", &c);//回到c时刻即操作之后
if (c == )
printf("");
cnt = data[c];
}else if (t == ){
int a, b;
scanf("%d%d", &a, &b);
int xa = find(a), xb = find(b);
if (xa == xb) {data[i] = cnt;continue;}
merge(xa, xb);
cnt = BIGCNT;
}else{
int a, b;
scanf("%d%d", &a, &b);
if (find(a) == find(b)) printf("1\n");
else printf("0\n");
}
data[i] = cnt;
}
//printf("%d", data[6]);
} int main(){ work();
return ;
}

【BZOJ3673】【可持久化并查集】可持久化并查集 by zky的更多相关文章

  1. 浅谈并查集&种类并查集&带权并查集

    并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...

  2. 庐山真面目之十二微服务架构基于Docker搭建Consul集群、Ocelot网关集群和IdentityServer版本实现

    庐山真面目之十二微服务架构基于Docker搭建Consul集群.Ocelot网关集群和IdentityServer版本实现 一.简介      在第七篇文章<庐山真面目之七微服务架构Consul ...

  3. 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  4. 分布式缓存技术redis学习(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  5. 分布式缓存技术redis系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  6. redis高级应用(集群搭建、集群分区原理、集群操作)

    文章主目录 Redis集群简介 Redis集群搭建 Redis集群分区原理 集群操作 参考文档 本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 ...

  7. redis系列之4----redis高级应用(集群搭建、集群分区原理、集群操作)

    文章主目录 Redis集群简介 Redis集群搭建 Redis集群分区原理 集群操作 参考文档 本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 ...

  8. 庐山真面目之六微服务架构Consul集群、Ocelot网关集群和Nginx版本实现

    庐山真面目之六微服务架构Consul集群.Ocelot网关集群和Nginx版本实现 一.简介      在上一篇文章<庐山真面目之五微服务架构Consul集群.Ocelot网关和Nginx版本实 ...

  9. 庐山真面目之七微服务架构Consul集群、Ocelot网关集群和IdentityServer4版本实现

    庐山真面目之七微服务架构Consul集群.Ocelot网关集群和IdentityServer4版本实现 一.简介      在上一篇文章<庐山真面目之六微服务架构Consul集群.Ocelot网 ...

  10. mongodb3.6集群搭建:分片+副本集

    mongodb是最常用的noSql数据库,在数据库排名中已经上升到了前五.这篇文章介绍如何搭建高可用的mongodb(分片+副本)集群. 在搭建集群之前,需要首先了解几个概念:路由,分片.副本集.配置 ...

随机推荐

  1. 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)

    命令模式:游戏开发设计模式之命令模式(unity3d 示例实现) 对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现) 原型模式:游戏开发设计模式之原型模式 & unity3d ...

  2. hdoj 2277 Change the ball【找规律】

    Change the ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. 快速设计一个简单的WPF串口上位机

    最近一直在学习UWP,其中有的技术参考了WPF,所以又回头再来学习WPF,感觉学的东西很杂,必须记录一下,不然时间长了还得忘掉,于是申请开始写博客,将学习的心得记录一下,以备后用.这次是因为公司内训, ...

  4. smarty、thinkphp中的html加载其他的html文件的方式

    1.smarty 在模板文件中,使用定界符 {include file="header.html"}  不可以省略.html 2.thinkphp的html文件中 <incl ...

  5. “发送至Onenote”惹来的小麻烦(转)

    原文来自 :  http://blog.csdn.net/yiyu_0417/article/details/7864172 [看到这篇文章,我觉得以后我也会遇到这个问题,很有价值我就先拷贝了,留着以 ...

  6. IAAS云计算产品畅想-公有云主机产品优势

    关于云计算的优势介绍真是太多太多了,但是说实话准确性欠妥. 云计算也是有很多细分的: 公有云.私有云.混合云 IAAS.PAAS.SAAS 园区云.行业云(医疗云.教育云等等) 说起优点来,绝对不能一 ...

  7. android-----JNI学习 helloworld

    (1)新建android工程 (2)添加NDK路径 (3)添加本地支持 给本地库起名 此时工程目录下会自动生成jni文件夹 此时Makefile也自动生成 LOCAL_PATH := $(call m ...

  8. iOS开发-No matching provisioning profiles found解决方法

    今天真机调试的时候莫名其妙遇到了这种一个问题: This product type must be built using a provisioning profile, however no pro ...

  9. CSS——(1)基础

    CSS(Cascading Style Sheets)层叠样式表 含义 一种计算机语言: 能够实现网页与内容分离: 用来表现文件样式,如HTML或XML: 比較 div与css 假设说div是容器的话 ...

  10. UIPickView的简单介绍

    UIPickView的简单介绍 设置UIPickView的时候,我们主要需要设置一下下面的两个属性 UIPickerView *pickView1; pickView1 = [[UIPickerVie ...