【算法】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. android项目中导入actionbarsherlock 需要注意的地方

    1,在导入actionbarsherlock 这个library时,如果一直报" Invalid Project Description" ;  解决办法:  android中li ...

  2. Ubuntu16.04修改IP

    首先用root用户登陆,然后输入你root的密码.如下图:   然后编辑interfaces文件,该文件位于/etc/network/下,执行如下命令: vim /etc/network/interf ...

  3. Alpha 冲刺4

    队名:日不落战队 安琪(队长) 今天完成的任务 组织第四次站立式会议. 完成40%草稿箱前端界面. 明天的计划 剩下的60%草稿箱前端界面. 如果还有时间,尝试去调用数据. 还剩下的任务 回收站前端界 ...

  4. Jenkins系列-Jenkins构建触发器

    触发器说明 build whenever a snapshot dependency is built,当job依赖的快照版本被build时,执行本job. 触发远程构建 (例如,使用脚本):这里使用 ...

  5. 第63天:json的两种声明方式

    一. json 两种声明方式 1. 对象声明   var  json = {width:100,height:100} 2. 数组声明   var  man = [        //  数组的 js ...

  6. Codeforces Round #522 Div. 1 没打记

    开场被A劝退,写了得有50min于是不敢交了.unrated了喜闻乐见. A:瞎猜都能猜到如果要走到那条直线上,进入直线的点横坐标或纵坐标与起点相同,离开直线的点横坐标或纵坐标与终点相同,证明脑补一下 ...

  7. [洛谷P5068][Ynoi2015]我回来了

    题目大意:给你一张$n(n\leqslant10^3)$个点$m(m\leqslant10^5)$个点的无向无权图,多组询问,每次询问给你一些二元组$(x_i,y_i)$,求有多少个$u$于至少一个二 ...

  8. BZOJ3675 & 洛谷3648 & UOJ104:[Apio2014]序列分割——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3675 https://www.luogu.org/problemnew/show/P3648 ht ...

  9. ContestHunter暑假欢乐赛 SRM 06

    T1二分check...为什么这么显然的我没看出来TAT,还在想倒着加入并查集check什么的,题写太多思维定势啦QAQ T2是NOIP题的弱化版...当时没看出来,写了个DP.可以看出这一位比上一位 ...

  10. Multi-target tracking with Single Moving Camera

    引自:http://www.eecs.umich.edu/vision/mttproject.html Wongun Choi, Caroline Pantofaru, Silvio Savarese ...