给n个数,有两种操作    Q a b   询问区间[a,b]的最大值,  U a b 将第a个数的值改成b

splay树的中序遍历是我们所维护的序列。如果要询问区间[a,b]的最大值,那么只要将第a-1个数旋转到根结点, 将第b+1个数旋转到根的右孩子,那么根的右孩子的左子树就是所要查询的区间。我们为每一个结点维护一个最大值,表示该以该结点为根的子树的最大值,  那么答案就是 Max[next[next[root][1]][0]];

为了防止越界, 比如要查询区间[1,n]  那么要将第0个数旋转到根结点,将第n+1个数旋转到根的右孩子,   但是却没有这两个数。 所以为了方便,在序列的两端加上两个数,这两个数比序列中的所有数都小, 所以并不影响答案。

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
const int N = + ;
int next[N][],pre[N],key[N],Max[N],sz[N],tot,root;
int a[N]; //如果更新第x个结点, 那么将第该结点splay到根,然后更新
/*
如果询问区间[a,b]的最大值, 那么将a-1 splay到root,将b+1旋到next[root][1] */
void newNode(int &rt, int fa, int k)
{
rt = ++tot;
next[rt][] = next[rt][] = ;
sz[rt] = ;
pre[rt] = fa;
key[rt] = k;
Max[rt] = k;//???????
}
void maintain(int rt)
{
Max[rt] = std::max(key[rt], std::max(Max[next[rt][]],Max[next[rt][]]));
sz[rt] = sz[next[rt][]] + sz[next[rt][]] + ;
}
void build(int &rt, int fa, int l, int r)
{
if(l>r) return;
int m =(l+r)>>;
newNode(rt,fa,a[m]);
build(next[rt][],rt,l,m-);
build(next[rt][],rt,m+,r);
maintain(rt);
}
void rotate(int x, int kind)
{
int y = pre[x];
next[y][!kind] = next[x][kind];
maintain(y);
pre[next[x][kind]] = y;
if(pre[y])
next[pre[y]][next[pre[y]][]==y] = x;
pre[x] = pre[y];
next[x][kind] = y;
pre[y] = x;
maintain(x);
}
int kth(int x, int k)
{
int tmp = sz[next[x][]] + ;
if(tmp==k)
return x;
if(tmp > k)
return kth(next[x][],k);
return kth(next[x][],k-tmp);
}
void splay(int x, int goal)
{
/*
只考虑左右旋的splay
while(pre[x]!=goal)
{
if(next[pre[x]][0]==x)
rotate(x,1);
else
rotate(x,0);
}
*/
while(pre[x]!=goal)
{
int y = pre[x];
if(pre[y]==goal)
{
if(next[y][]==x)
rotate(x,);
else
rotate(x,);
}
else
{
//kind 表示y是父亲的哪个儿子, 0 左,1 右
int kind = next[pre[y]][]==y;
if(next[y][kind]==x)//共线
{
rotate(y,!kind);
rotate(x,!kind);
}
else
{
rotate(x,kind);
rotate(x,!kind);
}
}
}
if(goal==)
root = x;
}
int main()
{
int n,m;
char opt[];
int x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot = ;
memset(next,,sizeof(next));
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
newNode(root,,-);
newNode(next[root][],root,-);
build(next[next[root][]][],next[root][],,n);
maintain(next[root][]);
maintain(root);
while(m--)
{
scanf("%s%d%d",opt,&x,&y);
if(opt[]=='Q')
{
int tmp = kth(root,x);
splay(tmp,);
splay(kth(root,y+),root);
printf("%d\n",Max[next[next[root][]][]]);
}
else
{
splay(kth(root,x+),);
key[root] = Max[root] = y;
maintain(root);
}
}
}
return ;
}

hdu1754(splay)的更多相关文章

  1. hdu1754(splay tree 单点更新,成段查询)

    题意就是简单的点更新,成段查询. splay tree 果真是常数比较大的log(n)操作. 比线段树还慢了这么多. // // main.cpp // splay // // Created by ...

  2. HDU 1754区间最值 & SPLAY

    真是亲切的1754啊..第一道傻逼版的线段树做的是这个,后来学了zkw做的是这个,在后来决定打lrj线段树又打了一遍,如今再用splay和老朋友见面   从上到下依次为:加了读入优化的splay,sp ...

  3. Splay树再学习

    队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...

  4. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  5. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  6. splay最终模板

    来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...

  7. bzoj 3506 && bzoj 1552 splay

    查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...

  8. 【splay】文艺平衡树 BZOJ 3223

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3  ...

  9. 【填坑】bzoj3224 splay裸题

    人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 #include <cstdio> ,n,p,q; ],c[][],size[],sp[]; void rot(i ...

随机推荐

  1. POJ1273_Drainage Ditches(网络流)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54887   Accepted: 2091 ...

  2. TPL异步并行编程之任务超时

    此处参考自阿涛的博文:http://www.cnblogs.com/HelloMyWorld/p/5526914.html 一 自己定义 基本的思路: net中异步操作由于是交给线程来实现,因此不可能 ...

  3. OCA读书笔记(7) - 管理数据库存储结构

    7.Managing Database Storage Structures 逻辑结构 数据库的存储结构有物理结构和逻辑结构组成的 物理结构:物理上,oracle是由一些操作系统文件组成的 SQL&g ...

  4. Swift - 解析JSON数据(内置NSJSONSerialization与第三方JSONKit)

    一,使用自带的NSJSONSerialization 苹果从IOS5.0后推出了SDK自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也比其 ...

  5. xp下Oracle数据库导入SQLServer数据库数据

    Oracle数据库利用ODBC数据源.PLSQL Developer导入SQLServer数据库数据 操作: 建立数据源:控制面板→管理工具→数据源 (ODBC) 打开,界面如下: 点击添加,界面如下 ...

  6. ISO/OSI网络体系结构和TCP/IP协议模型

    1. ISO/OSI的参考模型共有7层,由低层至高层分别为:物理层.数据链路层.网络层.传输层.会话层.表示层.     应用层.各层功能分别为: (1)物理层          提供建立.维护和拆除 ...

  7. hdu 1028 母函数 一个数有几种相加方式

    ///hdu 1028 母函数 一个数有几种相加方式 #include<stdio.h> #include<string.h> #include<iostream> ...

  8. 事务不提交,也有可能写redo和数据文件

    事务不提交,也有可能写redo和数据文件

  9. hdu 1392(凸包)

    传送门:Surround the Trees 题意:求凸包的周长. 分析:凸包模板题,先按极角排好序后,然后根据叉积正负确定凸包. #include <stdio.h> #include ...

  10. 在gem5的full system下运行 alpha编译的测试程序 running gem5 on ubuntu in full system mode in alpha

    背景 先需要在full system下运行gem5,通过网上查找资料以及向别人请教,终于成功运行,网上大多是关于alpha指令集的,且都是英文的,为了方便大家学习,现在总结一下,希望对大家有所帮助. ...