给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. C2B未来:大数据定制

    昨天看到微信SuperSofter写了一篇文章,有感而发.以便备记. 这是一种典型的C2B模式.阿里不仅仅是在与腾讯拼移动.它的电商本土业务也在稳步推进.近期一个里程碑事件是.阿里包下了美的.九阳.苏 ...

  2. ALV判断修改后是否有不合法数据,有则选中错误行,高亮度显示。

    alv数据表维护表时错误行需要高亮度显示 gt_index_rows TYPE lvc_t_row,"用以存放要选择行的内表 gs_index_rows TYPE lvc_s_row.&qu ...

  3. FairScheduler的任务调度机制——assignTasks

    首先需要了解FairScheduler是如何在各个Pool之间分配资源,以及每个Pool如何在Job之间分配资源的.FairScheduler的分配资源发生在update()方法中,而该方法由一个线程 ...

  4. ExtJS拖拽效果

    ExtJS拖拽效果 <html> <head> <title>hello</title> <meta http-equiv="conte ...

  5. CentOS5.6下安装Oracle10G软件 【保留报错经验】

    CentOS5.6下安装Oracle10G ****************************************************************************** ...

  6. Nubia Z5S 基于官方H207/4.4内核的Mokee4.4.4 RC3.2 (2014.7.31修复呼吸灯(能亮依旧不能呼吸))

    特别感谢 yun3195 和 轻描淡写Yhw  帮忙測试 转帖请务必注明本链接地址: http://blog.csdn.net/syhost/article/details/36444259 此ROM ...

  7. [Android学习笔记]Android中多线程开发的一些概念

    线程安全: 在多线程的情况下,不会因为线程之间的操作而导致数据错误. 线程同步: 同一个资源,可能在同一时间被多个线程操作,这样会导致数据错误.这是一个现象,也是一个问题,而研究如何解决此类问题的相关 ...

  8. li里的a标签浮动了,为什么li本身也浮动了?

    <ul> <li><a href="#"></a></li> <li><a href="#& ...

  9. asp.net(C#)之NPOI&quot;操作Excel

    1.首先到网上下载"NPOI.DLL".引用. 2.新建一个操作类"ExcelHelper.cs": using System.Collections.Gene ...

  10. poj 2411 Mondriaan's Dream dp

    一个比较简单的状压dp,记录下每个点的状态即可. #include <iostream> #include <cstdio> #include <cstring> ...