题目链接:

解法:

先建n颗平衡树,合并的时候将a中最右的结点翻转到根节点,b中最左的结点翻转到根节点,对合并后的根节点进行标记。

#include <bits/stdc++.h>
using namespace std;
#define ls(p) p << 1
#define rs(p) p << 1 | 1
const int M = 1e5 + ;
int n, m, cnt;
int rt[M];
struct node{
int fa, ch[], val, cnt, sz, lazy;
}spl[M];
bool ident(int x, int f) {
return spl[f].ch[] == x;
}
void connect(int x, int f, int s){
spl[f].ch[s] = x;
spl[x].fa = f;
}
void update(int now) {
spl[now].sz = spl[spl[now].ch[]].sz + spl[spl[now].ch[]].sz + spl[now].cnt;
}
void push_down(int now) {
if(!spl[now].lazy) return;
swap(spl[now].ch[], spl[now].ch[]);
spl[spl[now].ch[]].lazy ^= ;
spl[spl[now].ch[]].lazy ^= ;
spl[now].lazy = ;
}
void rotate(int x) {
int f = spl[x].fa, ff = spl[f].fa, k = ident(x, f);
connect(spl[x].ch[k ^ ], f, k);
connect(x, ff, ident(f, ff));
connect(f, x, k ^ );
update(f), update(x);
}
void splaying(int p, int x, int top) {
if(!top) rt[p] = x;
//printf("top = %d\n", top);
while(spl[x].fa != top) {
//if(num <= 5) num++, printf("\n%d %d\n", x, top);
int f = spl[x].fa, ff = spl[f].fa;
if(ff != top) ident(f, ff) ^ ident(x, f) ? rotate(x) : rotate(f);
rotate(x);
}
}
int find_r(int now) {
push_down(now);
while(spl[now].ch[]) {
now = spl[now].ch[];
push_down(now);
}
return now;
}
int find_l(int now) {
push_down(now);
while(spl[now].ch[]) {
now = spl[now].ch[];
push_down(now);
}
return now;
}
void newnode(int now, int val) {
spl[now].val = val;
spl[now].sz = ;
spl[now].cnt = ;
spl[now].ch[] = ;
spl[now].ch[] = ;
spl[now].fa = ;
spl[now].lazy = ;
}
void work(){
for(int i = ; i <= n; i++) {
rt[i] = i;
newnode(i, i);
}
for(int i = ; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
if(!rt[u]) {
if(rt[v]){
rt[u] = rt[v];
spl[rt[u]].lazy ^= ;
rt[v] = ;
}
}
else if(!rt[v]) {
spl[rt[u]].lazy ^= ;
}
else {
int r = find_r(rt[u]);
splaying(u, r, );
//printf("1 \n");
int l = find_l(rt[v]);
//printf("3\n");
splaying(v, l, );
//printf("2\n");
connect(l, r, );
spl[rt[u]].lazy ^= ;
rt[v] = ;
update(rt[u]);
}
}
}
void out(int now){
if(!now) return;
push_down(now);
out(spl[now].ch[]);
printf("%d ", spl[now].val);
out(spl[now].ch[]);
}
int main(){
while(~scanf("%d%d", &n, &m)){
cnt = ;
work();
printf("%d ", spl[rt[]].sz);
if(spl[rt[]].sz) out(rt[]);
printf("\n");
}
return ;
}
/*
999 5
99 8
8 99
99 8
7 99
1 7
*/
/*
999 5
1 3
1 4
1 5
1 6
1 7
*/

最新文章

  1. CSS复习
  2. PHP生成验证码及单实例应用
  3. spring中使用mockito
  4. 分层导航and隐藏导航
  5. background-origin
  6. ascx aspx ashx asmx 文件的作用
  7. 使用ecshop电子商务系统的100个小问题
  8. php 数组操作类(整合 给意见)
  9. python基础教程_学习笔记10:异常
  10. Windows Server 2008 R2防火墙出站规则
  11. Oracle 12cR1 RAC 在VMware Workstation上安装(中)—图形界面安装
  12. CodeForces - 796B 模拟
  13. 如何书写一篇能看懂的html和CSS代码
  14. mysq建表参数设置
  15. 【codeforces 983E】NN country
  16. L2-005 集合相似度 (25 分) (STL——set)
  17. GDI+_绘制QQ头像
  18. string.format格式化字符串中转义大括号“{}”
  19. springCloud学习之服务注册和发现
  20. Confluence 6 文档主题合并问答

热门文章

  1. 【论文排版工具】——LaTeX的安装及使用(MiKTeX+TexStudio+Windows)
  2. 20、Outer Apply 和 Cross Apply
  3. AESUtil
  4. C# 获取系统字体方法
  5. 【面试突击】-Redis常见面试题(一)
  6. 解决vue-cli项目开发中跨域问题
  7. .python3基础之“术语表(1)”
  8. Flink原理、实战与性能优化读书笔记
  9. 基于web站点的xss攻击
  10. MySQL- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column &#39;information_schema.PROFILING.SEQ&#39; which is not functionally dependent on columns in GR