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 ...
随机推荐
- FJNUOJ the greed of Yehan(最长路 + 权值乘积转化)题解
Description During the trip, Yehan and Linlin pass a cave, and there is a board at the door, which s ...
- C# 实现简单的 Heap 堆(二叉堆)
如题,C# 实现简单的二叉堆的 Push() 和 Pop(), 如有不足欢迎指正. 另外,在C#中使用 Heap 的相似功能可以考虑使用:Priority Queues,SortedDictiona ...
- 非[无]root权限 服务器 下安装perl以及perl模块--转载
转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无root权限.理论上适合所有的用户. 我的安装目录 cd /home/zilhua/software 1. ...
- Python time模块详解--转载
1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平台可能有所 ...
- c++ 判断数组元素是否有负数(any_of)
#include <iostream> // std::cout #include <algorithm> // std::any_of #include <array& ...
- Java 面向对象之static,final,匿名对象,内部类,包,修饰符
01final关键字概念 A: 概述 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承, 或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这 ...
- 代码中特殊的注释技术 -- TODO、FIXME和XXX的用处
借鉴地址:https://www.cnblogs.com/pengyingh/articles/2445826.html 在阅读一些代码时,经常会遇到诸如:TODO.FIXME和XXX的单词,它们是有 ...
- 《剑指offer》第三十二题(之字形打印二叉树)
// 面试题32(三):之字形打印二叉树 // 题目:请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺 // 序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印, / ...
- jdk1.8
Jdk1.8新特性 毫无疑问,Java 8是Java自Java 5(发布于2004年)之后的最重要的版本.这个版本包含语言.编译器.库.工具和JVM等方面的十多个新特性.在本文中我们将学习这些新特性, ...
- Codeforces B - Tavas and SaDDas
535B - Tavas and SaDDas 方法一:打表大法. 代码1: #include<bits/stdc++.h> using namespace std; ]={,,,,,,, ...