http://codevs.cn/problem/1743/

splay区间翻转。

数字在原序列中的位置保存在splay的data[]中。splay中点的编号为原序列的数字大小。

每次pushdown只在find时进行就行,因为find结束时splay只会改动i点祖先的信息,而其余时候没有splay操作了。

记得加个+INF 一个-INF。

 #include<cstdio>
#include<algorithm>
using namespace std;
#define N 400000
#define INF 2000000000
int lazy[N],s[N][],f[N],cnt[N],data[N],root,total;//son:1left 2right
int fi,num,p,q,le[N],m,n,i,j,ans;
char opt;
struct lbz
{
int a,b;
}x[N];
bool cmp(lbz a,lbz b)
{
return a.a<b.a;
}
void rotate(int x,int w)//w:1leftZag 2rightZig
{
int y;
y=f[x];
cnt[y]=cnt[y]-cnt[x]+cnt[s[x][w]];
cnt[x]=cnt[x]-cnt[s[x][w]]+cnt[y];
s[y][-w]=s[x][w];
if (s[x][w]!=)
f[s[x][w]]=y;
f[x]=f[y];
if (f[y]!=)
if (y==s[f[y]][])
s[f[y]][]=x;
else
s[f[y]][]=x;
f[y]=x;
s[x][w]=y;
}
void splay(int x,int t)
{
int y;
while (f[x]!=t)
{
y=f[x];
if (f[y]==t)
if (x==s[y][])
rotate(x,);
else
rotate(x,);
else
if (y==s[f[y]][])
if (x==s[y][])
{
rotate(y,);
rotate(x,);
}
else
{
rotate(x,);
rotate(x,);
}
else
if (x==s[y][])
{
rotate(y,);
rotate(x,);
}
else
{
rotate(x,);
rotate(x,);
}
}
if (f[x]==) root=x;
} int sea(int x,int w)
{
int t;
t=x;
while ()
{
if (data[t]==w) break;
if (w<=data[t])
{
if (s[t][]==) break;
t=s[t][];
}
else
{
if (s[t][]==) break;
t=s[t][];
} }
splay(t,);
return t;
}
void add(int w)
{
int x;
if (total==)
{
total++;
f[]=;cnt[]=;data[]=w;root=;
return;
}
x=root;
while()
{
cnt[x]++;
if (w<=data[x])
{
if (s[x][]==) break;
x=s[x][];
}
else
{
if (s[x][]==) break;
x=s[x][];
}
}
total++;
data[total]=w;
f[total]=x;
cnt[total]=;
if (w<=data[x])
s[x][]=total;
else
s[x][]=total;
splay(total,);
} void pushdown(int x)
{
lazy[x]^=;
lazy[s[x][]]^=;
lazy[s[x][]]^=;
swap(s[x][],s[x][]);
}
int find(int k,int w)//w=1 Kthsmall;w=2 Kthbig
{
int i,t;
i=root;t=k;
if (lazy[i]==)
pushdown(i); while (t!=cnt[s[i][w]]+)
{ if (t>cnt[s[i][w]]+)
{
t-=cnt[s[i][w]]+;
i=s[i][-w];
}
else
i=s[i][w];
if (lazy[i]==)
pushdown(i);
}
splay(i,);
return i;
} int rec(int l,int r)
{
int ll,rr;
ll=find(l,);
rr=find(r+,);
splay(ll,);
splay(rr,ll);
lazy[s[rr][]]^=;
}
int main()
{
scanf("%d",&n);
for (i=;i<=n;i++)
{
scanf("%d",&x[i].a);
x[i].b=i;
}
sort(x+,x++n,cmp);
for (i=;i<=n;i++)
add(x[i].b);
add();
add(-);
while ()
{
fi=find(,);
if (fi==) break;
rec(,fi);
ans++;
}
printf("%d\n",ans);
return ;
}

codevs1743的更多相关文章

  1. [codevs1743]反转卡片

    [codevs1743]反转卡片 试题描述 [dzy493941464|yywyzdzr原创] 小A将N张卡片整齐地排成一排,其中每张卡片上写了1~N的一个整数,每张卡片上的数各不相同. 比如下图是N ...

  2. 【codevs1743】 反转卡片

    http://codevs.cn/problem/1743/ (题目链接) 题意 给出一个序列{a1,a2,a3···},要求维护这样一种操作:将前a1个数反转,若第a1等于1,则停止操作. Solu ...

  3. 【集训第四天·继续刷题】之 lgh怒刚ypj

    继续水题,终于完全掌握了伸展树了,好心痛QAQ. 1.codevs1343 蚱蜢 区间最大值查询+单点移动 最大值查询维护一个mx数组就行,单点移动么,先删除在插入 CODE: /* PS: 比较ma ...

  4. codevs 1743 反转卡片 rope or splay

    [codevs1743]反转卡片 题目描述 Description [dzy493941464|yywyzdzr原创] 小A将N张卡片整齐地排成一排,其中每张卡片上写了1~N的一个整数,每张卡片上的数 ...

随机推荐

  1. the Uneducated are|anymore|that| so as to |die from|die of|

    定冠词加上某些形容词可以泛指一类人,谓语动词一般用复数形式,the uneducated泛指未受过教育的人, the Uneducated are more to be pitied than bla ...

  2. make的工作方式

    摘自<跟我一起写Makefile> GUN的make工作时的执行步骤如下: 1)读入所有的Makefile. 2)读入被include的其他Makeifle. 3)初始化文件中的变量. 4 ...

  3. 设置Fiddler来抓取Android接口数据

    1.下载安装fiddler,安装包可自行百度.安装完成打开fiddler 2.将Fiddler设置远程访问PC 选择Fiddler->Tools->Fiddler Option 3.选择C ...

  4. Esp8266和HomeKit

    Summary 没有找到合适的简单解决方案,将Esp8266控制的设备连接到HomeKit.所以参照EspEasy实现 HomeKit和Esp8266连接. 连接方式: Raspberry Zero ...

  5. unique()函数使用

    前提:要先令容器有序. unique的作用是“去掉”容器中相邻元素的重复元素(不一定要求数组有序),它会把重复的元素添加到容器末尾(所以数组大小并没有改变),而返回值是去重之后的尾地址. 用法:uni ...

  6. JavaScript中对象数组去重方法

    在一次对后端返回的对象数组的操作时想通过indexOf()或者includes()的方法来实现对对象数组的去重但是行不通,因为用indexOf()返回的都是-1,一下记录两种对象数组(更具指定属性)去 ...

  7. Windows下使用swoole的环境搭建

    Cygwin 官方地址:http://www.cygwin.com/ swoole 官方下载地址:https://github.com/swoole/swoole-src/releases 方法/步骤 ...

  8. 餐厅随评系列之四:Umu日本料理(米其林二星)

    文章目录 在过去的几个月,工作和生活都极其忙碌,因此博客短暂停更了一阵子.慢慢积累下了很多素材,从近期开始恢复博客更新,不过很多内容估计得靠回忆了. 索性采取"倒叙"的方法,先从最 ...

  9. FPGA小白学习之路(2)error:buffers of the same direction cannot be placed in series

    锁相环PLL默认输入前端有个IBUFG单元,在输出端有个BUFG单元,而两个BUFG(IBUFG)不能相连,所以会报这样的错: ERROR:NgdBuild:770 - IBUFG 'u_pll0/c ...

  10. [dubbo 源码之 ]2. 服务消费方如何启动服务

    启动流程 消费者在启动之后,会通过ReferenceConfig#get()来生成远程调用代理类.在get方法中,会启动一系列调用函数,我们来一个个解析. 配置同样包含2种: XML <?xml ...