本质上是维护两个可持久化数组,用可持久化线段树维护.

 /**************************************************************
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 );
}
}
}

最新文章

  1. 同步机制 note
  2. 项目管理过程组和知识领域表(PMBOK2008)
  3. 算法设计和数据结构学习_5(BST&amp;AVL&amp;红黑树简单介绍)
  4. poj 2553 The Bottom of a Graph
  5. HW5.3
  6. cf C. Inna and Dima
  7. hdu 3037 Saving Beans(组合数学)
  8. Gentoo/Arch常用软件配置
  9. google和oracle闹掰,Java 会不会被抛弃?
  10. ZOJ2212 Argus 优先队列 结构体
  11. 《You dont know JS》类型篇总结
  12. python web开发-flask读取txt文件内容
  13. Python3的requests类抓取中文页面出现乱码的解决办法
  14. 【CSS 第五天】背景,边框
  15. C++标准库algorithm
  16. javascript20150124
  17. 无法连接到localhost.其他信息:用户“sa”登录失败。(MicroSoft Sql Server,错误:18456)
  18. linux安装experss搭建本地服务器
  19. NSSearchPathForDirectoriesInDomains用法(转)
  20. [opencv] 图像几何变换:旋转,缩放,斜切

热门文章

  1. li分两列显示
  2. 【原创】backbone1.1.0源码解析之View
  3. 阿里云Linux服务器安装 nginx+mysql+php
  4. Linux - awk 文本处理工具三
  5. python爬虫-图片批量下载
  6. 2016最新的中国省市区三级数据库表.sql mssql
  7. 八、mini2440裸机程序之UART(2)UART0与PC串口通信【转】
  8. 读写分离MYSQL类
  9. MySQL安装与初步操作
  10. opencv 车牌字符分割 ANN网络识别字符