bzoj3223 文艺平衡树
传送门 :http://www.lydsy.com/JudgeOnline/problem.php?id=3223
splay区间翻转的基础题,然而我还是调了一晚上(蒟蒻的悲哀)
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,root,x,y;
int ls[100005],rs[100005],s[100005],v[100005],lazy[100005],fa[100005],f[100005];
void pushup(int rt)
{
s[rt]=s[ls[rt]]+s[rs[rt]]+1;
}
void pushdown(int rt)
{
if (!lazy[rt]) return;
swap(ls[rt],rs[rt]);
lazy[ls[rt]]^=1;
lazy[rs[rt]]^=1;
lazy[rt]=0;
}
void build(int l,int r,int fa)
{
if (l>r) return;
int now=v[l];
int last=v[fa];
if (l==r)
{
s[now]=1;
f[now]=last;
if (l<fa) ls[last]=now;
else rs[last]=now;
return;
}
int mid=(l+r)>>1;
now=v[mid];
build(l,mid-1,mid);
build(mid+1,r,mid);
f[now]=last;
if (mid<fa) ls[last]=now;
else rs[last]=now;
pushup(mid);
}
void zig(int x)
{
int y=f[x],z=f[y];
if (ls[z]==y) ls[z]=x;
else rs[z]=x;
f[x]=z;
f[y]=x;
ls[y]=rs[x];
f[rs[x]]=y;
rs[x]=y;
pushup(y);
pushup(x);
if (y==root) root=x;
}
void zag(int x)
{
int y=f[x],z=f[y];
if (ls[z]==y) ls[z]=x;
else rs[z]=x;
f[x]=z;
f[y]=x;
rs[y]=ls[x];
f[ls[x]]=y;
ls[x]=y;
pushup(y);
pushup(x);
if (y==root) root=x;
}
void splay(int x,int k)
{
while(f[x]!=k)
{
if (ls[f[x]]==x) zig(x);
else zag(x);
}
}
int find(int &k,int x)
{
pushdown(k);
if(s[ls[k]]+1==x)return k;
if(s[ls[k]]>=x)return find(ls[k],x);
return find(rs[k],x-s[ls[k]]-1);
}
void lazye(int l,int r)
{
int x=find(root,l);
int y=find(root,r);
splay(x,0);
splay(y,x);
lazy[ls[y]]^=1;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n+2;i++)
{
v[i]=i;
}
root=(n+3)>>1;
build(1,n+2,0);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
lazye(x,y+2);
}
for (int i=2;i<=n+1;i++)
{
printf("%d ",find(root,i)-1);
}
}
bzoj3223 文艺平衡树的更多相关文章
- bzoj3223 文艺平衡树 (treap or splay分裂+合并)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3313 Solved: 1883 [Submit][S ...
- BZOJ3223文艺平衡树——非旋转treap
此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...
- [BZOJ3223]文艺平衡树 无旋Treap
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树
http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...
- [luogu3391][bzoj3223]文艺平衡树【splay】
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 分析 ...
- [bzoj3223]文艺平衡树(splay区间反转模板)
解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- bzoj3223 文艺平衡树 codevs3303 翻转区间
splay模版题吧 只有区间翻转 至于为什么要把须翻转区间旋到根 因为查找一个区间可以先找出他左端点左边第一个点和右端点x右边第一个点y 然后将x旋到根节点 y旋到x的右儿子 这样x的右边的点就是所有 ...
- [bzoj3223]文艺平衡树——splay
题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...
随机推荐
- angular 数据绑定
1.ng-band 使用在index.html页面使用,该页面不要直接使用双花括号,否则网络延迟的时候页面会显示双花括号,由于通常只在index.html里导入angular.js,所以在其他模板页面 ...
- vert.x学习(六),动态模板与静态文件的结合
这篇学习在动态模板里面引入css,把动态模板与静态文件结合起来使用. 编写DynamicReference.java package com.javafm.vertx.helloworld; impo ...
- Elasticsearch判断多列存在、bool条件组合查询示例
and符号判断多列存在:{ "filter": { "and": [ { "exists": { ...
- 记一次【求n以内的素数个数】的优化记录
最近在leetCode上刷提,还是满锻炼人的,为以后面试打基础吧.不多说下面开始. 问题:求[2,n]之间的素数的个数. 来源:leetCode OJ 提示: Let's start with a i ...
- QStriingList
#include <QCoreApplication> #include<QDebug> #include<QStringList> int main(int ar ...
- mysql关于排序值的问题
- Redis数据类型介绍
Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...
- laravel 在windows中使用一键安装包步骤
安装 PHP 注意一:Laravel 5.0 开始对 PHP 版本的要求是 >=5.4,Laravel 5.1 要求 PHP 版本 >=5.5.9,所以,建议大家尽量安装 5.5.x 的最 ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- Windows Server 2008 R2父域管理员与子域管理员相互登录访问
一.问题: 父域创建的域管理员登录不了子域服务器,子域创建的域管理员登录不了父域服务器? 二.原因分析: 父域创建的域管理员是存在父域中,而子域创建的域管理员存在子域中,父域创建的管理员是不存在子域中 ...