hdu 1754 I Hate It

其实我只是来存一下我的splay模板的。。请大牛们多多指教

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std ; const int maxn = 222222 ; int son[2][maxn] , col[maxn] , fa[maxn] , size[maxn] , val[maxn] ;
int tot ;
int num[maxn] ; void new_node () {
size[tot] = 1 ;
fa[tot] = son[0][tot] = son[1][tot] = -1 ;
val[tot] = col[tot] = 0 ;
tot ++ ;
} void update ( int rt ) {
size[rt] = 1 ;
col[rt] = val[rt] ;
if ( son[0][rt] != -1 )
size[rt] += size[son[0][rt]] , col[rt] = max ( col[rt] , col[son[0][rt]] ) ;
if ( son[1][rt] != -1 )
size[rt] += size[son[1][rt]] , col[rt] = max ( col[rt] , col[son[1][rt]] ) ;
} int build ( int l , int r ) {
if ( l > r ) return -1 ;
int mid = ( l + r ) >> 1 ;
new_node () ;
int temp = tot - 1 ;
val[temp] = num[mid] ;
son[0][temp] = build ( l , mid - 1 ) ;
if ( son[0][temp] != -1 ) fa[son[0][temp]] = temp ;
son[1][temp] = build ( mid + 1 , r ) ;
if ( son[1][temp] != -1 ) fa[son[1][temp]] = temp ;
update ( temp ) ;
return temp ;
} void rot ( int rt , int c ) {
int y = fa[rt] , z = fa[y] ;
son[!c][y] = son[c][rt] ;
if ( son[c][rt] != -1 ) fa[son[c][rt]] = y ;
fa[rt] = z ;
if ( z != -1 ) {
if ( y == son[0][z] ) son[0][z] = rt ;
else son[1][z] = rt ;
}
son[c][rt] = y , fa[y] = rt ;
update ( y ) ;
} void splay ( int rt , int to ) {
while ( fa[rt] != to ) {
if ( fa[fa[rt]] == to ) {
if ( rt == son[0][fa[rt]] ) rot ( rt , 1 ) ;
else rot ( rt , 0 ) ;
}
else {
int y = fa[rt] , z = fa[y] ;
if ( rt == son[0][y] ) {
if ( y == son[0][z] ) rot ( y , 1 ) , rot ( rt , 1 ) ;
else rot ( rt , 1 ) , rot ( rt , 0 ) ;
}
else {
if ( y == son[1][z] ) rot ( y , 0 ) , rot ( rt , 0 ) ;
else rot ( rt , 0 ) , rot ( rt , 1 ) ;
}
}
}
update ( rt ) ;
/*为什么只要在最后更新rt节点呢?因为在旋转的过程中,fa[rt]总是旋到rt下面的
所以在旋转的时候,我们不断的更新fa[rt],而不需要一直更新rt,只要在splay之后更新rt就好了
*/
} int find ( int rt , int key ) {
int cnt = 0 ;
if ( son[0][rt] != -1 ) cnt = size[son[0][rt]] ;
if ( cnt + 1 == key ) return rt ;
if ( cnt >= key ) return find ( son[0][rt] , key ) ;
return find ( son[1][rt] , key - cnt - 1 ) ;
} int main () {
int n , m , i , j , k , a , b ;
char op[11] ;
while ( scanf ( "%d%d" , &n , &m ) != EOF ) {
for ( i = 1 ; i <= n ; i ++ ) scanf ( "%d" , &num[i] ) ;
tot = 0 ;
int root = build ( 0 , n + 1 ) , temp ;
while ( m -- ) {
scanf ( "%s%d%d" , op , &a , &b ) ;
a ++ , b ++ ;//因为建树的时候,把0也建进去了,但实际上0是不在原数组中的,所有往后推移一位
if ( op[0] == 'U' ) {
temp = find ( root , a ) ;
splay ( temp , -1 ) ;
root = temp ;
val[root] = b - 1 ;
// update ( root ) ;
//这里为什么不用更新rt呢?因为对于size的值是不会变的,而val值已经更新了,而对于col值,下次是splay时,肯定会更新掉的
}
else {
temp = find ( root , a - 1 ) ;
splay ( temp , -1 ) ;
root = temp ;
temp = find ( root , b + 1 ) ;
splay ( temp , root ) ;
printf ( "%d\n" , col[son[0][temp]] ) ;
}
}
}
}

hdu 1754 I Hate It (splay tree伸展树)的更多相关文章

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

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

  2. hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)

    题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只 ...

  3. HDU 1754区间最值 & SPLAY

    真是亲切的1754啊..第一道傻逼版的线段树做的是这个,后来学了zkw做的是这个,在后来决定打lrj线段树又打了一遍,如今再用splay和老朋友见面   从上到下依次为:加了读入优化的splay,sp ...

  4. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. [置顶] hdu 4699 2个栈维护 or 伸展树

    hdu 4699  Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和.. 注意这里的k是在光标之前的, ...

  6. 【模板】Splay(伸展树)普通平衡树(数据加强版)/洛谷P6136

    题目链接 https://www.luogu.com.cn/problem/P6136 题目大意 需要写一种数据结构,来维护一些非负整数( \(int\) 范围内)的升序序列,其中需要提供以下操作: ...

  7. HDU 1754 I Hate It (Splay 区间操作)

    题目大意 维护一个序列,支持两种操作 操作一:将第x个元素的值修改为y 操作二:询问区间[x,y]内的元素的最大值 解题分析 splay的区间操作,事先加入两个编号最小和最大的点防止操作越界. 具体的 ...

  8. Splay(伸展树)/HDU6873

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6873 题目大意 给定一组 \(n\) 列的方块,每列方块数 \(b_i\) ,现有 \(q\) 次操作 ...

  9. HDU 4718 The LCIS on the Tree(树链剖分)

    Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i < ...

随机推荐

  1. homework-Agile Software Development

    对敏捷开发的一些思考 这周的作业是对敏捷开发的相关阅读和思考. 在阅读的过程中,可以看到作者是一位具有丰富编程经验的大师.在开发的经历中,作者经历了极限编程等开发过程,但是在作者的多年经验中,作者还是 ...

  2. Highcharts资料

    对应的API:   http://api.hcharts.cn/#chart.events 对应的中文网实例:http://www.hcharts.cn/demo/highcharts/dynamic ...

  3. java连接数据库时出现如下错误:

    java.lang.ClassNotFoundException: com.mysql.jdbc.driver at org.apache.catalina.loader.WebappClassLoa ...

  4. 被FBI点名的中国黑客-Lion

    网名:Lion(狮子) 真实姓名:林勇 QQ:21509     简介:红客联盟创始人(该组织在2001年5月的黑客大战中一举成名,会员人数最多时达到6万,很有影响力),现在安氏因特网安全系统(中国) ...

  5. 0xc0000428 winload.exe无法验证其数字签名的解决方法

    只要把windows/system32/boot中的winload.exe复制到windows/system32中替换即可!! 只有启动画面会有变化,可以使用魔方等软件进行修复,恢复到之前的样子

  6. SEO 网站页面SEO优化之页面title标题优化

    在seo优化中,标题的优化占着举足轻重的地位,无论是从用户体验的角度出发,还是从搜索引擎的排名效果出发,title标题都是页面优化最最重要的因素.笔者总结了优化title标题应该注意的六个方面: ①. ...

  7. Spring3 +mybatis3 之 MapperScannerConfigurer

    之前一直使用"org.mybatis.spring.mapper.MapperFactoryBean"这个类在spring中配置mybatis的dao接口,后来发现如果dao太多话 ...

  8. mjpg-streamer on raspberrypi

    http://sourceforge.net/projects/mjpg-streamer/ svn address svn checkout svn://svn.code.sf.net/p/mjpg ...

  9. ASP.NET控件Button (e.CommandArgument的使用方法)

    e.CommandArgument的使用方法 1. 在 Web 窗体页上显示普通按钮 (Button) 控件. <asp:Button id="MyButton" Text= ...

  10. Android 各个版本WebView

    转载请注明出处   http://blog.csdn.net/typename/ powered by miechal zhao : miechalzhao@gmail.com 前言: 根据Googl ...