题目链接:点击打开链接

题意比較明显,不赘述。

删除时能够把i-1转到根,把i+1转到根下

则i点就在 根右子树 的左子树,且仅仅有i这一个 点

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 300500
#define inf 10000000
#define L(x) tree[x].ch[0]
#define R(x) tree[x].ch[1]
#define Father(x) tree[x].fa
#define Size(x) tree[x].size
#define Val(x) tree[x].val
#define Left(x) tree[x].left
#define Right(x) tree[x].right
#define Sum(x) tree[x].sum
#define Ans(x) tree[x].ans
struct node{
int ch[2],fa,size;
int val, left, right, sum, ans;
}tree[N];
int tot, root;
int a[N];
void Newnode(int& id, int val, int fa){
node E={0,0,fa,1,val,val,val,val,val};
id = tot++;
tree[id] = E;
}
void push_up(int id){
Size(id) = Size(L(id))+Size(R(id))+1;
Sum(id) = Sum(L(id)) + Sum(R(id)) + Val(id);
Left(id) = max(Left(L(id)), Sum(L(id))+Val(id)+max(Left(R(id)), 0));
Right(id) = max(Right(R(id)), Sum(R(id))+Val(id)+max(Right(L(id)), 0));
Ans(id) = max(Ans(L(id)), Ans(R(id)));
Ans(id) = max(Ans(id), Val(id)+max(Right(L(id)),0)+max(Left(R(id)),0));
}
void push_down(int id){}
void Rotate(int id, int kind){
int y = Father(id);
push_down(id); push_down(y);
tree[y].ch[kind^1] = tree[id].ch[kind];
Father(tree[id].ch[kind]) = y;
if(Father(y))
tree[Father(y)].ch[R(Father(y))==y] = id;
Father(id) = Father(y);
Father(y) = id;
tree[id].ch[kind] = y;
push_up(y);
}
void Splay(int id, int goal){
push_down(id);
while(Father(id)!=goal){
int y = Father(id);
if(Father(y)==goal)
Rotate(id,L(y)==id);
else {
int kind = L(Father(y))==y;
if(tree[y].ch[kind]==id)
{
Rotate(id, kind^1);
Rotate(id, kind);
}
else
{
Rotate(y, kind);
Rotate(id, kind);
}
}
push_down(id);
}
if(goal==0)root = id;
push_up(id);
}
int Get_kth(int k){
int id = root;
push_down(id);
while(Size(L(id))!=k){
if(Size(L(id))>k)
id = L(id);
else {
k -= (Size(L(id)) +1);
id = R(id);
}
push_down(id);
}
return id;
} int Getmax(int id){
push_down(id);
while(R(id)){
id = R(id);
push_down(id);
}
return id;
}
void Delete(int id){
int a = Get_kth(id-1);
int b = Get_kth(id+1);
Splay(a, 0);
Splay(b, root);
L(b) = 0;
push_up(b);
push_up(a);
}
int build(int l, int r, int& id, int fa){
if(l>r)return 0;
int mid = (l+r)>>1;
Newnode(id, a[mid], fa);
build(l, mid-1, L(id), id);
build(mid+1, r, R(id), id);
push_up(id);
}
int n, que;
char s[2];
void init(){
Father(0) = L(0) = R(0) = Size(0) = 0;
Sum(0) = 0;
Val(0) = Left(0) = Right(0) = Ans(0) = -inf;
root = tot = 1;
Newnode(root, -inf, 0);
Newnode(R(root), -inf, root);
build(1, n, L(R(root)), R(root));
push_up(R(root)); push_up(root);
} int main(){
int i, j;
while(~scanf("%d",&n)){
for(i = 1; i <= n; i++)scanf("%d",&a[i]);
init();
scanf("%d",&que);
while(que--){
scanf("%s",s);
if(s[0]=='I')
{
scanf("%d %d",&i,&j);
Splay(Get_kth(i), 0);
Splay(Getmax(L(root)), root);
Newnode(R(L(root)), j, L(root));
push_up(L(root));
push_up(root);
}
else if(s[0]=='Q')
{
scanf("%d %d",&i,&j);
Splay(Get_kth(i-1), 0);
Splay(Get_kth(j+1), root);
printf("%d\n", Ans(L(R(root))));
}
else if(s[0]=='D')
{
scanf("%d",&i);
Delete(i);
}
else if(s[0]=='R')
{
scanf("%d %d",&i,&j);
Splay(Get_kth(i), 0);
Val(root) = j;
push_up(root);
}
}
}
return 0;
}
/*
5
3 -4 3 -1 6
99
I 6 2
Q 3 5
R 5 -4
Q 3 5
D 2
Q 1 5
I 2 -10
Q 1 6
R 2 -1
Q 1 6
Q 1 6
*/

SPOJ 4487. Can you answer these queries VI 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. GSS6 4487. Can you answer these queries VI splay

    GSS6 Can you answer these queries VI 给出一个数列,有以下四种操作: I x y: 在位置x插入y.D x  : 删除位置x上的元素.R x y: 把位置x用y取替 ...

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

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

  4. SPOJ GSS6 Can you answer these queries VI

    Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...

  5. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  6. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  7. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  8. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  9. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

随机推荐

  1. Thymeleaf学习记录(4)--$/*/#/@语法

    表达式符号 Thymeleaf对于变量的操作主要有$\*\#三种方式: 变量表达式: ${...},是获取容器上下文变量的值. 选择变量表达式: *{...},获取指定的对象中的变量值.如果是单独的对 ...

  2. 原型链继承中的prototype、__proto__和constructor的关系

    前不久写了有关原型链中prototype.__proto__和constructor的关系的理解,这篇文章说说在原型链继承中的prototype.__proto__和constructor的关系. 通 ...

  3. JDBC基本操作示例

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  4. IEC62304开发过程框架

    软件开发计划的任务 制定整体软件开发计划 制定设计和开发计划 规划软件开发的工具.标准和方法(Class C) 制定软件集成和集成计划 制定软件验证计划 制定软件风险管理计划 制定配置管理计划 软件需 ...

  5. 微信小程序 - 弹出键盘遮挡住输入框

    在开发微信小程序的时候遇到,输入用户名或者手机号以及地址,手机键盘调起来,会把输入框遮挡. 如图: 以上两张图是自己工作中遇到的,此处不要着急,一个属性帮你搞定. cursor-spacing:指定光 ...

  6. 《IT老外在中国》第11期:首次访华的编程巨匠、C#之父Anders

    见到Anders的时候,他正专注的倾听国内开发者的提问,一副远视眼镜斜歪着架在头顶,宽松的深蓝色休闲毛衫随意套在白色圆领T恤外. 如果不是他那专注的神情,以及现场见证开发者对他的狂热崇拜,很难想象这位 ...

  7. linux-软件下载安装

    RPM 安装 rpm -ivh 包全名 :-i(install):安装:-v(verbose):显示详细信息:-h(hash):显示进度: rpm -Uvh 包全名:-U(upgrate):升级 rp ...

  8. [翻译] KGModal

    KGModal KGModal is an easy drop in control that allows you to display any view in a modal popup. The ...

  9. Exchange2016 & Skype for business 集成之一配置合作伙伴应用程序

    准备条件 为Skype for Business Server和Exchange Server之间建立服务器到服务器的身份验证,您必须做两件事:1)您必须为每台服务器分配合适的证书(详细参考文档htt ...

  10. MP4个人制作