【题目分析】

增加了插入和删除。

直接用Splay维护就好辣!

写了一个晚上,(码力不精),最后发现更新写挂了

【代码】

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib> #include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 500005
#define eps 1e-8
#define db double
#define L ch[x][0]
#define R ch[x][1]
#define ll long long
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i) void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("wa.txt","w",stdout);
#endif
} int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} int ch[maxn][2],sum[maxn],lx[maxn],rx[maxn],mx[maxn],fa[maxn],siz[maxn],v[maxn];
int a[maxn],n,rt,q,tot; void update(int x)
{
siz[x]=siz[L]+siz[R]+1;
sum[x]=sum[L]+sum[R]+v[x];
mx[x]=lx[x]=rx[x]=v[x];
if (x==1)
{
mx[x]=mx[R];
lx[x]=lx[R];
rx[x]=rx[R];
return ;
}
if (x==n+2)
{
mx[x]=mx[L];
lx[x]=lx[L];
rx[x]=rx[L];
return ;
}
if (((!L))&&((!R))) lx[x]=rx[x]=mx[x]=sum[x]=v[x];
else if ((!L))
{
lx[x]=max(v[x],v[x]+lx[R]);
rx[x]=max(rx[R],sum[R]+v[x]);
mx[x]=max(v[x],max(mx[R],v[x]+lx[R]));
mx[x]=max(mx[x],max(lx[x],rx[x]));
}
else if ((!R))
{
lx[x]=max(lx[L],sum[L]+v[x]);
rx[x]=max(v[x],v[x]+rx[L]);
mx[x]=max(v[x],max(mx[L],rx[L]+v[x]));
mx[x]=max(mx[x],max(lx[x],rx[x]));
}
else
{
lx[x]=max(lx[L],max(sum[L]+v[x],sum[L]+v[x]+lx[R]));
rx[x]=max(rx[R],max(sum[R]+v[x],sum[R]+v[x]+rx[L]));
mx[x]=max(mx[L],max(mx[R],max(rx[L],0)+v[x]+max(lx[R],0)));
mx[x]=max(mx[x],max(lx[x],rx[x]));
}
} void rot(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1; r=l^1;
if (y==k) k=x;
else
{
if (ch[z][0]==y) ch[z][0]=x;
else ch[z][1]=x;
}
fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
ch[y][l]=ch[x][r];ch[x][r]=y;
update(y); update(x);
} void splay(int x,int &k)
{
while (x!=k)
{
int y=fa[x],z=fa[y];
if (y!=k)
{
if ((ch[z][0]==y)^(ch[y][0]==x)) rot(x,k);
else rot(y,k);
}
rot(x,k);
}
} int find(int x,int f)
{
if (f<=siz[L]) return find(L,f);
else if (f==(siz[L]+1)) return x;
else return find(R,f-siz[L]-1);
} int build(int l,int r,int lst)
{
int mid=l+r>>1;
v[mid]=a[mid]; fa[mid]=lst;
ch[mid][0]=l<mid?build(l,mid-1,mid):0;
ch[mid][1]=r>mid?build(mid+1,r,mid):0;
update(mid);
return mid;
} void debug()
{
printf("The Tree's root is %d\n",rt);
F(i,1,15) printf("Node %d : fa %d L %d R %d lx %d rx %d mx %d sum %d siz %d\n",i,fa[i],ch[i][0],ch[i][1],lx[i],rx[i],mx[i],sum[i],siz[i]);
} int fx,fy; int main()
{
Finout();
n=Getint();
F(i,1,n) a[i+1]=Getint();
rt=build(1,n+2,0); tot=n+2;
// debug();
q=Getint(); char opt[11];int x,y;
F(i,1,q)
{
scanf("%s",opt);
switch(opt[0])
{
case 'Q':
x=Getint();y=Getint();
// printf("%d %d\n",x,y);
fx=find(rt,x),fy=find(rt,y+2);
// printf("%d %d\n",fx,fy);
splay(fx,rt); splay(fy,ch[fx][1]);
printf("%d\n",mx[ch[ch[fx][1]][0]]);
update(fy); update(fx);
break;
case 'I':
++tot; x=Getint();y=Getint();
v[tot]=y;
fx=find(rt,x),fy=find(rt,x+1);
splay(fx,rt);splay(fy,ch[fx][1]);
fa[tot]=fy; ch[fy][0]=tot;
update(tot); update(fy); update(fx);
break;
case 'R':
x=Getint();y=Getint();
fx=find(rt,x+1); splay(fx,rt);
v[fx]=y;update(fx);
break;
case 'D':
x=Getint();
fx=find(rt,x); fy=find(rt,x+2);
splay(fx,rt); splay(fy,ch[fx][1]);
ch[fy][0]=0; update(fy); update(fx);
break;
}
// debug();
}
}

  

SPOJ GSS6 Can you answer these queries VI ——Splay的更多相关文章

  1. 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 ...

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

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

  3. 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取替 ...

  4. 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 ...

  5. SP4487 GSS6 - Can you answer these queries VI

    题目大意 给出一个由N个整数组成的序列A,你需要应用M个操作: I p x 在 p  处插入插入一个元素 x D p 删除 p 处的一个元素 R p x 修改 p 处元素的值为 x Q l r 查询一 ...

  6. 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 ...

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

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

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

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

  9. 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 ...

随机推荐

  1. Azure 项目构建 – 构建直播教学系统之媒体服务篇

    本课程主要介绍如何在 Azure 平台上快速构建和部署基于 Azure 媒体服务的点播和直播教学系统, 实践讲解如何使用 Azure 门户创建媒体服务, 配置视频流进行传输,连接 CDN 加速等. 具 ...

  2. 求矩阵的n次方 c语言实现

    矩阵的n次方,比较容易理解的想法是递归. 思路是这样的,把n分成两部分,当n是偶数的时候,即为左右两边的乘积,如果n是奇数,即为左右两边的乘积再乘a ) matrixn())^*a else matr ...

  3. MySQL 导出一句话

    听说是很老的东西了,学习的时候发现还是很好用的,故学习转载过来,留备学习. mysql 导出一句话 方法1:网上流行的方法 流程:(1)建表--->(2)插入数据--->(3)select ...

  4. hihoCoder #1151 : 骨牌覆盖问题·二 (矩阵快速幂,DP)

    题意:给一个3*n的矩阵,要求用1*2的骨牌来填满,有多少种方案? 思路: 官网题解用的仍然是矩阵快速幂的方式.复杂度O(logn*83). 这样做需要构造一个23*23的矩阵,这个矩阵自乘n-1次, ...

  5. Asp.Net Core 入门(十)—— 模型绑定和验证

    模型绑定时将Http请求中的数据映射到控制器操作方法上对应的参数,操作方法中的参数可以是简单类型,如整形,字符串等,也可以是复杂类型,如Product,Order等. Asp.Net Core MVC ...

  6. js 返回上一页并刷新页面

    js 方法 代码如下 self.location=document.referrer;

  7. C-基础:atoi

    C语言库函数名: atoi 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); 函数说明: 参数np ...

  8. github更换仓库

    1.找到.git目录   2.打开config文件 3.修改仓库地址 4.重新提交 git push --all origin 这样就替我们的项目换仓啦!!!^_^   分类: git 参考资料: h ...

  9. js的工厂模式

    工厂模式: 什么叫工厂模式,工厂就是大家大脑里想的那样,一个流水线作业的一个东西,只需要满足刚需就可以了,一个人,只干一件事,最后串起来,就是一个事件. 首先需要说一下工厂模式.工厂模式根据抽象程度的 ...

  10. C++系统学习之七:类

    类的基本思想是数据抽象和封装. 数据抽象是一种依赖于接口和实现分离的编程技术.类的接口包括用户所能执行的操作:类的实现包括类的数据成员.负责接口实现的函数体以及定义类所需的各种私有函数. 封装实现了类 ...