数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
GSS3 - Can you answer these queries III
You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations:
modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
Input
The first line of input contains an integer N. The following line contains N integers, representing the sequence A1..AN.
The third line contains an integer M. The next M lines contain the operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
Output
For each query, print an integer as the problem required.
Example
Input:
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3 Output:
6
4
-3
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
const int INF=;
int n,Q,a[maxn]; struct Node{
int lx,rx,mx,sum;
Node(int _lx=-INF,int _rx=-INF,int _mx=-INF){
lx=_lx;rx=_rx;mx=_mx;
}
}T[maxn<<]; void Push_up(Node &x,Node l,Node r){
x.sum=l.sum+r.sum;
x.lx=max(l.lx,l.sum+r.lx);
x.rx=max(r.rx,r.sum+l.rx);
x.mx=max(max(l.mx,r.mx),l.rx+r.lx);
} void Build(int x,int l,int r){
if(l==r){
T[x].lx=T[x].rx=max(a[l],);
T[x].mx=T[x].sum=a[l];
return;
}
int mid=(l+r)>>;
Build(x<<,l,mid);
Build(x<<|,mid+,r);
Push_up(T[x],T[x<<],T[x<<|]);
} void Modify(int x,int l,int r,int g){
if(l==r){
T[x].lx=T[x].rx=max(a[l],);
T[x].mx=T[x].sum=a[l];
return;
}
int mid=(l+r)>>;
if(mid>=g)Modify(x<<,l,mid,g);
else Modify(x<<|,mid+,r,g);
Push_up(T[x],T[x<<],T[x<<|]);
} Node Query(int x,int l,int r,int a,int b){
if(l>=a&&r<=b)
return T[x];
int mid=(l+r)>>;
Node L,R,ret;
if(mid>=a)L=Query(x<<,l,mid,a,b);
if(mid<b)R=Query(x<<|,mid+,r,a,b);
Push_up(ret,L,R);
return ret;
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
Build(,,n);
scanf("%d",&Q);
int tp,x,y;
while(Q--){
scanf("%d%d%d",&tp,&x,&y);
if(!tp)
a[x]=y,Modify(,,n,x);
else
printf("%d\n",Query(,,n,x,y).mx);
}
return ;
}
水题。
数据结构(线段树):SPOJ GSS3 - Can you answer these queries III的更多相关文章
- 线段树 SP1716 GSS3 - Can you answer these queries III
SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...
- 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 ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- SPOJ GSS3 Can you answer these queries III
Time Limit: 330MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...
- SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树
GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...
- 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国
SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...
- 线段树 SP1043 GSS1 - Can you answer these queries I
SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...
- GSS3 - Can you answer these queries III
题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 感谢 @Edgration 提供的 ...
随机推荐
- PHP编译安装出错configure: error: mcrypt.h not found. Please reinstall libmcrypt的解决办法
1.下载libmcrypt wget http://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.ta ...
- oracle的安装与plsql的环境配置
1,首先得有oracle的安装包和plsql的安装包,安装包地址可见百度云 http://pan.baidu.com/s/1miTqhmg 2.解压下来进入0817账套,找到set.exe文件,双击安 ...
- oo面向对象原则
1.单一职责原则 一个类,最好只做一件事,只有一个引起他变化的原因否则就应该考虑重构. 2.开放封闭原则 软件实体应该是可扩展的,而不是可修改的.也就是说对扩展开放,对修改封闭.主要体现在两个方面: ...
- Topas命令详解
Topas命令详解 执行topas命令后如图所示: #topas 操作系统的最全面动态,而又查看方便的性能视图就是topas命令了,下面以topas输出为例,对AIX系统的性能监控做简要描述,供运维工 ...
- iOS远程消息推送自我整理版
@interface AppDelegate () <UIApplicationDelegate> @end @implementation AppDelegate - (BOOL)app ...
- 谈一下关于C++函数包装问题
在C++中,我们经常遇到在某个特定的时刻,需要将函数进行包装调用,尤其是当我们需要将不同签名的函数放到同一个集合时,由于函数签名不一致导致我们不能直接将各式各样的函数指针放到诸如list这样的集合中, ...
- 改进《完美让IE兼容input placeholder属性的jquery实现》的不完美
<完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...
- PHPCMS(2)PHPCMS V9 环境搭建(转)
转自:http://www.cnblogs.com/Braveliu/p/5072920.html PHPCMS V9的学习总结分为以下几点: [1]PHPCMS 简介 PHP原始为Personal ...
- 一个IP支持多个网站实例(Apache2、Ubuntu相关)
http://www.blogjava.net/Andyluo/archive/2009/08/24/21821.html http://blog.csdn.net/zltianhen/article ...
- 9张思维导图学习Javascript
分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...