数据结构(线段树):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 提供的 ...
随机推荐
- HDFS的Java客户端操作代码(查看HDFS下所有的文件存储位置信息)
1.查看HDFS下所有的文件存储位置信息 package Hdfs; import java.net.URI; import org.apache.hadoop.conf.Configuration; ...
- foreach遍历----for(object o: list)
备注,两种写法是一样的.
- 数据的动态合并和导出至EXCEL
最近一段时间都在处理数据的动态合并和导出EXCEL的问题,写个demo记录下,希望和我碰到同样问题的博友可以顺利解决:后面会提供demo下载链接. (VS2012,ASP.NET) 一.主要解决以下问 ...
- 分享整理的sql脚本
1. 表空间使用率 SQL> select a.tablespace_name, 2 round(a.total_size) "total_size M" ...
- iOS网络通信类库
iOS网络通信类库 iOS网络通信类库:ASIHTTPRequest,AFNetworking,MKNetWorkKIt. ASIHTTPRequest在ios5.0之后就不在维护了,所以之后主要就是 ...
- What and where are the stack and heap?
The stack is the memory set aside as scratch space for a thread of execution. When a function is cal ...
- objective-c相关知识点
1,objective-c中实现线程同步: Mutexlock (互斥锁).NSCondition lock (条件锁)消息传送 2,UDP和TCP: TCP :传输控制协议,可以提供面向连接的.可靠 ...
- angular.js 简单的表达式
<!doctype html> <html> <head> <meta charset ="utf-8"> <script s ...
- 英语学习[ZZ]
本文作者三年间从四级勉强及格到高级口译笔试210,口试232.找工作面试时给其口试的老外考官听了一分钟就说你的英语不用考了.虽不敢说方法一定是最好的,但从现在开始随便谁不要再去找学习资料,每天花两个钟 ...
- iOS 视频播放横屏,隐藏状态栏
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] init]; ...