SPOJ 4487. Can you answer these queries VI splay
题目链接:点击打开链接
题意比較明显,不赘述。
删除时能够把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的更多相关文章
- 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 ...
- 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取替 ...
- SPOJ GSS6 Can you answer these queries VI ——Splay
[题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...
- 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 ...
- 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 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- 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 ...
- 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 ...
随机推荐
- Html5 填表 表单(二) input type 各种输入, 各种用户选择,上传等等泛输入用户交互
<input> 无限制输入 type 限制输入 type = 如下类型 type 后还可以跟一些属性: 如<input type=text max ...
- html5-audio 播放列表和自动播放
一个简单audio的列表和播放小例子 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- js点击事件在苹果端失效的问题
在安卓机上,我们随意定义点击事件也能够在找到点击的元素,但是在苹果端上就是不行,怎么点击都没有效果.这是因为在苹果机上window禁止了手指误点功能,必须解除这一功能,或者给点击事件指引某个元素上绑定 ...
- vue2 入门 教程 单页应用最佳实战[*****]
推荐 vue2 入门 教程 -------- 看过其他的,再看作者的,很赞 vue2 入门 教程 单页应用最佳实战 : 具体在 https://github.com/MeCKodo/vue-tuto ...
- .NET AOP微型框架发布 --CleanAOP
CleanAOP--简介 作者:立地(欧文) 邮箱:jarvin_g@126.com 导语: AOP为Aspect Oriented Programming的缩写. 意为:面向切面编程.将日志记录,性 ...
- Codeforces Round #419 A+B
A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes Karen is ...
- Android SD卡上文件
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- 自定义BaseActivity
思路很简单:将软件里用到的大量重复的页面布局抽象出来,编写成一个抽象的Activity类,然后在实现具体页面时继承它,并且在主内容空白区填入需要的内容. 例如在最近开发的一款资讯类应用中,每张页面上面 ...
- flask介绍
安装flask pip3 install flask 短小精悍.可扩展强 的一个Web框架. 牛逼点:上下文管理机制 依赖wsgi: werkzeug(相当于Django的wsgi):只要安装flas ...
- 原生mybaits学习笔记
目录 简介 maven引入 核心配置文件 方式一(简单) 配置 操作 方式二(old) 创建接口 配置mapper.xml 实现接口 操作 方式三(常用) 创建接口 配置mapper.xml 操作 配 ...