题解:

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的更多相关文章

  1. POJ 2112 - Optimal Milking

    原题地址:http://poj.org/problem?id=2112 题目大意:有K个挤奶机(标号为1 ~ K)和C头奶牛(编号为K + 1 ~ K + C),以邻接矩阵的方式给出它们两两之间的距离 ...

  2. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

随机推荐

  1. Python3基础 tuple 创建空元组或者只有一个元素的元组 并 用乘法成倍扩充

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. HDU 3974 Assign the task(DFS序)题解

    题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值.给出m个询问. 思路:DFS序,将树改为线性结构,用线段树维护.start[ ]记录每个节点的编号,End[ ]为该节点的最 ...

  3. rabbitmq direct、fanout、topic 三种Exchange java 代码比较

    Producer端 1.channel的创建 无论是才用什么样的Exchange,创建channel代码都是相同的,如下 ConnectionFactory factory = new Connect ...

  4. spring集成mybatis后,打印SQL语句

    网上说mybatis的早前版本配置打印sql还比较简单,在3.0.6之后配置方式修改了. 现在的spring-mybatis.xml配置如下: <bean id="sqlSession ...

  5. 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试

    相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控 用python + hadoop streami ...

  6. Network Simulator for P4(NSP4) src内容介绍

    Structure What's NSP4? src source code introduction What's NSP4? NSP4是一个用于P4的网络仿真工具,旨在简化P4的环境部署和运行,将 ...

  7. LA 3644 易爆物

    https://vjudge.net/problem/UVALive-3644 简单的并查集题目. #include<iostream> using namespace std; + ; ...

  8. python flask demo

    from flask import Flask, jsonify from flask import abort from flask import make_response from flask ...

  9. yaml 文件保存

    with open(mpath, "w") as f: yaml.safe_dump(yaml_dict,f,encoding='utf-8', allow_unicode=Tru ...

  10. python 获取字符串中所有数字

    s = "dede323frf54de23" l = len(s) numbers = [] i = 0 while i < l: num = '' symbol = s[i ...