http://www.lydsy.com/JudgeOnline/problem.php?id=3674

http://www.lydsy.com/JudgeOnline/problem.php?id=3673

双倍经验啦啦啦。。

给主席树换了个名称果然高大上。。。

首先要可持久化并查集其实就是可持久化数组。。。

那么因为数组的形式是这样的$P[x]$,那么我们用一种数据结构实现查找x返回对应的$P[x]$即可啦啦啦。

然后那么我所学的可持久化目前只有主席树QAQ哪天去写写fhqtreap。。。

所以用主席树查找下标x,然后在叶子中维护父亲。

然后就完了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <ext/rope>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=2*1e5+10;
struct node { int l, r, f; }t[N*100];
int cnt, root[N], n; void update(int l, int r, int &x, int pos, int f) {
t[++cnt]=t[x]; x=cnt;
if(l==r) { t[x].f=f; return; }
int mid=(l+r)>>1;
if(pos<=mid) update(l, mid, t[x].l, pos, f);
else update(mid+1, r, t[x].r, pos, f);
}
int ask(int l, int r, int x, int pos) {
if(l==r) return t[x].f;
int mid=(l+r)>>1;
if(pos<=mid) return ask(l, mid, t[x].l, pos);
else return ask(mid+1, r, t[x].r, pos);
}
int P(int x, int y) { return ask(1, n, x, y); }
int un(int &x, int a, int b) { update(1, n, x, a, b); return b; } // union p(a)=y
int find(int &x, int y) { int f=P(x, y); return f==y?y:un(x, y, find(x, f)); }
int main() {
read(n); int m=getint(), la=0;
for1(i, 1, n) update(1, n, root[0], i, i);
for1(i, 1, m) {
int c=getint();
root[i]=root[i-1];
if(c==1) {
int x=getint(), y=getint(); x^=la; y^=la;
int fx=find(root[i], x), fy=find(root[i], y);
if(fx!=fy) un(root[i], fx, fy);
}
else if(c==2) root[i]=root[getint()^la];
else {
int x=getint(), y=getint(); x^=la; y^=la;
int fx=find(root[i], x), fy=find(root[i], y);
printf("%d\n", la=(fx==fy));
}
}
return 0;
}

  


Description

Description:
自从zkysb出了可持久化并查集后……
hzwer:乱写能AC,暴力踩标程
KuribohG:我不路径压缩就过了!
ndsf:暴力就可以轻松虐!
zky:……

n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0
请注意本题采用强制在线,所给的a,b,k均经过加密,加密方法为x = x xor lastans,lastans的初始值为0
0<n,m<=2*10^5

Input

 

Output

 

Sample Input

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

Sample Output

1
0
1

HINT

 

Source

【BZOJ】3673: 可持久化并查集 by zky & 3674: 可持久化并查集加强版(可持久化线段树)的更多相关文章

  1. bzoj3673: 可持久化并查集 by zky&&3674: 可持久化并查集加强版

    主席树可持久化数组,还挺好YY的 然而加强版要路径压缩.. 发现压了都RE 结果看了看数据,默默的把让fx的父亲变成fy反过来让fy的父亲变成fx 搞笑啊 #include<cstdio> ...

  2. bzoj 2733: [HNOI2012]永无乡【并查集+权值线段树】

    bzoj上数组开大会T-- 本来想用set瞎搞的,想了想发现不行 总之就是并查集,每个点开一个动态开点的权值线段树,然后合并的时候把值并在根上,询问的时候找出在根的线段树里找出k小值,看看这个值属于哪 ...

  3. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  4. 洛谷P3224 [HNOI2012]永无乡(线段树合并+并查集)

    题目描述 永无乡包含 nnn 座岛,编号从 111 到 nnn ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 nnn 座岛排名,名次用 111 到 nnn 来表示.某些岛之间由巨大的桥连接, ...

  5. 洛谷P4121 [WC2005]双面棋盘(线段树套并查集)

    传送门 先膜一下大佬->这里 据说这题正解是LCT,然而感觉还是线段树套并查集的更容易理解 我们对于行与行之间用线段树维护,每一行内用并查集暴力枚举 每一行内用并查集暴力枚举连通块这个应该容易理 ...

  6. 区间第K小——可持久化线段树模板

    概念 可持久化线段树又叫主席树,之所以叫主席树是因为这东西是fotile主席创建出来的. 可持久化数据结构思想,就是保留整个操作的历史,即,对一个线段树进行操作之后,保留访问操作前的线段树的能力. 最 ...

  7. 【BZOJ 3674】可持久化并查集加强版&【BZOJ 3673】可持久化并查集 by zky 用可持久化线段树破之

    最后还是去掉异或顺手A了3673,,, 并查集其实就是fa数组,我们只需要维护这个fa数组,用可持久化线段树就行啦 1:判断是否属于同一集合,我加了路径压缩. 2:直接把跟的值指向root[k]的值破 ...

  8. BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...

  9. bzoj 3673&3674 可持久化并查集&加强版(可持久化线段树+启发式合并)

    CCZ在2015年8月25日也就是初三暑假要结束的时候就已经能切这种题了%%% 学习了另一种启发式合并的方法,按秩合并,也就是按树的深度合并,实际上是和按树的大小一个道理,但是感觉(至少在这题上)更好 ...

随机推荐

  1. 为win7添加ubuntu的启动引导项

    利用MBRFix删除ubuntu的开机引导界面,恢复成win7引导之后,为win7添加ubuntu的启动引导项: 直接利用EasyBCD添加一个Grub2的引导项即可 参考:http://mathis ...

  2. linux shell expr 使用

    linux shell expr 使用 收藏人:春秋百味 -- | 阅: 转: | | 分享 非原创, 摘自:<LINUX与UNIX Shell编程指南> 17.5 expr用法 expr ...

  3. Redis使用介绍

    Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用.它提供了Pyth ...

  4. 【云计算】ubuntu下docker安装配置指南

    Docker Engine安装配置 以下描述仅Docker在Ubuntu Precise 12.04 (LTS).Ubuntu Trusty 14.04 (LTS).Ubuntu Wily 15.10 ...

  5. 73 [面试题]交换一个整数的二进制表示的奇偶位(swapOddEvenBits)

    [本文链接] http://www.cnblogs.com/hellogiser/p/swap-odd-even-bits.html [分析] 假定一个数字是8位数,设为ABCDEFGH ABCDEF ...

  6. 【转】cas注册后自动登录

    本文转自:http://denger.iteye.com/blog/805743  1. 关于CAS的介绍不再累述,我想涉及过SSO同学应该都会对该框架所有了解,我们目前项目采用的CAS Server ...

  7. iOS7 中的新加入的下载类NSURLSession(随ios版本更新而更新)

    想详细的了解网络下载的相关知识,要仔细阅读URL Loading System Programming Guide 这里有篇好文章(http://www.shinobicontrols.com/blo ...

  8. centos 编译安装Apache 2.4

    2013年12月29日 16:40:20 ./configure --prefix=/usr/local/web/apache --enable-so --enable-rewrite --enabl ...

  9. Java动态加载类在功能模块开发中的作用

    Java中我们一般会使用new关键字实例化对象然后调用该对象所属类提供的方法来实现相应的功能,比如我们现在有个主类叫Web类这个类中能实现各种方法,比如用户注册.发送邮件等功能,代码如下: /* * ...

  10. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...