hdu1754I Hate It(splay)
线段树的水题,拿来学习一下splay.
本题涉及到求最大值以及单点更新,折腾了许久,差不多把splay搞明白了。
按位置建树,按位置是一颗排序二叉树,对于区间的操作非常方便,每次操作都将需要的结点转自根的右孩子的左孩子,因为加了2个结点,一个最小的,一个最大的,据说是为了防止越界。
这题只有单点,所以每次将所求结点旋自根节点即可。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 200010
#define LL __int64
#define INF 0xfffffff
#define key_value ch[ch[root][1]][0]
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int pre[N],s[N],size[N];
int ch[N][],a[N],val[N];
int root,n,tot;
void newnode(int &x,int k,int fa)
{
x = ++tot;
ch[x][]=ch[x][] = ;
pre[x] = fa;
size[x] = ;
s[x] = k;
val[x] = k;
}
void pushup(int w)
{
size[w] = size[ch[w][]]+size[ch[w][]]+;
s[w] = max(max(s[ch[w][]],s[ch[w][]]),val[w]);
// cout<<s[w]<<" "<<w<<"// "<<" "<<ch[w][1]<<" "<<s[w]<<endl;
}
void rotate(int r,int kind)
{
int y = pre[r];
ch[y][!kind] = ch[r][kind];
pre[ch[r][kind]] = y;
if(pre[y])
{
ch[pre[y]][ch[pre[y]][]==y] = r;
}
pre[r] = pre[y];
ch[r][kind] = y;
pre[y] = r;
pushup(y);
//pushup(r);
}
void splay(int r,int goal)
{
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
rotate(r,ch[pre[r]][]==r);
}
else
{
int y = pre[r];
int kind = (ch[pre[y]][]==y);
if(ch[y][kind]==r)
{
rotate(r,!kind);
rotate(r,kind);
}
else
{
rotate(y,kind);
rotate(r,kind);
}
}
}
pushup(r);
if(goal==) root = r;
}
int get_k(int k)
{
int r = root;
while(size[ch[r][]]!=k)
{
if(size[ch[r][]]>k)
r = ch[r][];
else
{
k-=(size[ch[r][]]+);
r = ch[r][];
}
}
return r;
}
int query(int l,int r)
{
splay(get_k(l-),);
splay(get_k(r+),root);
return s[key_value];
}
/*void update(int l,int r,int k)
{
splay(get_k(l-1),0);
splay(get_k(r+1),root);
val[key_value] = k;
s[key_value] = k;
pushup(ch[root][1]);
pushup(root);
}*/
void update(int l,int r,int k)
{
int kk = get_k(l);
val[kk] = k;
splay(kk,);
}
void build(int &w,int l,int r,int fa)
{
if(l>r) return ;
int m = (l+r)>>;
newnode(w,a[m],fa);
build(ch[w][],l,m-,w);
build(ch[w][],m+,r,w);
pushup(w);
}
void init()
{
int i;
for(i = ;i < n ;i++)
scanf("%d",&a[i]);
ch[][] = ch[][] = pre[] = size[] = s[] = ;
root = tot =;
newnode(root,-,);
newnode(ch[root][],-,root);
size[root] = ;
build(key_value,,n-,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
int main()
{
int q,x,y,i;
char sq[];
while(scanf("%d%d",&n,&q)!=EOF)
{
init();
while(q--)
{
scanf("%s%d%d",sq,&x,&y);
if(sq[]=='U')
{
update(x,x,y);
/*for(i = 0; i <= n+2; i++)
cout<<ch[i][0]<<" "<<ch[i][1]<<" "<<i<<endl;
puts("");*/
}
else
{
printf("%d\n",query(x,y));
/*for(i = 0; i <= n+2; i++)
cout<<ch[i][0]<<" "<<ch[i][1]<<" "<<i<<endl;
puts("");*/
}
}
}
return ;
}
hdu1754I Hate It(splay)的更多相关文章
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- splay最终模板
来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...
- bzoj 3506 && bzoj 1552 splay
查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...
- 【splay】文艺平衡树 BZOJ 3223
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- 【填坑】bzoj3224 splay裸题
人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 #include <cstdio> ,n,p,q; ],c[][],size[],sp[]; void rot(i ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...
- HDU1890 Robotic Sort[splay 序列]
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- cnn汉字识别 tensorflow demo
# -*- coding: utf-8 -*- import tensorflow as tf import os import random import tensorflow.contrib.sl ...
- 【应用】SVG动态 时钟
没有做秒针,只做了分针和时针,5分钟以后来看应该可以看到效果╮(╯-╰)╭ <!DOCTYPE html> <html> <head> <title>& ...
- SPOJ Find the max XOR value(二进制,贪心即可)
You have two integers L and R, and you are required to find the max xor value of a and b where L < ...
- javascript之闭包,递归,深拷贝
闭包 理解:a函数执行后return出b函数且b函数可以访问a函数的数据 好处:子函数存储在复函数内部,子函数执行完不会被自动销毁 坏处:占用内存比较大 ex: function bibao(){ v ...
- JQ对象和原生DOM对象
相同点:两者本质上都是DOM元素. 不同点:JQ对象是在原生DOM对象上进行了一次封装,使开发人员使用起来更简洁.高效. 两者之间用法也完全不同,很说初学者经常混淆. 其实区分两者并不难, 1.语法不 ...
- Learning ReactNative (一) : JavaScript模块基本原理与用法
在使用ReactNative进行开发的时候,我们的工程是模块化进行组织的.在npmjs.com几十万个库中,大部分都是遵循着CommonJS规则的.在ES6中引入了class的概念,从此JavaScr ...
- 19.break和continue
break;语句: 1.可以在switch语句中,结束分支语句: 2.break:语句可以出现在单循环当中,默认情况下结束距离他最近的一个循环. 3.break 后面跟一个循环的名字可以结束你指定的这 ...
- 【旧文章搬运】关于NtUserBuildHwndList的一点记录~
原文发表于百度空间,2011-04-07========================================================================== 该函数与r ...
- c/c++面试30-38之指针
30 看代码写结果-----指针加减 #include <stdio.h> int main(void) { ] = { , , , , }; );//这里要特别注意,&a+1的值 ...
- spring mvc 处理映射的几种方式
1.Spring MVC bean的nameurl处理映射 <bean class="org.springframework.web.servlet.view.InternalReso ...