BZOJ3223/洛谷P3391 - 文艺平衡树
题意
模板题啦~×2
代码
//文艺平衡树
#include <cstdio>
#include <algorithm>
using namespace std;
int const N=1e5+10;
int n,m;
int rt,fa[N],ch[N][2],siz[N]; bool rev[N];
int wh(int p) {return p==ch[fa[p]][1];}
void update(int p) {siz[p]=siz[ch[p][0]]+siz[ch[p][1]]+1;}
void pushdown(int p)
{
if(rev[p])
{
swap(ch[p][0],ch[p][1]);
rev[ch[p][0]]^=1; rev[ch[p][1]]^=1;
rev[p]=false;
}
}
void trPrint()
{
for(int i=1;i<=n+2;i++) printf("%d fa=%d son=%2d,%2d siz=%d rev=%d\n",i,fa[i],ch[i][0],ch[i][1],siz[i],rev[i]);
printf("\n");
}
void rotate(int p,int &k)
{
int q=fa[p],r=fa[q],w=wh(p);
if(q==k) k=p;
else ch[r][q==ch[r][1]]=p;
fa[ch[q][w]=ch[p][w^1]]=q;
fa[ch[p][w^1]=q]=p; fa[p]=r;
update(q); update(p);
//printf("After rotate(%d)\n",p); trPrint();
}
void splay(int p,int &k)
{
//printf("Splay %d to %d.\n",p,k);
while(p!=k)
{
int q=fa[p],r=fa[q];
if(q!=k)
if(wh(p)^wh(q)) rotate(p,k);
else rotate(q,k);
rotate(p,k);
}
}
int build(int fr,int to)
{
if(fr>to) return 0;
if(fr==to) {siz[fr]=1; return fr;}
int p=(fr+to)>>1;
fa[ch[p][0]=build(fr,p-1)]=p;
fa[ch[p][1]=build(p+1,to)]=p;
update(p);
return p;
}
int find(int x)
{
int p=rt;
while(true)
{
pushdown(p);
//printf("Find No.%d in %d.\n",x,p);
int sizL=siz[ch[p][0]]+1;
if(sizL>x) p=ch[p][0];
if(sizL==x) return p;
if(sizL<x) p=ch[p][1],x-=sizL;
}
}
void reverse(int fr,int to)
{
int p1=find(fr-1),p2=find(to+1);
//printf("Reverse [%d,%d], p1=%d, p2=%d\n",fr,to,p1,p2);
splay(p1,rt); splay(p2,ch[rt][1]);
int p=ch[p2][0];
rev[p]^=1;
//trPrint();
}
void type(int p)
{
if(p==0) return;
pushdown(p);
type(ch[p][0]);
if(p!=1 && p!=n+2) printf("%d ",p-1);
type(ch[p][1]);
}
int main()
{
scanf("%d%d",&n,&m);
rt=(n+3)>>1;
build(1,n+2);
for(int owo=1;owo<=m;owo++)
{
int fr,to;
scanf("%d%d",&fr,&to);
reverse(fr+1,to+1);
}
type(rt);
return 0;
}BZOJ3223/洛谷P3391 - 文艺平衡树的更多相关文章
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- 洛谷 P3391 文艺平衡树
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 --b ...
- 洛谷P3391文艺平衡树(Splay)
题目传送门 转载自https://www.cnblogs.com/yousiki/p/6147455.html,转载请注明出处 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 ...
- 洛谷P3391 文艺平衡树 (Splay模板)
模板题. 注意标记即可,另外,涉及区间翻转操作,记得设立首尾哨兵. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- 洛谷.3391.文艺平衡树(fhq Traep)
题目链接 //注意反转时先分裂r,因为l,r是针对整棵树的排名 #include<cstdio> #include<cctype> #include<algorithm& ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- [luogu P3391] 文艺平衡树
[luogu P3391] 文艺平衡树 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区 ...
随机推荐
- mybatis传参的几种方式
1,@Param @参考文章 @Select("select s_id id,s_name name,class_id classid from student where s_name= ...
- java编程中常用英语单词
JAVA 省略语与名词解释 -------------------------------- J2EETM----JavaTM 2PlatformEnterpriseEdition : J2EE(JA ...
- 【转】linux grep命令
1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来 2.格式 grep [options] 3.主要参数 [options]主要参数: - ...
- 01-Go命令与基础
什么是Go? Go是一门并发支持.垃圾回收的编译型系统编程语言,旨在创造一门具有在静态编译语言的高性能和动态的高效开之间拥有良好平衡点的一门编程语言. Go的主要特点有哪些? 类型安全和内存安全 以非 ...
- TCP/IP协议全解析 三次握手与四次挥手[转]
所谓三次握手(Three-Way Handshake)即建立TCP连接,就是指建立一个TCP连接时,需要客户端和服务端总共发送3个包以确认连接的建立.所谓四次挥手(Four-Way Wavehand) ...
- 《.NET 设计规范》第 7 章:异常
第 7 章:异常 异常与各种面向对象语言集成得非常好. 异常增强了 API 的一致性. 在用返回值来报告错误时,错误处理的代码与可能会发生错误的代码距离总是很近. 更容易使错误处理的带码全局化. 错误 ...
- AppScan 工作原理
Rational AppScan(简称 AppScan)其实是一个产品家族,包括众多的应用安全扫描产品,从开发阶段的源代码扫描的 AppScan source edition,到针对 Web 应用进行 ...
- 《深入理解Java虚拟机》学习笔记(一)
JDK是支持Java程序开发的最小环境集,JRE是支持Java程序运行的标准环境,JRE是JDK的一部分. Java 1.0版本诞生于1995年,其使用的虚拟机是Sun Classisc VM,这款虚 ...
- 常系数齐次线性递推 & 拉格朗日插值
常系数齐次线性递推 具体记在笔记本上了,以后可能补照片,这里稍微写一下,主要贴代码. 概述 形式: \[ h_n = a_1 h_{n-1}+a_2h_{n-2}+...+a_kh_{n-k} \] ...
- NSIS 设置系统变量
定义 ; include for some of the windows messages defines !include "winmessages.nsh" ; HKLM (a ...