题意就是简单的点更新,成段查询。

splay tree 果真是常数比较大的log(n)操作。 比线段树还慢了这么多。

//
// main.cpp
// splay
//
// Created by 陈加寿 on 16/3/25.
// Copyright © 2016年 chenhuan001. All rights reserved.
// #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; //来一发点更新,成段查询
#define MAXN 1001000
#define INF 1001000000 struct Splay_Tree
{
int cnt, rt;
struct node
{
int K,mxk;
int key, size, fa, son[];
void set(int _key, int _size, int _fa,int _K)
{
mxk = K = _K;//这个表示这个节点的值
key = _key;//这个为ID
size=_size;
fa=_fa;
son[]=son[]=;
}
}T[MAXN];
inline void init()
{
cnt=;
T[cnt++].set(, , , );//
rt = ;
//初始化的时候,把0这个节点的K设置为INF
}
inline void PushUp(int x)
{
//这一步就很关键了
T[x].size=T[T[x].son[]].size+T[T[x].son[]].size+;
T[x].mxk = max(T[x].K,max(T[T[x].son[]].mxk,T[T[x].son[]].mxk));
} inline void Rotate(int x, int p) //0左旋 1右旋
{
int y=T[x].fa;
T[y].son[!p]=T[x].son[p];
T[T[x].son[p]].fa=y;
T[x].fa=T[y].fa;
if(T[x].fa)
T[T[x].fa].son[T[T[x].fa].son[] == y]=x;
T[x].son[p]=y;
T[y].fa=x;
PushUp(y);//先更新y
PushUp(x);//再更新x
} void Splay(int x, int To) //将x节点插入到To的子节点中
{
while(T[x].fa != To)
{
if(T[T[x].fa].fa == To)
Rotate(x, T[T[x].fa].son[] == x);
else
{
int y=T[x].fa, z=T[y].fa;
int p=(T[z].son[] == y);
if(T[y].son[p] == x)
Rotate(x, !p), Rotate(x, p); //之字旋
else
Rotate(y, p), Rotate(x, p); //一字旋
}
}
if(To == ) rt=x;
} int find(int key) //返回值为key的节点 若无返回0 若有将其转移到根处
{
int x=rt;
while(x && T[x].key != key)
x=T[x].son[key > T[x].key];
if(x) Splay(x, );
return x;
} int prev() //返回比根值小的最大值 若无返回0 若有将其转移到根处
{
int x=T[rt].son[];
if(!x) return ;
while(T[x].son[])
x=T[x].son[];
Splay(x, );
return x;
} int next() //返回比根值大的最小值 若无返回0 若有将其转移到根处
{
int x=T[rt].son[];
if(!x) return ;
while(T[x].son[])
x=T[x].son[];
Splay(x, );
return x;
} void Insert(int key,int K) //插入key 并且将该节点转移到根处
{
if(!rt)
T[rt = cnt++].set(key, , , K);
else
{
int x=rt, y=;
while(x)
{
y=x;
x=T[x].son[key > T[x].key];
}
T[x = cnt++].set(key, , y, K);
T[y].son[key > T[y].key]=x;
Splay(x, );
}
} void Delete(int key) //删除值为key的节点 若有重点只删其中一个 x的前驱移动到根处
{
int x=find(key);
if(!x) return;
int y=T[x].son[];
while(T[y].son[])
y=T[y].son[];
int z=T[x].son[];
while(T[z].son[])
z=T[z].son[];
if(!y && !z)
{
rt=;
return;
}
if(!y)
{
Splay(z, );
T[z].son[]=;
PushUp(z);
return;
}
if(!z)
{
Splay(y, );
T[y].son[]=;
PushUp(y);
return;
}
Splay(y, );
Splay(z, y);
T[z].son[]=;
PushUp(z);
PushUp(y);
} int GetPth(int p) //获得第p小的节点 并将其转移到根处
{
if(!rt) return ;
int x=rt;
while(x)
{
if(p == T[T[x].son[]].size+)
break;
if(p>T[T[x].son[]].size+)
{
p-=T[T[x].son[]].size+;
x=T[x].son[];
}
else
x=T[x].son[];
}
Splay(x, );
return x;
} int GetRank(int key) //获得值<=key的节点个数 并将其转移到根处 若<key只需将<=换为<
{
if(!rt) return ;
int x=rt, ret=, y=;
while(x)
{
y=x;
if(T[x].key <= key)
{
ret+=T[T[x].son[]].size+;
x=T[x].son[];
}
else
x=T[x].son[];
}
Splay(y, );
return ret;
}
}spt; int main() {
//两种操作
//一、修改某个节点值
//二、区间查询
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
spt.init();
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
spt.Insert(i, tmp);
}
char sign;
for(int i=;i<m;i++)
{
getchar();
int a,b;
scanf("%c %d %d",&sign,&a,&b);
if(sign == 'Q')
{
if(a == b)
{
int id = spt.GetPth(a);
printf("%d\n",spt.T[id].K);//区间只有一个点的时候
continue;
}
int idb = spt.GetPth(b);
int ida = spt.GetPth(a);
spt.Splay(idb, ida);
int idson = spt.T[idb].son[];
int mx = max( spt.T[idson].mxk ,max(spt.T[ida].K,spt.T[idb].K));
printf("%d\n",mx);
}
else
{
//更新操作
int id = spt.GetPth(a);
spt.T[id].K = b;
spt.PushUp(id);
}
}
}
return ;
}

hdu1754(splay tree 单点更新,成段查询)的更多相关文章

  1. 【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst

    题目大意: 2个操作 A.区间a b 增加 c B 查询a b; 注意事项:1.记住要清除标记 2.查询时要下放标记,但没必要向上更新 线段:自带的,不用建模 区间和性质:sum: /* WA 1次 ...

  2. HDU 1754 I Hate It 线段树(单点更新,成段查询)

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题解: 单点更新,成段查询. 代码: #include<iostream> ...

  3. splay tree成段更新,成段查询poj3466

    线段树入门题,换成splay tree 来搞搞. #include <stdio.h> #include <string.h> #include <algorithm&g ...

  4. [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 RT,该题要求每次更新是更新所有节点,分别求平方根,查询是求和.昨晚思前想后找有没有一个数学上的 ...

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

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

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

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

  7. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

  8. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  9. HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)

    Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...

随机推荐

  1. Android TextView 常见问题与使用总结

    一.文字显示行数设置 1. 仅显示一行文字 android:singleLine="true" setTransformationMethod(TransformationMeth ...

  2. 字符编码简介:ASCII,Unicode,UTF-8,GB2312

    字符编码简介:ASCII,Unicode,UTF-8,GB2312 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和 1两种状态,因 ...

  3. android Menory 小结

    不建议在Activity中使用static 变量,考虑使用Application.当然,static final例外 但Application也不要cache某个Activity使用的View,如果c ...

  4. MapReduce源代码分析之JobSubmitter(一)

    JobSubmitter.顾名思义,它是MapReduce中作业提交者,而实际上JobSubmitter除了构造方法外.对外提供的唯一一个非private成员变量或方法就是submitJobInter ...

  5. 在VS2013 IIS Express 添加MIME映射

    打开VS2013返回json提示MIME映射问题 1.在DOS窗口下进入IIS Express安装目录,默认是C:\Program Files\IIS Express,cmd  命令行cd 到 该目录 ...

  6. 网页CSS font-size使用em替代px

    px和em都是长度单位,区别是,px的值是固定的,em的值是相对的,并且em会继承父级元素的字体大小. 任意浏览器的默认字体高都是16px.所以未经调整的浏览器都符合: 1em=16px.那么12px ...

  7. Android开发系列之创建自定义控件

    Android开发过程中我们经常需要定义自己的控件,一方面基于复用的角度考虑,一方面也是基于逻辑处理思维的角度考虑.在这篇博客里面,笔者想要介绍.总结几种Android自定义控件的方法,如果有什么不对 ...

  8. codeblocks设置当前行高亮

    默认是不开启当前行高亮的. 如果想打开,选择:Settings>Editor>Editor Settings>Other options> 勾选Highlight line u ...

  9. python 循环结构

    for循环 list或tuple可以表示一个有序集合.如果我们想依次访问一个list中的每一个元素呢?比如 list: L = ['Adam', 'Lisa', 'Bart'] print L[0] ...

  10. UI-1-UI入门

    课程要点: 创建一个iOS工程 AppDelegate类 UIKit框架以及UIWindow 在window上添加第一个试图UIView NSTimer(定时器) 创建一个iOS工程 PS:接下来简单 ...