hdu1754(splay)
给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)的更多相关文章
- hdu1754(splay tree 单点更新,成段查询)
题意就是简单的点更新,成段查询. splay tree 果真是常数比较大的log(n)操作. 比线段树还慢了这么多. // // main.cpp // splay // // Created by ...
- HDU 1754区间最值 & SPLAY
真是亲切的1754啊..第一道傻逼版的线段树做的是这个,后来学了zkw做的是这个,在后来决定打lrj线段树又打了一遍,如今再用splay和老朋友见面 从上到下依次为:加了读入优化的splay,sp ...
- Splay树再学习
队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- splay最终模板
来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...
- bzoj 3506 && bzoj 1552 splay
查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...
- 【splay】文艺平衡树 BZOJ 3223
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- 【填坑】bzoj3224 splay裸题
人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 #include <cstdio> ,n,p,q; ],c[][],size[],sp[]; void rot(i ...
随机推荐
- 与众不同 windows phone (12) - Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任务)
原文:与众不同 windows phone (12) - Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任 ...
- System.getProperty("catalina.home")
System.getProperty("catalina.base"),服务器配置目录
- [置顶] 简单解析linux下进程通信方法
linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...
- Js正则表达式学习之test和compile的简单介绍
RegExp 对象用于规定在文本中检索的内容. 定义 RegExp RegExp 对象用于存储检索模式. 通过 new 关键词来定义 RegExp 对象.以下代码定义了名为 patt1 的 RegEx ...
- ajax+jsp实现三级联动下拉框
js文件sjld.js : $(document).ready( function(){ $.ajax({ url:"bindZ", type:"get", ...
- hdu 3488 Tour
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意:给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都只 ...
- Linux命令之rz - 批量上传文件,简单易用(转载)
用途说明 rz命令能够批量上传文件,当然也可上传单个文件啦.使用的协议是古老的ZMODEM协议,尽管协议古老,但毫不影响的简单易用的特性.一般情 况我们要上传文件到Linux系统,要么使用ftp(还得 ...
- iOS 搜索框控件 最简单的dome
刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...
- IE, FireFox, Opera 浏览器支持CSS实现Alpha透明的方法 兼容问题
一:要解决的问题时:在ie6-ie11下兼容下面透明上传文件button的效果. 实现方式通过滤镜实现. 二:效果图例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- POJ 1018 【枚举+剪枝】.cpp
题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...