sgu187&&spoj7734
题解:
splay翻转(只有翻转
sgu ac,spoj tle
代码:
#pragma GCC optimize(2)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int n,m,sz,rt,pre[N],cas,l,r,c[N][],alen,data[N],size[N],rev[N];
void pushup(int k)
{
size[k]=size[c[k][]]+size[c[k][]]+;
}
void pushdown(int k)
{
int l=c[k][],r=c[k][];
if(rev[k])
{
swap(c[k][],c[k][]);
rev[l]^=;rev[r]^=;
rev[k]=;
}
}
void rotate(int x,int &k)
{
int y=pre[x],z=pre[y],l,r;
if(c[y][]==x)l=;
else l=;
r=l^;
if(y==k)k=x;
else {if(c[z][]==y)c[z][]=x;else c[z][]=x;}
pre[x]=z;pre[y]=x;pre[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
pushup(y);pushup(x);
}
void splay(int x,int &k)
{
while(x!=k)
{
int y=pre[x],z=pre[y];
if(y!=k)
{
if(c[y][]==x^c[z][]==y)rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int find(int k,int rank)
{
pushdown(k);
int l=c[k][],r=c[k][];
if (size[l]+==rank)return k;
else if (size[l]>=rank)return find(l,rank);
else return find(r,rank-size[l]-);
}
void change(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]^=;
}
void build(int l,int r,int f)
{
if(l>r)return;
int now=data[l],last=data[f];
if(l==r)
{
pre[now]=last;size[now]=;
if(l<f)c[last][]=now;
else c[last][]=now;
return;
}
int mid=(l+r)>>;now=data[mid];
build(l,mid-,mid);
build(mid+,r,mid);
pre[now]=last;pushup(mid);
if(mid<f)c[last][]=now;
else c[last][]=now;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n+;i++)data[i]=++sz;
build(,n+,);rt=(n+)>>;
while (m--)
{
scanf("%d%d",&l,&r);
if (l!=r)change(l,r);
}
for(int i=;i<=n+;i++)printf("%d\n",find(rt,i)-);
}
sgu187&&spoj7734的更多相关文章
- POJ 2112 - Optimal Milking
原题地址:http://poj.org/problem?id=2112 题目大意:有K个挤奶机(标号为1 ~ K)和C头奶牛(编号为K + 1 ~ K + C),以邻接矩阵的方式给出它们两两之间的距离 ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
随机推荐
- C++ shared_ptr的用法
一. http://www.cnblogs.com/welkinwalker/archive/2011/10/20/2218804.html 二.http://www.cnblogs.com/Tian ...
- 0xc0000005:读取位置时发生访问冲突
这是空指针,比如: A* a=NULL; a->fun();//会提示标题错误,因为a没有分配空间
- linux下有线网卡出现ADDRCONF(NETDEV_UP): eth0: link is not ready的解决方法
一.背景 2018年5月24日,笔者的pc已经连续运转两天了,突然要使用有线网卡,却发现有线网卡无法正常工作,于是查看了一下内核日志: r8169 0000:05:00.0 eth0: link do ...
- Spyder clear variable explorer from memory
https://stackoverflow.com/questions/45853595/spyder-clear-variable-explorer-along-with-variables-fro ...
- HDU 3404 Switch lights(Nim积)题解
题意:在一个二维平面中,有n个灯亮着并告诉你坐标,每回合需要找到一个矩形,这个矩形xy坐标最大的那个角落的点必须是亮着的灯,然后我们把四个角落的灯状态反转,不能操作为败 思路:二维Nim积,看不懂啊, ...
- react 为元素添加自定义事件监听器
https://zhenyong.github.io/react/tips/dom-event-listeners.html class MovieItem extends React.Compone ...
- 【ssh免登录】设置集群环境ssh免登录步骤
1.每台机器都需要执行,生成自己的密钥 # ssh-keygen -t rsa 过程中遇到选项,全部enter #cd ~/.ssh # cat id_rsa.pub > authorized_ ...
- animate动画回调函数
对非动画的实现排队,比如这个css()是要放在回调函数里才能,让前面的动画执行完成后在进行实现 $('button').click(function(event) { $(this).next().a ...
- Redis Cluster集群
一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用无中心 ...
- 51nod 1445 变色DNA(最短路变形)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445 题意: 思路: 挺好的一道题目,如果$colormap[i][j] ...