题目大意

给出一个由N个整数组成的序列A,你需要应用M个操作:

  • I p x 在 p  处插入插入一个元素 x
  • D p 删除 p 处的一个元素
  • R p x 修改 p 处元素的值为 x
  • Q l r 查询一个区间[l,r]的最大子段和

输入格式

第一行一个数N,表示序列的长度

第二行N个数,表示初始序列A

第三行一个数M,表示操作的次数

接下来的M行,每行一个操作,格式见题目描述

输出格式

输出若干行,每行一个整数,表示查询区间的最大子段和

感谢@Anoxiacxy 提供的翻译

题目描述

Given a sequence A of N (N <= 100000) integers, you have to apply Q (Q <= 100000) operations:

Insert, delete, replace an element, find the maximum contiguous(non empty) sum in a given interval.

输入输出格式

输入格式:

The first line of the input contains an integer N.
The following line contains N integers, representing the starting
sequence A1..AN, (|Ai| <= 10000).

The third line contains an integer Q. The next Q lines contains the operations in following form:

I x y: insert element y at position x (between x - 1 and x).
D x : delete the element at position x.
R x y: replace element at position x with y.
Q x y: print max{Ai + Ai+1 + .. + Aj | x <= i <= j <= y}.

All given positions are valid, and given values are between -10000 and +10000.

The sequence will never be empty.

输出格式:

For each "Q" operation, print an integer(one per line) as described above.

输入输出样例

输入样例#1:

5
3 -4 3 -1 6
10
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
输出样例#1:

8
3
6
3
5

Solution:

  某天下午和机房巨佬们比赛做这题谁最快AC,巨恶心。

  题意毫无思维难度,写一个平衡树就好了,关键是信息的维护情况贼多,在更新子树信息时要把所有的情况都考虑到,然后就比较码农了。

代码:

/*Code by 520 -- 10.18*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int N=;
int n,m,root,ch[N][],rnd[N],date[N],cnt,siz[N];
int lf[N],rf[N],maxn[N],sum[N]; int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-') x=getchar();
if(x=='-') x=getchar(),f=;
while(x>=''&&x<='') a=(a<<)+(a<<)+(x^),x=getchar();
return f?-a:a;
} il int newnode(int v){
++cnt;
siz[cnt]=,maxn[cnt]=sum[cnt]=date[cnt]=v,rnd[cnt]=rand(),lf[cnt]=rf[cnt]=max(date[cnt],);
return cnt;
} il void up(int rt){
siz[rt]=siz[ch[rt][]]+siz[ch[rt][]]+;
sum[rt]=date[rt]+sum[ch[rt][]]+sum[ch[rt][]];
if(ch[rt][]&&ch[rt][]){
lf[rt]=max(lf[ch[rt][]],date[rt]+sum[ch[rt][]]+lf[ch[rt][]]);
rf[rt]=max(rf[ch[rt][]],date[rt]+sum[ch[rt][]]+rf[ch[rt][]]);
maxn[rt]=max(lf[ch[rt][]]+date[rt]+rf[ch[rt][]],max(maxn[ch[rt][]],maxn[ch[rt][]]));
}
else if(ch[rt][]) lf[rt]=max(max(,lf[ch[rt][]]),sum[ch[rt][]]+date[rt]),rf[rt]=max(,date[rt]+rf[ch[rt][]]),maxn[rt]=max(maxn[ch[rt][]],rf[ch[rt][]]+date[rt]);
else if(ch[rt][]) rf[rt]=max(max(,rf[ch[rt][]]),date[rt]+sum[ch[rt][]]),lf[rt]=max(,date[rt]+lf[ch[rt][]]),maxn[rt]=max(maxn[ch[rt][]],date[rt]+lf[ch[rt][]]);
else maxn[rt]=date[rt],lf[rt]=rf[rt]=max(date[rt],); } int merge(int x,int y){
if(!x||!y) return x+y;
if(rnd[x]<rnd[y]) {ch[x][]=merge(ch[x][],y),up(x);return x;}
else {ch[y][]=merge(x,ch[y][]),up(y);return y;}
} void split(int rt,int v,int &x,int &y){
if(!rt) {x=y=;return;}
if(siz[ch[rt][]]>=v) y=rt,split(ch[rt][],v,x,ch[y][]),up(y);
else x=rt,split(ch[rt][],v-siz[ch[rt][]]-,ch[x][],y),up(x);
} il void ins(int k,int v){
int x,y; split(root,k-,x,y);
root=merge(merge(x,newnode(v)),y);
} il void del(int k){
int x,y,z; split(root,k-,x,y),split(y,,y,z);
root=merge(x,z);
} il void change(int k,int v){
int x,y,z; split(root,k-,x,y),split(y,,y,z);
root=merge(merge(x,newnode(v)),z);
} il int query(int l,int r){
int x,y,z,ans; split(root,r,x,y),split(x,l-,x,z);
ans=maxn[z];
root=merge(merge(x,z),y);
return ans;
} int main(){
n=gi(); char opt[];int x,y;
For(i,,n) ins(i,gi());
m=gi();
while(m--){
scanf("%s",opt),x=gi();
if(opt[]=='I') y=gi(),ins(x,y);
else if(opt[]=='D') del(x);
else if(opt[]=='R') y=gi(),change(x,y);
else y=gi(),printf("%d\n",query(x,y));
}
return ;
}
 
 
 

SP4487 GSS6 - Can you answer these queries VI的更多相关文章

  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 GSS6 Can you answer these queries VI ——Splay

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

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

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

  6. kuangbin专题七 HDU4027 Can you answer these queries? (线段树)

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

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

  8. hdu 4027 Can you answer these queries?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...

  9. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

随机推荐

  1. day80

    昨日回顾 上节回顾: 中间件: -django请求生命周期: -中间件:对全局请求的修改,和全局响应的修改 -process_request:从上往下执行 -process_response:从下往上 ...

  2. 警惕ASP.NET MVC中的ValidateInputAttribute

    最近在做一个ASP.NET MVC项目的时候发现,有一个Controller的Action死活都没法接收到从客户端提交过来的Html表单请求和数据,后来才发现是因为默认情况下ASP.NET MVC在执 ...

  3. IDEA创建Scala项目

    一.安装插件 见Scala入门篇 二.新建项目 选择new project,其中SBT相当于精简版的maven,其他的待补充.这里选择IDEA 填写信息,选择Scala SDK 在src目录下新建Sc ...

  4. 20155204《网络对抗》Exp7 网络欺诈防范

    20155204<网络对抗>Exp7 网络欺诈防范 一.基础问题回答 1.通常在什么场景下容易受到DNS spoof攻击 在不安全的网络环境下访问网站. 2.在日常生活工作中如何防范以上两 ...

  5. POJ 1328&&2109&&2586

    这次是贪心(水笔贪心)专题. 先看1328,一道经典的导弹拦截(或者是打击?不懂背景). 大意是说在一个坐标系中有一些点(或是导弹),你要在x轴上建一些东西,它们可以拦截半径为d的圆范围中的点.问最少 ...

  6. PostgreSQL索引页

    磨砺技术珠矶,践行数据之道,追求卓越价值   [作者 高健@博客园  luckyjackgao@gmail.com] 本页目的,是起到索引其他所有本人所写文档的作用: 分类一:PostgreSQL基础 ...

  7. 【Java框架型项目从入门到装逼】第十一节 用户新增之把数据传递到后台

    让我们继续来做"主线任务",这一节,我们来做具体的用户新增功能.首先,为了简单起见,我把主页面改了一些,改的是列表那一块.删去了一些字段,和数据库表对应一致: 现在,我们要实现一个 ...

  8. Salesforce Apex学习 : 利用Schema命名空间中的DescribeSObjectResult类型来获取sObject对象的基本信息

    DescribeSObjectResult 对象的取得: //使用getDescribe方法和sObject token Schema.DescribeSObjectResult mySObjDesc ...

  9. 矩阵分解----Cholesky分解

    矩阵分解是将矩阵拆解成多个矩阵的乘积,常见的分解方法有 三角分解法.QR分解法.奇异值分解法.三角分解法是将原方阵分解成一个上三角矩阵和一个下三角矩阵,这种分解方法叫做LU分解法.进一步,如果待分解的 ...

  10. 科普贴 | 数字钱包MetaMask安装使用详解,活用MetaMask轻松驾驭以太坊

    MetaMask 是一款浏览器插件钱包,不需下载安装客户端,只需添加至浏览器扩展程序即可使用,非常方便.它是很多支持 ETH 参投的 ICO 项目推荐使用的钱包之一. 2018年初最火的一个币,应该就 ...