题意:与区间查询点更新,点有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伸展树 初战(单点更新,区间属性查询)的更多相关文章

  1. [转] Splay Tree(伸展树)

    好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...

  2. HDU 1754 I Hate It(线段树之单点更新 区间最值查询)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 1754 I hate it 树状数组维护区间最大值

    Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...

  4. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  5. hdu 1754 I Hate It (splay tree伸展树)

    hdu 1754 I Hate It 其实我只是来存一下我的splay模板的..请大牛们多多指教 #include<stdio.h> #include<string.h> #i ...

  6. POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 ...

  7. hdu 1754 I Hate It(线段树之 单点更新+区间最值)

    I Hate It                                                                             Time Limit: 90 ...

  8. SPOJ - QTREE(树链剖分+单点更新+区间最大值查询)

    题意:给出n个点n-1条边的树,有两个操作,一个是查询节点l到r的边的最大值,然后指定边的更改权值. 题解:差不多是树链剖分的模版题,注意每个点表示的边是连向其父亲节点的边. #include < ...

  9. Splay伸展树入门(单点操作,区间维护)附例题模板

    Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...

随机推荐

  1. idea Please specify commit message

    在idea中使用github来进行版本控制的时候, 当点击提交的时候遇到了这个问题 错误: Please specify commit message 解决方法: 在commit message中填写 ...

  2. hibernate4+spring3+struts2搭建框架实例

    1.所需要的JAR包 2.web.xml配置文件,这个和平时的配置是一样的 <?xml version="1.0" encoding="UTF-8"?&g ...

  3. 【动态规划】luoguP1941 飞扬的小鸟

    细节总是打挂选手:) 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管 ...

  4. webpack-dev-server proxy代理

    一个最简单的代理例子:index.html中有如下代码 fetch('/api/pub/article/list?pageSize=2').then((data)=>{ return data. ...

  5. LeetCode(72) Edit Distance

    题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...

  6. django 常见过滤器

      一.形式:小写 {{ name | lower }} 二.过滤器是可以嵌套的,字符串经过三个过滤器,第一个过滤器转换为小写,第二个过滤器输出首字母,第三个过滤器将首字母转换成大写 标签 {{ st ...

  7. Django之ORM操作(重要)

    Django ORM操作 一般操作 看专业的官网文档,做专业的程序员! 必知必会13条   <1> all(): 查询所有结果 <2> get(**kwargs): 返回与所给 ...

  8. SPOJ DIVSUM - Divisor Summation

    DIVSUM - Divisor Summation #number-theory Given a natural number n (1 <= n <= 500000), please ...

  9. NYOJ 995 硬币找零

    硬币找零 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从 ...

  10. Educational Codeforces Round 31

    A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...