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. Monitor All SQL Queries in MySQL (alias mysql profiler)

    video from youtube: http://www.youtube.com/watch?v=79NWqv3aPRI one blog post: Monitor All SQL Querie ...

  2. iOS 7隐藏statusbar

    - (BOOL)prefersStatusBarHidden { return YES; } [[UIApplication sharedApplication] setStatusBarHidden ...

  3. percona-xtrabackup安装及使用教程

    安装percona-xtrabackup依赖包(前提已安装mysql) yum -y install perl-CPAN perl-DBD-MySQL perl make gcc gcc-c++ pa ...

  4. SQLite入门与分析(六)---再谈SQLite的锁

    写在前面:SQLite封锁机制的实现需要底层文件系统的支持,不管是Linux,还是Windows,都提供了文件锁的机制,而这为SQLite提供了强大的支持.本节就来谈谈SQLite使用到的文件锁——主 ...

  5. More on 1Password’s Components

    Stefan van As of 1Password fame sent me a more exhaustive list of the libraries and tools used in 1P ...

  6. 156. Binary Tree Upside Down

    题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...

  7. WCF - Hosting WCF Service

    After creating a WCF service, the next step is to host it so that the client applications can consum ...

  8. 开发完整的Web项目必备

    开发工具 数据库系统 DB2数据库 Oracle数据库 SQL Server数据库 MySQL数据库 Access数据库 Web服务器 IIS BEA WebLogic Server Apache T ...

  9. Sublime Text SFTP 注册码,亲测有效,SVN注册码

    SFTP { "email": "xiaosong@xiaosong.me", "product_key": "d419f6-de ...

  10. [转] Java之ACM速成

    转载自:http://blog.csdn.net/skiffloveblue/article/details/7032290 相关题目:POJ 1131 1205 1220 1405 1503 160 ...