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 ...
随机推荐
- smarty模板引擎基础(二)
smarty模板引擎所需文件夹共可分为存放页面缓存的(cache).存放配置文件的(configs).存放模板扩充插件的(plugins).存放模板文件的(templates).存放编译后的模板文件的 ...
- laravel基础课程---1、laravel安装及基础介绍(laravel如何安装)
laravel基础课程---1.laravel安装及基础介绍(laravel如何安装) 一.总结 一句话总结: [修改composer镜像地址].[明确laravel的安装要求].[安装指定版本的la ...
- html5--5-12 渐变色
html5--5-12 渐变色 学习要点 掌握渐变色的绘制方法 渐变色绘制方法 createLinearGradient() 创建线性渐变 createLinearGradient(x1,y1,x2, ...
- Swing项目编译成exe,并且打包成安装文件(二)
前面我们讲到了将Swing项目编译成双击可执行的文件exe,这篇我就教大家怎么把exe打包成需要在电脑安装的那种,首先需要一个工具,Inno Setup 编译器, 下载地址,我这个是汉化版的,双击打开 ...
- cnn汉字识别 tensorflow demo
# -*- coding: utf-8 -*- import tensorflow as tf import os import random import tensorflow.contrib.sl ...
- 浏览器端JS导出EXCEL——001
<script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type=" ...
- JDBC 笔记3 通过PreparedStatement 对数据库进行增删改查 (转载)
之前MVC时一直用它,学了框架后就没怎么用了.这里转载一位同学的博客,以后可能也会用到的. 转自:https://www.cnblogs.com/zilong882008/archive/2011/1 ...
- HDU1852 Beijing 2008(快速幂+特殊公式)
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...
- php之创建jsonp接口调数据
jsonp跨域访问数据 首先在php内写jsonp调用接口 <?php//设置头部文件类型为json header('Content-type: application/json'); //将r ...
- fragment error
error: android.view.InflateException: Binary XML file line #6: Error inflating class fragment 解决办法 ...