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 ...
随机推荐
- 如何高效判断java数组是否包含某个值
在java中,我们如何判断一个未排序数组中是否包含一个特定的值?这在java中是一个频繁非常实用的操作.那么什么样的方法才是最高效的方式?当然 ,这个问题在Stack Overflow也是得票率非常高 ...
- UNIX系统的显示时间何时会到尽头
本文转载自:http://www.cnblogs.com/dfcao/p/expertCprogramming_intr0.html 本文分为三个小块: 一.UNIX系统中时间的存储形式: 二. ti ...
- No compatible targets were found Do you wish to a add new Android Virtual Device ?
运行一个Android小程序时提示: No compatible targets were found Do you wish to a add new Android Virtual Device ...
- 论文笔记——Factorized Convolutional Neural Networks
1. 论文思想 将3D卷积分解为spatial convolution in each channel and linear projection across channels. (spatial ...
- 【异常记录(八)】 This operation requires IIS integrated pipeline mode.
突然提示这个Error: Server Error in '/' Application. This operation requires IIS integrated pipeline mode. ...
- codeforces804D Expected diameter of a tree
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 基于MySQl的分页显示
<%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet ...
- c 宏的定义
#include <stdio.h> #include <conio.h> #define VAL 40 #ifdef VAL #undef VAL #endif #defin ...
- Ubuntu16.04 安装 Django
pip2 install django==1.11 或者手动安装: 链接:https://pan.baidu.com/s/1uQJD-pON7gELoCC2TwYnEw 提取码:flgg cd Dja ...
- SQL实现新增表,表名更改,列名更改,约束更改等
--新建表: CREATE TABLE TABLENAME ( ID INT IDENTITY (1,1) PRIMARY KEY , NAME VARCHAR(50) DEFAULT 'HELLO' ...