前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3674: 可持久化并查集加强版

3674: 可持久化并查集加强版

作者头像
attack
发布2018-04-12 11:24:12
7000
发布2018-04-12 11:24:12
举报

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

出题人大SB++

rope大法好!!!!。

有了rope,

主席树什么的都可以靠边站了,

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<ext/rope>
 7 using namespace std;
 8 using namespace __gnu_cxx;
 9 const int MAXN=2000050;
10 const int maxn=0x7fffffff;
11 void read(int &n)
12 {
13     char c='+';int x=0;bool flag=0;
14     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
15     while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}
16     flag==1?n=-x:n=x;
17 }
18 
19 rope<int> *rp[MAXN];
20 int a[MAXN];
21 int n,m,how,x,y,lastans;
22 int find(int i,int x)
23 {
24     if(rp[i]->at(x)==x)     return x;
25     int f=find(i,rp[i]->at(x));
26     if(f==rp[i]->at(x))        return f;
27     rp[i]->replace(x,f);
28     return f;
29 }
30 void merge(int i,int x,int y)
31 {
32     x=find(i,x),y=find(i,y);
33     if(x!=y)    rp[i]->replace(y,x);
34 }
35 int main()
36 {
37     int n,m;
38     read(n);read(m);
39     for(int i=1;i<=n;i++)    a[i]=i;
40     rp[0]=new rope<int> (a,a+n+1);
41     for(int i=1;i<=m;i++)
42     {
43         rp[i]=new rope<int> (*rp[i-1]);
44         int how;read(how);
45         if(how==1)
46         {
47             read(x);read(y);
48             merge(i,x^lastans,y^lastans);
49         }
50         else if(how==2)
51         {
52             read(x);
53             rp[i]=rp[x^lastans];
54         }
55         else if(how==3)
56         {
57             read(x);read(y);
58             printf("%d\n",lastans=(find(i,x^lastans)==find(i,y^lastans)));
59         }
60     }
61     return 0;
62 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档