平衡树

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 ;
}

最新文章

  1. oracle分组取第一条
  2. 【Android Studio使用教程6】Execution failed for task &#39;:&#215;&#215;&#215;:compileReleaseAidl&#39;
  3. Android SDK安装时碰到的问题之解决办法
  4. Oracle用户system解锁
  5. 错误:[将截断字符串或二进制数据。\r\n语句已终止。]
  6. Photoshop快捷键
  7. AFHTTPRequestOperationManager当一个网络请求加入菊花
  8. iOS知识点、面试题 之三
  9. Kibana安装配置
  10. ORA-02030: can only select from fixed tables/views
  11. 【转】浅谈常用的几种web攻击方式
  12. SQL中on条件与where条件的区别
  13. Selenium和firefox兼容性问题
  14. [转]深入理解MFC中程序框架
  15. iOS开发之--iOS APP打包的时候出现的四个选项
  16. 【BZOJ1935/4822】[Shoi2007]Tree 园丁的烦恼/[Cqoi2017]老C的任务 树状数组
  17. Apache 配置代理服务
  18. docker-3-常用命令(上)
  19. Spring学习(三)—— 自动装配案例分析
  20. Android4.0系统以上程序不出现菜单键的问题解决

热门文章

  1. CodeWar---将字符串转换为驼峰命名
  2. 转:C#并口热敏小票打印机打印位图
  3. error MIDL2311 解决方法
  4. UICollectionView 使用 介绍
  5. pyv8的安装和使用:python中执行js代码
  6. Hijacking FM Radio with a Raspberry Pi &amp; Wire
  7. Python机器学习--聚类
  8. remove_if的问题
  9. 生活娱乐 VERYCD的T恤设计大赛
  10. Razor视图引擎布局 Razor视图引擎的基本概念与法语 SQL Server Mobile 和 .NET 数据访问接口之间的数据类型映射 binary 和 varbinary datetime 和 smalldatetime float 和 real