hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)
题意:与区间查询点更新,点有20W个,询问区间的最大值。曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多。
第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩。现在只学了一点点。切个点更新试试。
大致思路:用编号(数组)作为树的键值建树,每插一个数,沿路节点更新最大值(每个结点有一个附加信息标记以之为子树的树所有点的最大值)。所以,查询时【i,j】,只要把i-1伸展到树根,把j+1伸展到I-1下面,那么j+1的左子树就是要的区间了!查该子树根值信息即可(特判端点)!同理,更新操作,只要把待更新的伸展到根,然后更新它,同时维护一下信息即可。
#include<iostream>
#include<cstdio>
using namespace std;
const int maxx=200010;
int a[maxx];int child[maxx][2];int fa[maxx];int maxo[maxx]; //a[i]是原来数组值,child左右孩子(0,1),maxo【i】是以序号i为根的子树(包括自身)的最大值。
int root=0;
void inline maintain(int n) //点被修改后,该点值的维护
{
maxo[n]=maxo[n]>a[n]?maxo[n]:a[n];
}
void inline rotate(int x,int f) //f=1右旋,f=0左旋
{
int y=fa[x];
maxo[x]=maxo[y]; //最大值的更新
maxo[y]=maxo[child[x][f]]>maxo[child[y][f]]?maxo[child[x][f]]:maxo[child[y][f]];
maintain(y); child[y][!f]=child[x][f]; //三次的重连线,注意顺序。
fa[child[x][f]]=y; if(fa[y])
{
if(y==child[fa[y]][0])
child[fa[y]][0]=x;
else
child[fa[y]][1]=x;
}
else
{
root=x;
}
fa[x]=fa[y]; child[x][f]=y;
fa[y]=x;
}
void splay(int n,int goal) //把序号为i的点转到点goal下面的孩子。
{
while(fa[n]!=goal) //一直左右旋即可
{
int y=fa[n];
rotate(n,child[y][0]==n?1:0);
}
}
void inline insert(int n) //插入来建树
{
int temp=root;
if(root==0) //根节点
{
root=n;
maxo[n]=n;
return ;
}
else
{
while(1)
{
maxo[temp]=maxo[temp]<a[n]?a[n]:maxo[temp];//插入时候维护最大值
if(n<temp) //左边
{
if(child[temp][0]==0)
{
child[temp][0]=n;
fa[n]=temp;
maxo[n]=a[n];
splay(n,0); //注意这里要伸展,否则建树就是一般的排序二叉树,会超时
return ;
}
temp=child[temp][0];
}
else //右边
{
if(child[temp][1]==0)
{
child[temp][1]=n;
fa[n]=temp;
maxo[n]=a[n];
splay(n,0);
return ;
}
temp=child[temp][1];
}
}
}
}
void update(int n,int x) //更新 ,把序号为n的值更新为x
{
splay(n,0);
a[n]=x;
maxo[n]=maxo[child[n][0]]>maxo[child[n][1]]?maxo[child[n][0]]:maxo[child[n][1]];
maintain(n);
}
void clear() //初始化
{
root=0;
for(int i=0;i<maxx;i++)
{
maxo[i]=child[i][0]=child[i][1]=fa[i]=0;
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
insert(i);
}
char q;int i,j;
while(m--)
{
getchar();
scanf("%c%d%d",&q,&i,&j);
if(q=='Q')
{ int ans=0;
if(i==1&&j!=n) //区间端点的特判
{
splay(j+1,0);
ans=maxo[child[j+1][0]];
}
else if(i!=1&&j==n)
{
splay(i-1,0);
ans=maxo[child[i-1][1]];
}
else if(i==1&&j==n)
{
ans=maxo[root];
}
else
{
splay(i-1,0);
splay(j+1,i-1);
ans=maxo[child[j+1][0]];
}
printf("%d\n",ans);
}
else
{
update(i,j);
}
}
}
return 0;
}
hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)的更多相关文章
- [转] Splay Tree(伸展树)
好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...
- HDU 1754 I Hate It(线段树之单点更新 区间最值查询)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 1754 I hate it 树状数组维护区间最大值
Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- hdu 1754 I Hate It (splay tree伸展树)
hdu 1754 I Hate It 其实我只是来存一下我的splay模板的..请大牛们多多指教 #include<stdio.h> #include<string.h> #i ...
- POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34522 Accepted: 16224 ...
- hdu 1754 I Hate It(线段树之 单点更新+区间最值)
I Hate It Time Limit: 90 ...
- SPOJ - QTREE(树链剖分+单点更新+区间最大值查询)
题意:给出n个点n-1条边的树,有两个操作,一个是查询节点l到r的边的最大值,然后指定边的更改权值. 题解:差不多是树链剖分的模版题,注意每个点表示的边是连向其父亲节点的边. #include < ...
- Splay伸展树入门(单点操作,区间维护)附例题模板
Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...
随机推荐
- 用例重试机制rerunfailures
安装 rerunfailures插件 pip install pytest-rerunfailures 使用: pytest --reruns 重试次数 如:pytest --reruns 2 重 ...
- java B转换KB MB GB TB PB EB ZB
public static String readableFileSize(long size) { if (size <= 0) { return "0"; } final ...
- shell脚本,如何监控目录下的文件内容是否被修改。
第一种方法是通过cmp来进行比对[root@localhost bo]# ls .html .html .html .html .html .html .html .html .html cat.sh ...
- python 中requests 模块用py2exe生成exe后SSL certificate exception的问题
[('system library', 'fopen', 'No such process'), ('BIO routines', 'BIO_new_file', 'no such file'), ( ...
- perl学习之FLOCK函数的调用(讲的非常好)
一段演示flock系统调用的perl程序http://www.extmail.org/forum/viewthread.php?tid=1066
- InnoDB体系架构总结(二)
事务 确保事务内的SQL都可以同步执行 要么一起成功 要么一起失败.事务有四个特性原子性 一致性,隔离性,持久性 实现方式 开始事务的时候回家记录记录一个LSN日志序列 当事务执行的时候 会首先在In ...
- cs229_part6
part 6 接下来就是无监督学习算法了. k均值聚类 问题背景 样本集描述: \[ x\in D, x\in R^n \] 之前的有监督学习问题中,所有的x都有对应的y.但是如果我们的x没有对应的y ...
- 常见slave 延迟原因以及解决方法
一 序言在运维线上M-M 架构的MySQL数据库时,接收的比较多关于主备延时的报警: 点击(此处)折叠或打开 check_ins_slave_lag (err_cnt:1)critical-slav ...
- MFC中的CListControl控件
一直想要这种效果,无奈刚开始用了cListbox控件,不知道怎么生成背景的边框,找了好久资料,突然发现好像控件用错了. 用CListControl控件实现图中效果,超级开心~ 实现过程: 添加一个Li ...
- PHP优化之批量操作MySQL
设计一个数据表如下: create table optimization( id INT NOT NULL AUTO_INCREMENT, value VARCHAR(10) NOT NULL, PR ...