hdu1754splaytree区间查询
以前用线段树做的题。。发现splay好神奇
splay的区间查询就是把那个区间移到两个节点之间进行操作即可,同时每次rotate不要忘记pushup
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 200050 int pre[maxn],ch[maxn][],size[maxn],Max[maxn],nums[maxn],keys[maxn],tot,root;
int a[maxn],n,q; inline void pushup(int r){
Max[r]=max(max(Max[ch[r][]],Max[ch[r][]]),keys[r]);
size[r]=size[ch[r][]]+size[ch[r][]]+;
}
inline void newnode(int &r,int fa,int val){
r=++tot;
ch[r][]=ch[r][]=;
size[r]=;
Max[r]=nums[r]=keys[r]=val;
pre[r]=fa;
}
void build(int &r,int L,int R,int fa){
if(L>R) return;
int mid=L+R>>;
newnode(r,fa,a[mid]);
build(ch[r][],L,mid-,r);
build(ch[r][],mid+,R,r);
pushup(r);
}
void init(){
root=tot=;
ch[root][]=ch[root][]=size[root]=pre[root]=;
newnode(root,,-);newnode(ch[root][],root,-);
build(ch[ch[root][]][],,n,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
void rotate(int x,int kind){
int fa=pre[x];
ch[fa][!kind]=ch[x][kind];
pre[ch[x][kind]]=fa;
pre[x]=pre[fa];
if(pre[fa])
ch[pre[fa]][ch[pre[fa]][]==fa]=x;
pre[fa]=x;
ch[x][kind]=fa;
pushup(fa);pushup(x);
}
void splay(int r,int goal){
while(pre[r]!=goal){
if(pre[pre[r]]==goal){
rotate(r,ch[pre[r]][]==r);
}
else {
int fa=pre[r];
int kind=ch[pre[fa]][]==fa;
if(ch[fa][kind]==r){
rotate(r,!kind);
rotate(r,kind);
}
else {
rotate(fa,kind);
rotate(r,kind);
}
}
}
pushup(r);
if(goal==) root=r;
}
int getth(int r,int pos){
int t=size[ch[r][]]+;
if(t==pos) return r;
else if(t<pos) return getth(ch[r][],pos-t);
else return getth(ch[r][],pos);
}
void update(int pos,int val){
int r=getth(root,pos);
splay(r,);
Max[root]=nums[root]=keys[root]=val;
pushup(root);
}
int query(int l,int r){
int L=getth(root,l),R=getth(root,r);
splay(L,);
splay(R,root);
return Max[ch[ch[root][]][]];
}
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d key = %2d\n",x,ch[x][],ch[x][],pre[x],size[x],keys[x]);
Treavel(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
int main(){
while(scanf("%d%d",&n,&q)==){
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
init(); int a,b;
char op[];
while(q--){
scanf("%s%d%d",&op,&a,&b);
if(op[]=='U')
update(a+,b);
else printf("%d\n",query(a,b+));
//debug();
}
}
return ;
}
hdu1754splaytree区间查询的更多相关文章
- Thinkphp查询 1.查询方式 2.表达式查询 3.快捷查询 4.区间查询 5.组合查询 6.统计查询 7.动态查询 8.SQL 查询
1.使用字符串作为条件查询 $user = M('User'); var_dump($user->where('id=1 AND user="蜡笔小新"')->sele ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)
题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...
- POJ3237-Tree (树链剖分,线段树区间更新+点更新+区间查询)
两个更新操作,一个将第i条路径权值改为w,一个是将a-b之间所有路径权值取反. 一个查询操作,求a-b之间路径中权值最大的边. 很容易想到维护一个最大最小值,取反就是把最大最小取反交换一下. 开始遇到 ...
- ZOJ 3633 Alice's present 倍增 区间查询最大值
Alice's present Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...
- CF390-E. Inna and Large Sweet Matrix(区间更新+区间查询)
题意很好理解,不说了 题解就是每次把值压缩成一维,比如x上,这样就可以求出任意宽度的整个竖条的和. 如这张图,求的是s5-(s1+s3+s7+s9) 因为可以求出一整竖条和一整横条,我们可以求出是s2 ...
- tyvj P1716 - 上帝造题的七分钟 二维树状数组区间查询及修改 二维线段树
P1716 - 上帝造题的七分钟 From Riatre Normal (OI)总时限:50s 内存限制:128MB 代码长度限制:64KB 背景 Background 裸体就意味着 ...
- 【CF689D Friends and Subsequences】二分搜索,区间查询
题意:给定两个整数序列a,b,将a,b对齐,问有多少个区间满足a的区间内最大值等于b的区间内最小值. 数据范围:区间长度n属于[1, 200000],序列中的元素在整型范围内 思路:枚举所有n*(n+ ...
随机推荐
- Socket 连接建立过程
阻塞模式下: 1,客户端向服务器端发起请求建立连接时,服务器端只需要运行到 serverSocket = ); 客户端注册的 SelectionKey.OP_CONNECT 事件就能够发生. 也就是 ...
- html5 实时监听输入框值变化的完美方案:oninput & onpropertychange
结合 HTML5 标准事件 oninput 和 IE 专属事件 onpropertychange 事件来监听输入框值变化. H5手机端: <input type="text" ...
- POJ1258 Agri-Net【最小生成树】
题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离. 思路: 又是一个最小生成树,因为 ...
- 青云VPC网络配置
1 创建VPC网络 2 申请公网IP 3 回到VPC图形界面绑定公网ip 4 创建私有网络,并绑定私有网络 5 创建3台主机 6 新建防火墙,并绑定到VPC 7 配置VPC端口转发规则 8 添加防火墙 ...
- mysql gtid 第一篇
GTID1 简介 就是全局事务ID(global transaction identifier )2 构成 uuid+transaction_id 3 格式 7a07cd08-ac1b-11 ...
- D- 泛型练习 ,继承,方法
unit Unit3; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- 如何用ModelsimSE仿真IP核-以PLL为例
我们之前介绍了如何使用Modelsim SE进行仿真和利用do文件的仿真方法,但是其中待仿真的模块是我们自己编写的Verilog模块,但是在实际工作中,我们的设计中会经常用到FPGA厂商给我们提供的现 ...
- vsftpd控制用户禁止访问上级目录 只能访问自己目录
涉及文件: vsftpd.conf chroot_list_file=/etc/vsftpd.chroot_list 如果设置为 chroot_local_user=YES chroot_list_e ...
- AF_INET域与AF_UNIX域socket通信原理对比【转】
转自:https://www.cnblogs.com/lfxiao/p/9672797.html 1. AF_INET域socket通信过程 典型的TCP/IP四层模型的通信过程. 发送方.接收方依 ...
- MySQL 误操作后数据恢复(update,delete忘加where条件)【转】
在数据库日常维护中,开发人员是最让人头痛的,很多时候都会由于SQL语句 写的有问题导致服务器出问题,导致资源耗尽.最危险的操作就是在做DML操作的时候忘加where条件,导致全表更新,这是作为运维或者 ...