【算法】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键盘IO

    public class IO { public static void main(String[] args) throws Throwable { ScannerTest(); // testSc ...

  2. 一起写一个Android图片轮播控件

    注:本文提到的Android轮播控件Demo地址: Android图片轮播控件 1. 轮播控件的组成部分 我们以知乎日报Android客户端的轮播控件为例,分析一下轮播控件的主要组成: 首先我们要有用 ...

  3. lintcode-148-颜色分类

    148-颜色分类 给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数 0,1 和 2 分别代表红,白,蓝. 注意事项 ...

  4. sublime text 输入法不跟随光标

    1.引子 sublime text 有个BUG,那就是不支持中文的鼠标跟随(和PS类似输入的光标和文字候选框不在一起).如下图: 2.插件 安装IMESupport插件即可插件,这款插件是日本人写的. ...

  5. Activiti5工作流笔记四

    排他网关(ExclusiveGateWay) 流程图 部署流程定义+启动流程实例 查询我的个人任务 完成我的个人任务 并行网关(parallelGateWay) 流程图 部署流程定义+启动流程实例 查 ...

  6. 动态include是通过servlet进行页面信息交互的

    动态include是通过servlet进行页面信息交互的

  7. 第三方框架-纯代码布局:Masonry的简单使用

    Masonry是一个对系统NSLayoutConstraint进行封装的第三方自动布局框架,采用链式编程的方式提供给开发者API.系统AutoLayout支持的操作,Masonry都支持,相比系统AP ...

  8. 转:DP和HDP

    Dirichlet Process and Hierarchical Dirichlet Process 原文:http://hi.baidu.com/zentopus/item/46a622f5ef ...

  9. [洛谷P1440]求m区间内的最小值

    题目大意:给你n个数,求出每个数前m位的最小值 题解:单调队列,用一个可以双向弹出的队列来存一串数,满足里面的数具有单调性,我们可以假设它是单调递增的,即求最小的数.那么可以把要插入的这个数与队尾元素 ...

  10. CF498D:Traffic Jams in the Land——题解

    https://vjudge.net/problem/CodeForces-498D http://codeforces.com/problemset/problem/498/D 题面描述: 一些国家 ...