bzoj3673&3674: 可持久化并查集 - HZH's Blog - 自闭症
bzoj3673&3674: 可持久化并查集
跪毛主力和zky大神
本题是两位大神+主力出的,所以我这种渣渣只好写渣渣算法了
开始不会做,然后毛主力大发慈悲和我说可以用线段树模拟数组,
然后我就很高兴地谢了个主席树
然后re了
然后调了半个小时发现数组开小了,囧
于是就连A两题。。(本来就差不多的)
3673代码(因为3674因为强制在线有个加密没啥用就不发了)
#include<cstdio> using namespace std; struct mjj { int l,r,dad; mjj *lc,*rc; }*root[200010]; int n,m,i,t,x,y,k; void build0(int l,int r,mjj *&cur) { cur=new mjj;cur->l=l;cur->r=r; cur->lc=NULL;cur->rc=NULL; if (l==r) { cur->dad=l; return; } int mid=(l+r)/2; build0(l,mid,cur->lc); build0(mid+1,r,cur->rc); } void change(int d,int k,mjj *&cur,mjj *cur1) { cur=new mjj;cur->l=cur1->l;cur->r=cur1->r; cur->lc=NULL;cur->rc=NULL; if (cur->l==cur->r){cur->dad=d;return;} int mid=(cur->l+cur->r)/2; if (k>mid)cur->lc=cur1->lc,change(d,k,cur->rc,cur1->rc); if (k<=mid)cur->rc=cur1->rc,change(d,k,cur->lc,cur1->lc); } int find1(int x,mjj *cur) { if (cur->l==cur->r)return cur->dad; int mid=(cur->l+cur->r)/2; if (x<=mid)return find1(x,cur->lc); if (x>mid)return find1(x,cur->rc); } int find(int x,int y) { int dd=find1(x,root[y]); if (dd==x)return x; return find(dd,y); } int main() { /*freopen("3673.in","r",stdin); freopen("3673.out","w",stdout);*/ scanf("%d%d",&n,&m);build0(1,n,root[0]); for (i=1;i<=m;i++) { scanf("%d",&t);root[i]=root[i-1]; if (t==1) { scanf("%d%d",&x,&y); change(find(x,i),find(y,i),root[i],root[i-1]); } if (t==2)scanf("%d",&k),root[i]=root[k]; if (t==3) { scanf("%d%d",&x,&y); printf("%d\n",find(x,i)==find(y,i)); } } }
2014年8月08日 14:58
小哥你确认tag是可持久化算法而不是可持久化数据结构?!
2014年8月10日 19:42
好吧,貌似是数据结构。。
2022年8月15日 23:11
Uttarakhand Board Model Paper 2023 Class 3 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer,
2022年9月08日 02:42
The AP Intermediate students can download the Economics question bank with solved study material with practice questions in chapter wise to every TM, EM, UM student, and the economics subject paper-1 AP 1st Inter Economics Model Paper and paper-2 important questions with suggestions are available through AP Jr and Sr inter Economics Model Paper 2023 Pdf with guess paper.