题目链接:点击打开链接

题意比較明显,不赘述。

删除时能够把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. thinkphp I() 方法

    I() 方法用于获取前台提交的表单的value值. <form id="complai_form" method="post" action=" ...

  2. python安装后无法用cmd命令pip 装包

    出现问题: 原因:没有添加环境变量. 解决方法:将python安装目录下的Script目录添加进环境变量,其中有pip.exe,在cmd中输入pip install命令时要运行pip.exe. win ...

  3. js类的笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 转:在使用angularjs过程,ng-repeat中track by的作用

    转载:链接 <div ng-repeat="links in slides"> <div ng-repeat="link in links track ...

  5. (PowerShell) 重命名文件

    Get-ChildItem -Path C:\temp\test -Filter *.txt | Rename-Item -NewName {$_.Basename.Replace("Old ...

  6. CAS服务器集群和客户端集群环境下的单点登录和单点注销解决方案

    CAS的集群环境,包括CAS的客户应用是集群环境,以及CAS服务本身是集群环境这两种情况.在集群环境下使用CAS,要解决两个问题,一是单点退出(注销)时,CAS如何将退出请求正确转发到用户sessio ...

  7. Android BitmapFactory.Options

    public Bitmap inBitmap 如果设置,解码选项“对象的方法,采取将尝试重用这个位图加载内容时. public int inDensity 使用的位图的象素密度. public boo ...

  8. IDEA创建maven项目时,maven太慢-archetypeCatalog=internal

    创建项目时候加上archetypeCatalog=internal 参数, archetypeCatalog表示插件使用的archetype元数 据,不加这个参数时默认为remote,local,即中 ...

  9. 【转】snmpwalk常用用法

    在日常监控中,经常会用到snmp服务,而snmpwalk命令则是测试系统各种信息最有效的方法,现总结一些常用的方法如下: 1.snmpwalk -v 2c -c public 10.103.33.1 ...

  10. Centos7下面安装eclipse

    下载Eclipse压缩包 下载路径 http://mirrors.neusoft.edu.cn/eclipse/technology/epp/downloads/release/luna/SR2/ec ...