【算法】splay

【题解】

splay维护序列,用权值(离散化)作为编号。每次找第i小的话直接找对应编号splay即可。

但是这样splay没有下传翻转标记?直接暴力找到路径然后从根到改结点pushdown。暴力出奇迹!

如果没有find就直接splay,一定记得更新设置splay为传值调用并且在过程中更新地址才能更新root。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
const int maxn=;
int f[maxn],t[maxn][],s[maxn],first[maxn],second[maxn],ord[maxn],a[maxn],node[maxn],n,root,ans[maxn];
bool g[maxn];
int read()
{
char c;int s=;
while(!isdigit(c=getchar()));
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s;
}
bool cmp(int x,int y)
{return (first[x]<first[y])||(first[x]==first[y]&&second[x]<second[y]);}
void pushdown(int x)
{
if(g[x])
{
g[t[x][]]^=;g[t[x][]]^=;
swap(t[x][],t[x][]);
g[x]=;
}
}
void count(int x)
{s[x]=s[t[x][]]++s[t[x][]];}
void rotate(int x)
{
int k=x==t[f[x]][];
int y=f[x];
t[y][k]=t[x][!k];f[t[x][!k]]=y;
if(f[y])t[f[y]][y==t[f[y]][]]=x;f[x]=f[y];f[y]=x;
t[x][!k]=y;s[x]=s[y];//因为已经pushdown了所以不用传翻转标记
count(y);
}
void splay(int &r,int x)
{
int tot=;
for(int i=x;i!=r;i=f[i])node[++tot]=i;node[++tot]=r;
for(int i=tot;i>=;i--)pushdown(node[i]);
for(int fa=f[r];f[x]!=fa;)
{
if(f[f[x]]==fa){rotate(x);break;}//return之前要记得传参……所以还是break好……
int X=x==t[f[x]][],Y=f[x]==t[f[f[x]]][];
if(X^Y)rotate(x),rotate(x);
else rotate(f[x]),rotate(x);
}
r=x;
}
void find(int &r,int k)
{
for(int x=r;x;)
{
pushdown(x);
if(k<=s[t[x][]]){x=t[x][];continue;}
if(k==s[t[x][]]+){splay(r,x);return;}
k-=s[t[x][]]+;x=t[x][];
}
}
void build(int fa,int &x,int l,int r)
{
if(l>r)return;
int mid=(l+r)>>;
x=a[mid];f[x]=fa;g[x]=;s[x]=;
build(x,t[x][],l,mid-);
build(x,t[x][],mid+,r);
count(x);
}
//void writes()
//{
// printf("------------------------------------------\n");
// for(int i=0;i<=n+2;i++)printf("[%d]t1=%d t2=%d fa=%d g=%d s=%d\n",i,t[i][0],t[i][1],f[i],g[i],s[i]);
// printf("------------------------------------------\n");
//}
int main()
{
n=read();
for(int i=;i<=n;i++)
{
first[i]=read();
second[i]=i;
ord[i]=i;
}
sort(ord+,ord+n+,cmp);
for(int i=;i<=n;i++)a[ord[i]]=i;
a[]=n+;a[n+]=n+;
build(,root,,n+);
for(int i=;i<=n;i++)
{
splay(root,i);
int p=s[t[root][]];
ans[i]=p;
find(root,i);
find(t[root][],p-i+);
g[t[t[root][]][]]^=;
}
for(int i=;i<n;i++)printf("%d ",ans[i]);
printf("%d",ans[n]);
return ;
}

update:现在已改用fhq-treap代替splay。

【BZOJ】1552/3506 [Cerc2007]robotic sort的更多相关文章

  1. BZOJ 1552/1506 [Cerc2007]robotic sort

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1552 [分析] 这题哇!又有翻转操作...每次要输出第几个?是吧... 所以又要用Spla ...

  2. 洛谷 P4402 BZOJ1552 / 3506 [Cerc2007]robotic sort 机械排序

    FHQ_Treap 太神辣 蒟蒻初学FHQ_Treap,于是来到了这道略显板子的题目 因为Treap既满足BST的性质,又满足Heap的性质,所以,对于这道题目,我们可以将以往随机出的额外权值转化为每 ...

  3. BZOJ1552/3506 [Cerc2007]robotic sort

    Splay 与之前不同的是如果你仅仅是翻转左右区间的话可以在find里面做因为对他有影响的子树在做之前一定在他的上面从上到下搜索的过程可以把rever做了. 但这道题要求我们输出转换之前的,因此不能保 ...

  4. 【BZOJ】【1552】【Cerc2007】robotic sort / 【3506】【CQOI2014】排序机械臂

    Splay 离散化+Splay维护序列…… 好吧主要说一下我做这道题遇到的几个错误点: 1.离散化 2.由于找到的这个数的位置一定是大于等于 i 的,所以其实在把它splay到根以后,i 结点只能sp ...

  5. 【bzoj1552/3506】[Cerc2007]robotic sort splay翻转,区间最值

    [bzoj1552/3506][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. ...

  6. BZOJ 1552: [Cerc2007]robotic sort( splay )

    kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...

  7. 【BZOJ1552】[Cerc2007]robotic sort Splay

    [BZOJ1552][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N ...

  8. bzoj 1552: [Cerc2007]robotic sort

    1552: [Cerc2007]robotic sort Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1198  Solved: 457[Submit] ...

  9. 【BZOJ】3052: [wc2013]糖果公园

    http://www.lydsy.com/JudgeOnline/problem.php?id=3052 题意:n个带颜色的点(m种),q次询问,每次询问x到y的路径上sum{w[次数]*v[颜色]} ...

随机推荐

  1. java 基础 --匿名内部类-008

    不全代码 interface Inter(){void show();} class Outer{补全代码} class OuterDemo{ public static void main(Stri ...

  2. Python文件操作大全,随机删除文件夹内的任意文件

     在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法: os.path.abspath(path) #返回绝对路径os.path.basename(path ...

  3. Bootstrap排版类

    类 描述 实例 .lead 使段落突出显示 尝试一下 .small 设定小文本 (设置为父文本的 85% 大小) 尝试一下 .text-left 设定文本左对齐 尝试一下 .text-center 设 ...

  4. Swift学习与复习

    swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...

  5. 【bzoj1707】[Usaco2007 Nov]tanning分配防晒霜 贪心+Treap

    题目描述 奶牛们计划着去海滩上享受日光浴.为了避免皮肤被阳光灼伤,所有C(1 <= C <= 2500)头奶牛必须在出门之前在身上抹防晒霜.第i头奶牛适合的最小和最 大的SPF值分别为mi ...

  6. 透彻掌握Promise的使用,读这篇就够了

    透彻掌握Promise的使用,读这篇就够了 Promise的重要性我认为我没有必要多讲,概括起来说就是必须得掌握,而且还要掌握透彻.这篇文章的开头,主要跟大家分析一下,为什么会有Promise出现. ...

  7. springboot2.0 快速集成kafka

    一.kafka搭建 参照<kafka搭建笔记> 二.版本 springboot版本 <parent> <groupId>org.springframework.bo ...

  8. HDU 1002 (高精度加法运算)

    A + B ProblemII Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. Ark组件[转]

    Ark组件简介 Ark组件是基于.NET 4.0框架开发的基础组件,封装了一些常用的功能方法,并提供了若干程序开发的基础框架. HttpSession简介 HttpSession是Ark组件中负责HT ...

  10. vue中使用 contenteditable 制作输入框并使用v-model做双向绑定

    <template> <div class="div-input" :class="value.length > 0 ? 'placeholder ...