Description

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

Input

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n

Output

输出一行n个数字,表示原始序列经过m次变换后的结果

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

好久没写,水一发splay。。。。。。

 const
maxn=;
type
node=record
son:array[..]of longint;
a,fa,size:longint;
sw:boolean;
end;
var
f:array[..maxn]of node;
n,m,root:longint; function build(l,r,ff:longint):longint;
var
mid:longint;
begin
mid:=(l+r)>>;
f[mid].fa:=ff;
f[mid].size:=r-l+;
if mid>l then f[mid].son[]:=build(l,mid-,mid);
if (mid>) and (mid<n+) then f[mid].a:=mid-;
if mid<r then f[mid].son[]:=build(mid+,r,mid);
exit(mid);
end; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure new(x:longint);
begin
with f[x] do
begin
size:=f[son[]].size+f[son[]].size+;
if sw then
begin
swap(son[],son[]);
f[son[]].sw:=not f[son[]].sw;
f[son[]].sw:=not f[son[]].sw;
sw:=not sw;
end;
end;
end; function wh(x:longint):longint;
begin
if f[f[x].fa].son[]=x then exit();
exit();
end; procedure rotate(x,w:longint);
var
y:longint;
begin
y:=f[x].fa;
f[y].son[w]:=f[x].son[w xor ];
if f[x].son[w xor ]<> then f[f[x].son[w xor ]].fa:=y;
f[x].son[w xor ]:=y;
if root=y then root:=x
else f[f[y].fa].son[wh(y)]:=x;
f[x].fa:=f[y].fa;
f[y].fa:=x;
new(y);
end; procedure splay(x,z:longint);
var
y:longint;
begin
while f[x].fa<>z do
begin
y:=f[x].fa;
if f[y].fa<>z then
begin
if wh(x)=wh(y) then rotate(y,wh(y))
else rotate(x,wh(x));
end;
rotate(x,wh(x));
end;
new(x);
end; function find(k:longint):longint;
begin
find:=root;
while true do
begin
new(find);
if k=f[f[find].son[]].size+ then exit(find);
if k<=f[f[find].son[]].size then find:=f[find].son[]
else
begin
dec(k,f[f[find].son[]].size+);
find:=f[find].son[];
end;
end;
end; var
aa:array[..maxn]of longint;
tot:longint; procedure dfs(x:longint);
begin
new(x);
with f[x] do
begin
if son[]<> then dfs(son[]);
inc(tot);
aa[tot]:=a;
if son[]<> then dfs(son[]);
end;
end; procedure main;
var
i,l,r:longint;
begin
read(n,m);
root:=build(,n+,);
for i:= to m do
begin
read(l,r);
splay(find(l),);
splay(find(r+),root);
f[f[f[root].son[]].son[]].sw:=not f[f[f[root].son[]].son[]].sw;
end;
splay(find(),);
splay(find(n+),root);
dfs(f[f[root].son[]].son[]);
for i:= to n- do
write(aa[i],' ');
write(aa[n]);
end; begin
main;
end.

3223: Tyvj 1729 文艺平衡树 - BZOJ的更多相关文章

  1. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

  2. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  3. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

  4. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

  5. 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1347  Solved: 724[Submit][Stat ...

  6. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  7. [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转

    题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...

  8. 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...

  9. bzoj 3223/tyvj 1729 文艺平衡树 splay tree

    原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...

随机推荐

  1. 人情世故&潜规则

    大凡成功的牛人,无一例外都明白这一点.他们读懂了社会的本质和人际交往的潜规则,知道对方需要什么,知道对方脑子里在想什么.你几乎看不见他奔波劳碌,但是在不动声色中,他就已经实现人生目标.他们成功的密码是 ...

  2. ASP.NET,web.config 中SessionState的配置

    web Form 网页是基于HTTP的,它们没有状态, 这意味着它们不知道所有的请求是否来自同一台客户端计算机,网页是受到了破坏,以及是否得到了刷新,这样就可能造成信息的丢失. 于是, 状态管理就成了 ...

  3. swift-自定义无限轮播图

    一  前言 1.之前一直在用OC编程,最近才开始接触使用swift就发现使用OC越来越不习惯,感觉已经爱上了swift. 2.这个自定义轮播图只是对之前OC版本进行了翻译,欢迎指正. 3.我决定一步步 ...

  4. iOS 高阶

    1.UIStoryBoard 2. segue跳转传值 3. UIColor配色 //1. 十进制配色 [UIColor colorWithRed:163.0/255.0 green:148.0/25 ...

  5. Angular实现数据绑定,它实现原理是什么?

    简单的来说,就是给每个需要绑定的元素加上$watcher,缓存下oldValue,然后定时遍历所有的$watcher,比较newValue和oldValue,如果变化了就做更新的操作.

  6. Codevs 1092 不高兴的津津

    时间限制: 1 s   空间限制: 128000 KB   题目等级 : 白银 Silver 题目描述 Description 津津上初中了.妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参 ...

  7. DataGrid实现逻辑分页

    在ASP.NET内建提供的所有数据排列控件中,只有DataGrid数据控件是提供数据分页功能的.DataReapter数据控件只能提供一些简单 的.基础的数据重复排列功能,对于一些比较复杂的应用是无能 ...

  8. 11.find 查找并复制文件

    请把系统上拥有者为ira用户的所有文件,并将其拷贝到/root/findfiles目录中 find /home/ira/ -user ira -exec cp -a {} /root/findfile ...

  9. 转载:Linux内核探索之路——关于书

    转自http://blog.chinaunix.net/uid-20608849-id-3029223.html 在学习Linux内核代码的过程中,定会参考很多书籍以及网路资源,但是并不是所有的书籍和 ...

  10. js javascript:void(0) 真正含义

    我想使用过ajax的都常见这样的代码:<a href="javascript:doTest2();void(0);">here</a>但这儿的void(0) ...