[HDU 1754] I Hate It Splay
来源:互联网 发布:淘宝国际商城 编辑:程序博客网 时间:2024/06/04 22:26
http://acm.hdu.edu.cn/showproblem.php?pid=1754
题意:略
继续splay。。。。
#include <cstdio>#include <cstring>#include <iostream>using namespace std;/const int maxn = 200005;struct Tree{ int val, maxval; int pre, chd[2];};int n, m;Tree tr[maxn];int F(int rt){ return tr[tr[rt].pre].chd[1] == rt;}void PushUp(int rt) //向上更新{ tr[tr[rt].chd[0]].pre = rt; tr[tr[rt].chd[1]].pre = rt; tr[rt].maxval = tr[rt].val; if(tr[rt].maxval < tr[tr[rt].chd[0]].maxval){ //更新最大值 tr[rt].maxval = tr[tr[rt].chd[0]].maxval; } if(tr[rt].maxval < tr[tr[rt].chd[1]].maxval){ //更新最大值 tr[rt].maxval = tr[tr[rt].chd[1]].maxval; }}void Rotate(int rt) //旋转,将rt转到它父结点的位置{ int chd = F(rt); //判断自己是哪一个孩子 int pre = tr[rt].pre; tr[rt].pre = tr[pre].pre; tr[tr[pre].pre].chd[F(pre)] = rt; tr[pre].chd[chd] = tr[rt].chd[!chd]; tr[rt].chd[!chd] = pre; PushUp(pre); PushUp(rt);}int Splay(int rt, int rw){ while(tr[rw].pre != rt){ int pre = tr[rw].pre; if(F(rw) != F(pre) || tr[pre].pre == rt){ //单旋转 Rotate(rw); } else{ //双旋转 Rotate(pre); Rotate(rw); } } if(rt) //防止更新0 PushUp(rt); return rw;}int BuildTree(int Size){ for(int i = 0; i <= Size; i++){ tr[i].pre = i - 1; tr[i].chd[0] = 0; tr[i].chd[1] = i + 1; } tr[0].pre = 0; tr[0].val = tr[0].maxval = 0; tr[1].pre = 0; tr[1].val = tr[1].maxval = 0; tr[Size].chd[1] = 0; tr[Size].val = tr[Size].maxval = 0; return 1;}int main(){ int rt; while(~scanf("%d%d", &n, &m)){ for(int i = 1; i <= n; i++){ scanf("%d", &tr[i+1].val); } rt = BuildTree(n + 2); rt = Splay(0, n + 2); int x, y; char str[5]; while(m--){ scanf("%s%d%d", str, &x, &y); if(str[0] == 'U'){ rt = Splay(0, x + 1); tr[rt].val = y; PushUp(rt); } else{ if(x > y) swap(x, y); rt = Splay(0, x); Splay(rt, y + 2); printf("%d\n", tr[tr[tr[rt].chd[1]].chd[0]].maxval); } } } return 0;}
0 0
- hdu 1754 I Hate It (Splay)
- 【Splay】 HDU 1754 I Hate It
- [HDU 1754] I Hate It Splay
- hdu 1754 I Hate It splay
- hdu 1754 I Hate It(splay入门题)
- hdu 1754 I Hate It (splay tree伸展树)
- HDU 1754 I Hate It (Splay tree和Segment tree)
- 【线段树 Splay】 hdu 1754 I Hate It
- HDU-1754:I Hate It(splay结构体做法)
- I Hate It(splay)
- HDU1754 I Hate It (Splay)
- hdu 1754 I Hate It
- HDU 1754 I Hate It
- hdu 1754 I Hate It
- HDU 1754 I Hate It
- hdu 1754 I Hate It
- HDU 1754 I Hate It .
- HDU 1754 i hate it
- Lua提取 XXXX="YYYY" 赋值
- SQL中的case when then else end用法
- mysql输入密码闪退
- EditText 密码显示 隐藏三种方法
- jQuery 动画 - animate() 方法简介
- [HDU 1754] I Hate It Splay
- 2015年需要了解的前端框架和语言
- ANDROID L——Material Design详解(主题和布局)
- PHP文件上传与下载
- Java POI 读取Office excel (2003,2007)及相关jar包
- ListView(自定义适配器)
- ANDROID L - Material Design详解(视图和阴影)
- jquery draggable
- 优化后的冒泡排序