平衡树

6个操作做完当然GG了,其实只有两个操作,翻转[A+1,A+B],把这个区间放到C的后面,那么就是基本splay操作了,可是好久没打,又GG了,splay函数写错了。。。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + ;
namespace IO
{
const int Maxlen = N * ;
char buf[Maxlen], *C = buf;
int Len;
inline void read_in()
{
Len = fread(C, , Maxlen, stdin);
buf[Len] = '\0';
}
inline void fread(int &x)
{
x = ;
int f = ;
while (*C < '' || '' < *C) { if(*C == '-') f = -; ++C; }
while ('' <= *C && *C <= '') x = (x << ) + (x << ) + *C - '', ++C;
x *= f;
}
inline void fread(long long &x)
{
x = ;
long long f = ;
while (*C < '' || '' < *C) { if(*C == '-') f = -; ++C; }
while ('' <= *C && *C <= '') x = (x << ) + (x << ) + *C - '', ++C;
x *= f;
}
inline void read(int &x)
{
x = ;
int f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = (x << ) + (x << ) + c - ''; c = getchar(); }
x *= f;
}
inline void read(long long &x)
{
x = ;
long long f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = (x << 1ll) + (x << 3ll) + c - ''; c = getchar(); }
x *= f;
}
} using namespace IO;
struct node {
int f, rev, sz;
int ch[];
} t[N];
int n, m, root;
int ans[N], size[N];
void update(int x)
{
t[x].sz = t[t[x].ch[]].sz + t[t[x].ch[]].sz + ;
}
bool wh(int x)
{
return x == t[t[x].f].ch[];
}
void pushdown(int x)
{
if(!t[x].rev) return;
t[t[x].ch[]].rev ^= ;
t[t[x].ch[]].rev ^= ;
swap(t[x].ch[], t[x].ch[]);
t[x].rev = ;
}
void up(int x)
{
if(t[x].f) up(t[x].f);
pushdown(x);
}
int build(int l, int r, int f)
{
if(l > r) return ;
int mid = (l + r) >> ;
t[mid].f = f;
t[mid].ch[] = build(l, mid - , mid);
t[mid].ch[] = build(mid + , r, mid);
update(mid);
return mid;
}
void rotate(int x)
{
int f = t[x].f, w = wh(x);
t[x].f = t[f].f;
t[t[f].f].ch[wh(f)] = x;
t[f].ch[w] = t[x].ch[w ^ ];
t[t[x].ch[w ^ ]].f = f;
t[x].ch[w ^ ] = f;
t[f].f = x;
update(f);
update(x);
}
inline void splay(int x, int tar)
{
for(; t[x].f != tar; rotate(x))
if(t[t[x].f].f != tar)
rotate(wh(x) == wh(t[x].f) ? t[x].f : x);
if(!tar) root = x;
}
int find(int x, int k)
{
pushdown(x);
if(t[t[x].ch[]].sz + == k) return x;
if(k <= t[t[x].ch[]].sz) return find(t[x].ch[], k);
return find(t[x].ch[], k - t[t[x].ch[]].sz - );
}
int split(int l, int r)
{
int x = find(root, l - ), y = find(root, r + );
splay(x, );
splay(y, root);
return y;
}
void dfs(int u)
{
if(!u) return;
pushdown(u);
dfs(t[u].ch[]);
ans[++ans[]] = u;
dfs(t[u].ch[]);
}
int main()
{
read_in();
fread(n);
fread(m);
root = build(, n + , );
while(m--)
{
int A, B, C;
fread(A);
fread(B);
fread(C);
int x = split(A + , A + B + ), y = t[x].ch[];
t[y].rev ^= ;
t[y].f = t[x].ch[] = ;
update(x);
update(t[x].f);
int z = split(C + , C + );
t[y].f = z;
t[z].ch[] = y;
update(z);
update(t[z].f);
}
dfs(root);
for(int i = ; i < ans[]; ++i) printf("%d ", ans[i] - );
return ;
}

bzoj5063的更多相关文章

  1. 【BZOJ5063】旅游 Splay

    [BZOJ5063]旅游 Description 小奇成功打开了大科学家的电脑. 大科学家打算前往n处景点旅游,他用一个序列来维护它们之间的顺序.初 始时,序列为1,2,...,n. 接着,大科学家进 ...

  2. BZOJ5063旅游——非旋转treap

    题目描述 小奇成功打开了大科学家的电脑. 大科学家打算前往n处景点旅游,他用一个序列来维护它们之间的顺序.初 始时,序列为1,2,...,n. 接着,大科学家进行m次操作来打乱顺序.每次操作有6步: ...

随机推荐

  1. 实验十二 swing图形界面设计

    1.源程序 package information;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event ...

  2. 【Java TCP/IP Socket】深入剖析socket——TCP通信中由于底层队列填满而造成的死锁问题(含代码)

    基础准备 首先需要明白数据传输的底层实现机制,在http://blog.csdn.net/ns_code/article/details/15813809这篇博客中有详细的介绍,在上面的博客中,我们提 ...

  3. 【.Net 学习系列】-- 利用Aspose转换Excel为PDF文件

    功能: 从数据库中查询出数据 利用Aspose.cell + Excel模板绑定数据源生成Excel文件 通过Aspose.pdf + 生成好的Excel生成PDF文件 实现: 查询数据,根据Exce ...

  4. js时间戳和时间格式之间的转换

    //时间戳转换成日期时间2014-8-8 下午11:40:20 function formatDate(ns){ return new Date(parseInt(ns) * 1000).toLoca ...

  5. Matlab多项式拟合測试

    x=0:0.2:4; %生成等差数列 rnd=rand(1,size(x,2))*5; %生成一组随机数 y=x.*x.*x+x.*x+6+rnd; %生成y=x^3+x^2+6函数在垂直方向5个尺度 ...

  6. win7系统使用engine进行开发报错,“未能加载文件或程序集”

    http://www.gisall.com/wordpress/?p=7161 使用vs2010加 arcengine 开发winfrom应用,新建了uc,拖了几个控件后,编译,报未能加载文件或程序集 ...

  7. [Unit Testing] Fundamentals of Testing in Javascript

    In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScr ...

  8. hadoop-mapreduce中reducetask执行分析

    ReduceTask的执行 Reduce处理程序中须要运行三个类型的处理, 1.copy,从各map中copy数据过来 2.sort,对数据进行排序操作. 3.reduce,运行业务逻辑的处理. Re ...

  9. 二:redis 的hash类型相关操作

    =====================二种:hash类型================== 介绍:redis -> hash是一个string类型的field和value的映射表 hash ...

  10. Method Swizzling以及AOP编程:在运行时进行代码注入-b

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...