BZOJ1552/3506 [Cerc2007]robotic sort
Splay
与之前不同的是如果你仅仅是翻转左右区间的话可以在find里面做因为对他有影响的子树在做之前一定在他的上面从上到下搜索的过程可以把rever做了。
但这道题要求我们输出转换之前的,因此不能保证之前的rev标记都已执行完因此就要从上到下做一遍。
By:大奕哥
- #include<bits/stdc++.h>
- using namespace std;
- const int N=1e5+;
- int fa[N],c[N][],pos[N],rev[N],mn[N],w[N],size[N],n,m,rt;
- struct node
- {
- int pos,v;
- bool operator<(const node &b)const {
- if(v==b.v)return pos<b.pos;
- return v<b.v;
- }
- }a[N];
- void pushdown(int x)
- {
- if(rev[x])rev[c[x][]]^=,rev[c[x][]]^=,rev[x]^=,swap(c[x][],c[x][]);
- mn[x]=w[x];pos[x]=x;
- if(mn[c[x][]]<mn[x])mn[x]=mn[c[x][]],pos[x]=pos[c[x][]];
- if(mn[c[x][]]<mn[x])mn[x]=mn[c[x][]],pos[x]=pos[c[x][]];
- size[x]=size[c[x][]]+size[c[x][]]+;
- }
- void rotate(int x,int &k)
- {
- int y=fa[x],z=fa[y];
- int l=(c[y][]==x);int r=l^;
- if(y==k)k=x;else c[z][c[z][]==y]=x;
- fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
- c[y][l]=c[x][r];c[x][r]=y;
- pushdown(y);pushdown(x);
- }
- int s[N],top;
- void splay(int x,int &k)
- {
- top=;
- for(int i=x;i;i=fa[i])s[++top]=i;
- for(int i=top;i;--i)pushdown(s[i]);
- while(x!=k)
- {
- int y=fa[x],z=fa[y];
- if(y!=k)
- {
- if(x==c[y][]^y==c[z][])rotate(x,k);
- else rotate(y,k);
- }
- rotate(x,k);
- }
- }
- int find(int x,int k)
- {
- if(!x)return ;pushdown(x);
- if(size[c[x][]]+==k)return x;
- else if(size[c[x][]]+>k)return find(c[x][],k);
- else return find(c[x][],k-size[c[x][]]-);
- }
- int query(int l,int r)
- {
- int x=find(rt,l),y=find(rt,r+);
- splay(x,rt);splay(y,c[x][]);
- return pos[c[y][]];
- }
- void rever(int l,int r)
- {
- int x=find(rt,l),y=find(rt,r+);
- splay(x,rt);splay(y,c[x][]);
- int z=c[y][];
- rev[z]^=;
- return;
- }
- void build(int l,int r,int f){
- if(l>r)return;
- int mid=l+r>>;
- fa[mid]=f;c[f][mid>=f]=mid;
- w[mid]=mn[mid]=a[mid].v;
- if(l==r)
- {
- rev[mid]=;size[mid]=;pushdown(mid);return;
- }
- build(l,mid-,mid);build(mid+,r,mid);
- pushdown(mid);
- return;
- }
- bool cmp(node aa,node bb){
- return aa.pos<bb.pos;
- }
- int ans[N];
- int main()
- {
- scanf("%d",&n);
- build(,n+,);rt=(n+)>>;mn[]=a[].v=a[n+].v=1e9;
- for(int i=;i<=n+;++i)scanf("%d",&a[i].v);
- for(int i=;i<=n;++i)a[i+].pos=i;
- sort(a+,a+n+);
- for(int i=;i<=n;++i)a[i+].v=i;
- sort(a+,a+n+,cmp);
- build(,n+,);
- rt=(n+)>>;
- for(int i=;i<=n;++i)
- {
- int x=query(i,n);
- splay(x,rt);
- ans[i]=size[c[x][]];
- rever(i,ans[i]);
- }
- for(int i=;i<n;++i)
- {
- printf("%d ",ans[i]);
- }
- printf("%d",ans[n]);
- return ;
- }
这个是标准的按照序列中的位置建树,每次splay之前要query一下。跑了3000ms,后来又在网上找了一种写法,直接按照权值建图,查找就直接splay对应的标号即可,2000ms。
- #include<bits/stdc++.h>
- using namespace std;
- const int N=1e5+;
- int fa[N],c[N][],pos[N],rev[N],mn[N],w[N],size[N],n,m,rt;
- struct node
- {
- int pos,v;
- bool operator<(const node &b)const {
- if(v==b.v)return pos<b.pos;
- return v<b.v;
- }
- }a[N];
- bool cmp(node aa,node bb){
- return aa.pos<bb.pos;
- }
- void pushdown(int x)
- {
- if(rev[x])rev[c[x][]]^=,rev[c[x][]]^=,rev[x]^=,swap(c[x][],c[x][]);
- mn[x]=w[x];pos[x]=x;
- if(mn[c[x][]]<mn[x])mn[x]=mn[c[x][]],pos[x]=pos[c[x][]];
- if(mn[c[x][]]<mn[x])mn[x]=mn[c[x][]],pos[x]=pos[c[x][]];
- size[x]=size[c[x][]]+size[c[x][]]+;
- }
- void rotate(int x,int &k)
- {
- int y=fa[x],z=fa[y];
- int l=(c[y][]==x);int r=l^;
- if(y==k)k=x;else c[z][c[z][]==y]=x;
- fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
- c[y][l]=c[x][r];c[x][r]=y;
- pushdown(y);pushdown(x);
- }
- int s[N],top;
- void splay(int x,int &k)
- {
- top=;
- for(int i=x;i;i=fa[i])s[++top]=i;
- for(int i=top;i;--i)pushdown(s[i]);
- while(x!=k)
- {
- int y=fa[x],z=fa[y];
- if(y!=k)
- {
- if(x==c[y][]^y==c[z][])rotate(x,k);
- else rotate(y,k);
- }
- rotate(x,k);
- }
- }
- int find(int x,int k)
- {
- if(!x)return ;pushdown(x);
- if(size[c[x][]]+==k)return x;
- else if(size[c[x][]]+>k)return find(c[x][],k);
- else return find(c[x][],k-size[c[x][]]-);
- }
- void rever(int l,int r)
- {
- int x=find(rt,l),y=find(rt,r+);
- splay(x,rt);splay(y,c[x][]);
- int z=c[y][];
- rev[z]^=;
- return;
- }
- void build(int l,int r,int f){
- if(l>r)return;
- int mid=l+r>>;
- int now=a[mid].v,last=a[f].v;
- fa[now]=last;c[last][mid>=f]=now;
- if(l==r)
- {
- rev[now]=;size[now]=;return;
- }
- build(l,mid-,mid);build(mid+,r,mid);
- pushdown(now);
- return;
- }
- int ans[N];
- int main()
- {
- scanf("%d",&n);
- rt=(n+)>>;mn[]=1e9;
- for(int i=;i<=n+;++i)scanf("%d",&a[i].v);
- for(int i=;i<=n;++i)a[i+].pos=i;
- sort(a+,a+n+);
- for(int i=;i<=n+;++i)a[i].v=i;a[].v=;a[n+].v=n+;
- sort(a+,a+n+,cmp);
- build(,n+,);
- rt=a[(n+)>>].v;
- for(int i=;i<=n;++i)
- {
- splay(i+,rt);
- ans[i]=size[c[rt][]];
- rever(i,ans[i]);
- }
- for(int i=;i<n;++i)
- {
- printf("%d ",ans[i]);
- }
- printf("%d",ans[n]);
- return ;
- }
BZOJ1552/3506 [Cerc2007]robotic sort的更多相关文章
- 洛谷 P4402 BZOJ1552 / 3506 [Cerc2007]robotic sort 机械排序
FHQ_Treap 太神辣 蒟蒻初学FHQ_Treap,于是来到了这道略显板子的题目 因为Treap既满足BST的性质,又满足Heap的性质,所以,对于这道题目,我们可以将以往随机出的额外权值转化为每 ...
- 【BZOJ1552】[Cerc2007]robotic sort Splay
[BZOJ1552][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N ...
- 【BZOJ-1552&3506】robotic sort&排序机械臂 Splay
1552: [Cerc2007]robotic sort Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 806 Solved: 329[Submit][ ...
- 【bzoj1552】[Cerc2007]robotic sort
题目描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. 输出 输出共一行,N个用空格隔开的 ...
- 【BZOJ】1552/3506 [Cerc2007]robotic sort
[算法]splay [题解] splay维护序列,用权值(离散化)作为编号.每次找第i小的话直接找对应编号splay即可. 但是这样splay没有下传翻转标记?直接暴力找到路径然后从根到改结点push ...
- 【bzoj1552/3506】[Cerc2007]robotic sort splay翻转,区间最值
[bzoj1552/3506][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. ...
- [BZOJ1552][Cerc2007]robotic sort
[BZOJ1552][Cerc2007]robotic sort 试题描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数 ...
- BZOJ 1552: [Cerc2007]robotic sort( splay )
kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...
- bzoj 1552: [Cerc2007]robotic sort
1552: [Cerc2007]robotic sort Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1198 Solved: 457[Submit] ...
随机推荐
- [acmm week12]二分+dp+单调队列
1004 抄作业 Time Limit: 1sec Memory Limit:256MB Description Zfree虽然平时很爱学习,但是他迫于生活所迫(比如设计cpu实 ...
- ES6 中 Array.from() 不能得到的数组元素是 undefined 或是空数组
本文地址:http://www.cnblogs.com/veinyin/p/7944072.html 正确格式 let json = { '0': 'Waaaa~', '1': 'Hello,', ...
- HDU 5914 Triangle 斐波纳契数列 && 二进制切金条
HDU5914 题目链接 题意:有n根长度从1到n的木棒,问最少拿走多少根,使得剩下的木棒无论怎样都不能构成三角形. 题解:斐波纳契数列,a+b=c恰好不能构成三角形,暴力就好,推一下也可以. #in ...
- 2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- 在mac上安装ruby
1.先装RVM,一个多版本ruby环境的管理和切换工具 curl -sSL https://get.rvm.io | bash -s stable 会自动装上最新版.更新RVM版本:$ rvm get ...
- Django之ModelForm(二)-----ModelForm组件
a. class Meta: model, # 对应Model的 fields=None, ...
- How to read source code[repost]
https://github.com/benjycui/benjycui.github.io/blob/master/posts/how-to-read-open-source-javascript- ...
- Tornado/Python 学习笔记(一)
1.源代码下载及安装:http://www.tornadoweb.org/en/stable/ 2.python中xmlrpc库官方文档:https://docs.python.org/3/libra ...
- skb_reserve(skb,2)中的2的意义
skb_reserve() skb_reserve()在数据缓存区头部预留一定的空间,通常被用来在数据缓存区中插入协议首部或者在某个边界上对齐.它并没有把数据移出或移入数据缓存区,而只是简单地更新了数 ...
- Activity工作流 -- java运用
一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...