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;
}