题目链接:点击打开链接

题意比較明显,不赘述。

删除时能够把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. String 简单使用

    package com.direct.str; public class TestObject { /** * @param args */ /* * 1.object类是根类,里面定义的==和equ ...

  2. java 包装类和基础数据

    package com.tercher.demo; public class LangClass { public static void main(String[] args) { //所有的包装类 ...

  3. 用fritzing绘制arduino硬件连线图

    在http://fritzing.org/home/ 点击下载最新版本. 解压之后直接可以使用. 打开Fritzing.exe 在点击面包板,在搜索界面输入想要找到的原件拖拽即可放在面包板所在的图上. ...

  4. input textarea监听鼠标粘贴

    发现一个问题,在input/textarea中如果是鼠标粘贴内容进去,发现判断不了value的改变,html代码如下: <!doctype html> <html> <h ...

  5. 旋转/非旋转treap的简单操作

    treap(树堆) 是在二叉搜索树的基础上,通过维护随机附加域,使其满足堆性质,从而使树相对平衡的二叉树: 为什么可以这样呢? 因为在维护堆的时候可以同时保证搜索树的性质: (比如当一棵树的一个域满足 ...

  6. HTML中的图片

    在一开始时,Web仅有文本,那真的是很无趣.幸运的是,没过多久网页上就能嵌入图片和其他有趣的内容了.虽然还有许多其他类型的多媒体,但是从地位比较低的<img>元素开始是符合逻辑的,它常常被 ...

  7. MySQL数据库(6)----配置文件 my.cnf 的使用

    1. 使用源码安装好MySQL后,其配置文件一般位于 /usr/local/my.cnf,可以使用如下命令查看查看配置文件的搜索顺序: root@javis:~$ mysqld --help --ve ...

  8. Vue之自定义组件的v-model

    最近在学习vue,今天看到自定义事件的表单输入组件,纠结了一会会然后恍然大悟...官方教程写得不是很详细,所以我决定总结一下. v-model语法糖 v-model实现了表单输入的双向绑定,我们一般是 ...

  9. Delphi XE7功能之TMultiView

    TMultView,做为一个TPanel来显示控件,可通过属性Mode来控制TMultView的显示效果,如下拉或者以抽屉方式.从屏一侧象抽屉一样显示TMultView,但不会转换主屏,也就是说在主窗 ...

  10. 打通版微社区(3):在Web服务器上部署memcache For DZ3.2

    写在前面:首先这个数据库加速程序的原理,是将数据库内容缓存到Web服务器的内存上,也就是内存换速度.我本次微社区的应用其实应该用不了这个,只是看到好多DZ论坛部署的都安装了这个,我就练手一下以便不时之 ...