SPOJ - QMAX3VN 一个动态的序列 ,在线询问某个区间的最大值。关于静态序列的区间最值问题,用ST表解决,参考POJ 3264

乍一看上去 splay可以轻松解决。书上说可以用块状链表解决,也没说具体怎么做。我也想不出来。

直接给splay代码吧,比较裸,这道题常数卡的有点紧,要注意优化。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=400000,h=1e+9; int Root,n,m;
struct Node
{
int val,cnt,size;
bool rev;
int father;int lson;int rson; int hei;int hest;
Node(int v,int height,int c,int fa,int l,int r)
{this->val=v;this->hei=height;this->hest=height;this->cnt=c;this->father=fa;this->lson=l;this->rson=r;rev=false;}
}tren[maxn]=Node(0,0,0,0,0,0); queue<int> memq; int newnode()
{
int loc=memq.front();memq.pop();
return loc;
} void dele(int loc)
{
memq.push(loc);
return ;
} void pushdown(int x)
{
if(x==0)return ;
if(tren[x].rev)
{
tren[x].rev=false;
swap(tren[x].lson,tren[x].rson);
if(tren[x].lson!=0)tren[tren[x].lson].rev=!tren[tren[x].lson].rev;
if(tren[x].rson!=0)tren[tren[x].rson].rev=!tren[tren[x].rson].rev;
}
} void update(int x)
{
tren[x].size=tren[x].cnt;
tren[x].hest=tren[x].hei;
if(tren[x].lson!=0){tren[x].size+=tren[tren[x].lson].size;tren[x].hest=max(tren[x].hest,tren[tren[x].lson].hest);}
if(tren[x].rson!=0){tren[x].size+=tren[tren[x].rson].size;tren[x].hest=max(tren[x].hest,tren[tren[x].rson].hest);}
} void zig(int x)
{
int fa=tren[x].father;
pushdown(fa);pushdown(x);
if(tren[tren[fa].father].lson==fa){tren[tren[fa].father].lson=x;}
else {tren[tren[fa].father].rson=x;}
tren[x].father=tren[fa].father;
tren[fa].father=x;
tren[fa].lson=tren[x].rson;
tren[tren[x].rson].father=fa;
tren[x].rson=fa;
update(tren[x].rson);update(x);//update(tren[x].father);
//swap(tren[fa],tren[x]);
} void zag(int x)
{
int fa=tren[x].father;
pushdown(fa);pushdown(x);
if(tren[tren[fa].father].lson==fa){tren[tren[fa].father].lson=x;}
else {tren[tren[fa].father].rson=x;}
tren[x].father=tren[fa].father;
tren[fa].father=x;
tren[fa].rson=tren[x].lson;
tren[tren[x].lson].father=fa;
tren[x].lson=fa;
update(tren[x].lson);update(x);//update(tren[x].father);
//swap(tren[fa],tren[x]);
} void splay(int root,int now)//核心
{
if(root==0||now==0)return ;
int end=tren[root].father;
while(tren[now].father!=end)
{
if(tren[now].father==root)
{
if(tren[tren[now].father].lson==now)zig(now);
else zag(now);
return ;
}
int fa=tren[now].father;int grand=tren[fa].father;
if(tren[grand].lson==fa)
{
if(tren[fa].lson==now){zig(fa);zig(now);continue;}
else{zag(now);zig(now);continue;}
}
else{
if(tren[fa].rson==now){zag(fa);zag(now);continue;}
if(tren[fa].lson==now){zig(now);zag(now);continue;}
}
}
return ;
} int insert(int fa,int root,int value)
{
int ans;
pushdown(root);
if(root==0)
{
root=newnode();
tren[root]=Node(value,0,1,fa,0,0);
update(root);
ans= root;
//splay(1,root);
}
if(tren[root].val==value)
{tren[root].cnt++;update(root);ans=0;}
if(tren[root].val>value)
{
if(tren[root].lson==0)
{
tren[root].lson=newnode();
tren[tren[root].lson]=Node(value,0,1,root,0,0);
update(tren[root].lson);
// splay(1,tren[root].lson);
ans= tren[root].lson;
}
else ans= insert(root,tren[root].lson,value);
}
else
{
if(tren[root].rson==0)
{
tren[root].rson=newnode();
tren[tren[root].rson]=Node(value,0,1,root,0,0);
update(tren[root].rson);
//splay(1,tren[root].rson);
ans= tren[root].rson;
}
else ans= insert(root,tren[root].rson,value);
}
update(root);
return ans;
} int access(int root,int key)
{
pushdown(root);
if(tren[root].val==key)return root;
if(tren[root].val<key)return access(tren[root].rson,key);
else return access(tren[root].lson,key);
} int Max(int root)
{
pushdown(root);
if(root==0||tren[root].rson==0)return root;
else return Max(tren[root].rson);
} int join(int l,int r)
{
if(l==0)return r;
if(r==0)return l;
int max=Max(l);
splay(l,max);
tren[max].rson=r;
return max;
} void erase(int root,int key)
{
int now=access(root,key);
int fa=tren[now].father;
if(tren[now].cnt>1){tren[now].cnt--;return ;}
else
{
if(tren[fa].lson==now){tren[fa].lson=join(tren[now].lson,tren[now].rson);}
else{tren[fa].rson=join(tren[now].lson,tren[now].rson);}
}
return ;
} int getKth(int root,int k)
{
while(root!=0&&k<=tren[root].size)
{
pushdown(root);
int ls=tren[root].lson,rs=tren[root].rson;
if(ls!=0&&k<=tren[ls].size){root=ls;continue;}
k-=tren[root].cnt;
if(ls!=0)k-=tren[ls].size;
if(k<=0){return root;}
root=rs;
}
} void maintain(int now,int val)
{
while(now!=0)
{
tren[now].size+=val;
now=tren[now].father;
}
}
vector<int>ans;
void print( int root)
{
if(root==0)return ;
pushdown(root);
print(tren[root].lson);
if(tren[root].val<=n&&tren[root].val>=1)ans.push_back(tren[root].val);
print(tren[root].rson);
} int main()
{freopen("t.txt","r",stdin);
//freopen("1.txt","w",stdout);
scanf("%d",&n);
int tot=3;
tren[1]=Node(0,-1,1,0,0,0);
Root=1;
tren[2]=Node(0,-1,1,0,0,0);
tren[1].rson=2;
tren[2].father=1;
update(2);update(1); int k=0;
for(int i=0;i<n;i++)
{
char c;int a,b,l,r;
scanf("%s%d%d",&c,&a,&b);
if(c=='A')
{
a+=h;
l=b;
r=b+1;
int klo=getKth(Root,l);
int klb,newson;
if(tren[klo].rson!=0)
{ klb=getKth(tren[klo].rson,1);
newson=tren[klb].lson=tot++; }else{ klb=klo; newson=tren[klb].rson=tot++;}
tren[newson]=Node(1,a,1,klb,0,0);
klo=newson;
while(klo!=0)
{
update(klo);
klo=tren[klo].father;
} splay(Root,newson);Root=newson;
}
if(c=='Q')
{
l=a;
r=b+2;
int klo=getKth(Root,l);
splay(Root,klo);Root=klo;
klo=getKth(Root,r);
splay(tren[Root].rson,klo);
printf("%d\n",tren[tren[klo].lson].hest-h);
//splay(Root,tren[klo].lson);Root=tren[klo].lson;
}
} return 0;
}

  

SPOJ - QMAX3VN (4350) splay的更多相关文章

  1. spoj 4487. Can you answer these queries VI (gss6) splay 常数优化

    4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...

  2. SPOJ 4487 Splay 基本操作

    插入操作,删除操作和置换操作都是单点的,所以不需要lazy标记.这个很简单,都是两次RotateTo,一次Splay操作就搞定. 求最大连续字段和的操作和线段树的题目类似,只需要保存最左边的连续最大字 ...

  3. SPOJ 4487. Can you answer these queries VI splay

    题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...

  4. SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)

    You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...

  5. SPOJ GSS6 Can you answer these queries VI ——Splay

    [题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...

  6. SPOJ 3273

    传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 Treap 版 //SPOJ 3273 //by Cydiater //2016.8.31 #include < ...

  7. SPOJ QTREE 系列解题报告

    题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...

  8. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  9. SPOJ QTREE4 lct

    题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...

随机推荐

  1. ThinkPHP---辅助方法

    [三]Tp常见的辅助方法 原生SQL语句里除了目前所使用的基本操作增删改查,还有类似于group.where.order.limit等这样的字句. ThinkPHP封装了相应的子句方法:封装的方法都在 ...

  2. node 实现Token状态登录 及数据库增删改查

    1.项目目录结构 2.启动入口文件代码index.js const express = require('express') const bodyParser = require('body-pars ...

  3. [BOI2008]Elect 选举

    背包. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstri ...

  4. 安装ubuntu系统空间分配问题

    以下是我安装linux系统(ubuntu)时的系统空间配置,以50G为例: 挂载点 大小 格式 分区类型 / 15G Ext4 主分区 /home 30G Ext4 逻辑分区 /boot 1G Ext ...

  5. Ztree加载完成默认选中根节点右侧生成表格

    需求:页面加载完成之后,默认选中ztree的根节点,并执行其点击方法,右侧生成表格: 效果:如下图所示: 思路:在节点点击事件clickNode方法中根据节点的部门code查询这个部门下的所有员工,并 ...

  6. airfoil polar data during post stall stages (high AOA)

    airfoil polar data during post stall stages (high AOA) Table of Contents 1. airfoil polar during pos ...

  7. 洛谷 3979 BZOJ 3083 遥远的国度

    [题解] 这道题除去根操作就是普通的树链剖分了.但是有换根操作怎么处理呢? 我们可以发现如果现在的根不在查询的点的子树里,那么对本次查询没有影响.如果现在的跟在查询的点x的子树里,那么答案将变为整棵树 ...

  8. Windows学习总结(9)——Windows系统常用的网络控制指令

    ping 命令式用来测试TCP/IP 网络是否畅通或者网络连接速度的命 令,其原理是根据计算机唯一标示的IP 地址,当用户给目的地址发 送一个数据包时,对方就会返回一个同样大小的数据包,根据返回的 数 ...

  9. java多线程编程核心技术(三)--线程间通信

    1.等待/通知机制 1.wait()方法:使当前执行代码的线程进行等待.wait()方法是Object类的方法,该方法将当前线程放入“预执行队列”中,并在wait()所处的代码行处停止执行.只到被唤起 ...

  10. [BZOJ1045][HAOI2008]糖果传递(数学分析)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1045 分析:均分纸牌的环状版本. 先看线性的版本: 设f[i]表示第I位从第i+1位得 ...