洛谷:P3391 【模板】文艺平衡树(Splay)

 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=;
int n,m,tp,root,CNT;
int key[MAXN],lz[MAXN],fa[MAXN],sz[MAXN],val[MAXN],q[MAXN],Q[MAXN],ch[MAXN][];
void update(int k){ sz[k]=sz[ch[k][]]+sz[ch[k][]]+; }
void rotate(int x,int &y)
{
int old=fa[x],oldf=fa[old],op=ch[old][]==x;
if(old==y) y=x;
else ch[oldf][ch[oldf][]==old]=x;
fa[x]=oldf;
fa[ch[x][op^]]=old; ch[old][op]=ch[x][op^];
fa[old]=x; ch[x][op^]=old;
update(old); update(x);
}
void down(int x)
{
if(lz[x])
{
lz[x]^=; lz[ch[x][]]^=; lz[ch[x][]]^=;
swap(ch[x][],ch[x][]);
}
}
void splay(int x,int &y)
{
int now=x,old,oldf;Q[++tp]=now;
while(now!=y) Q[++tp]=fa[now] , now=fa[now];
while(tp--) down(Q[tp]);
while(x!=y)
{
old=fa[x],oldf=fa[old];
if(old!=y){
if((ch[old][]==x)^(ch[oldf][]==old)) rotate(x,y);
else rotate(old,y);
}
rotate(x,y);
}
}
void Build(int &k,int ll,int rr,int FA)
{
int mid=(ll+rr)/;
k=++CNT; fa[k]=FA; key[k]=val[mid];
if(mid>ll) Build(ch[k][],ll,mid-,k);
if(mid<rr) Build(ch[k][],mid+,rr,k);
update(k);
}
int findx(int x)//注意下放。
{
int now=root;
while()
{
down(now);
if(x<=sz[ch[now][]]) now=ch[now][];
else
{
x-=sz[ch[now][]]+;
if(x==) return now;
else now=ch[now][];
}
}
}
void rev(int L,int R)
{
int ll=findx(L-),rr=findx(R+);
splay(ll,root);
splay(rr,ch[root][]);
lz[ch[ch[root][]][]]^=;
}
void dfs(int u)
{
down(u);
if(ch[u][]) dfs(ch[u][]);
if(key[u]!=&&key[u]!=n+) printf("%d ",key[u]);
if(ch[u][]) dfs(ch[u][]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)val[i]=i;
Build(root,,n+,);
for(int i=,L,R;i<=m;i++)
{
scanf("%d%d",&L,&R);
L++; R++; rev(L,R);
}
dfs(root); puts("");
return ;
}

Splay(区间翻转) 模板的更多相关文章

  1. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

  2. splay区间翻转

    原题P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: ...

  3. hdu-1890-Robotic Sort splay区间翻转

    题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...

  4. bzoj 3223 文艺平衡树 splay 区间翻转

    Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17715  Solved: 7769[Submit][Status][ ...

  5. bzoj 1251序列终结者 splay 区间翻转,最值,区间更新

    序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4594  Solved: 1939[Submit][Status][Discuss] De ...

  6. BZOJ 3223 Splay区间翻转

    思路: 区间翻转的裸题 终于tm理解splay了-- //By SiriusRen #include <cstdio> #include <cstring> #include ...

  7. Splay(区间翻转)&树套树(Splay+线段树,90分)

    study from: https://tiger0132.blog.luogu.org/slay-notes P3369 [模板]普通平衡树 #include <cstdio> #inc ...

  8. [bzoj3223]文艺平衡树(splay区间反转模板)

    解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  9. hdu1890 Robotic Sort (splay+区间翻转单点更新)

    Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...

  10. 2019牛客国庆集训派对day1 K题 双向链表练习题 splay区间翻转

    题目链接: 解法: 先建n颗平衡树,合并的时候将a中最右的结点翻转到根节点,b中最左的结点翻转到根节点,对合并后的根节点进行标记. #include <bits/stdc++.h> usi ...

随机推荐

  1. iOS8 PUSH解决方法

    本文转载至 http://blog.csdn.net/pjk1129/article/details/39548523     - (void)registerForRemoteNotificatio ...

  2. iOS8需要兼容的内容

    本文转载至  http://blog.csdn.net/liuwuguigui/article/details/39494435 1.iPad上使用presentModalViewController ...

  3. 一起来学linux:PAM模块

    在Linux中执行有些程序时,这些程序在执行前首先要对启动它的用户进行认证,符合一定的要求之后才允许执行,例如login, su等 在Linux中进行身份或是状态的验证程序是由PAM来进行的,PAM( ...

  4. win10安装VMware

    首先下载VMware安装包: 双击安装: 点击:“uninstall”安装,这个过程需要一些时间 点击“Next” 选择典型或者自定义安装 这里我选择的是典型安装,点击“Next”: 选择是否检查更新 ...

  5. 设置开启telnet功能

    今天访问服务器的时候发现ip可以ping通,但是不能访问,就telnet一下端口吧,谁知系统逗我:

  6. SpringBoot-(6)-日志SLF4j

    一,日志简介: 目前有很多日志框架,SpringBoot内部采用了SLF4j+logback的形式. SpringBoot内部日志库依赖关系如下: 二,日志的分级 常用的Log日志分级如下: /* * ...

  7. Spring 4.2框架中注释驱动的事件监听器详解

    事件交互已经成为很多应用程序不可或缺的一部分,spring框架提供了一个完整的基础设施来处理瞬时事件.下面我们来看看Spring 4.2框架中基于注释驱动的事件监听器. 1.早期的方式 在早期,组件要 ...

  8. performSelector: 与 dispatch_time 异同

    iOS中timer相关的延时调用,常见的有NSObject中的performSelector:withObject:afterDelay:这个方法在调用的时候会设置当前runloop中timer,还有 ...

  9. java to Json or Json to JavaBean

    今天练习,放这里,以后再补充 这里使用的jar包是 net.sf.json.JSONObject package yh.test.t1118; import net.sf.json.JSONArray ...

  10. 理解HTML解析过程

    浏览器解析html的过程是:接受网络数据->将二进制码变成字符->将字符变为unicode code points.->tokenizer ->tree constructor ...