SPOJ GSS3 Can you answer these queries III
2016-05-12 23:47
381 查看
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 }.
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 }.
参见http://blog.csdn.net/sdfzyhx/article/details/51388593,SPOJ GSS1。
增加修改而已。
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.参见http://blog.csdn.net/sdfzyhx/article/details/51388593,SPOJ GSS1。
增加修改而已。
#include<cstdio> struct node { int lch,rch,l,r,lmax,rmax,max,sum; }t[200010]; struct ans { int l,r,max,s; }; int n,nn,a[50010]; int mx(int x,int y) { return x>y?x:y; } void build(int p,int l,int r) { t[p].l=l; t[p].r=r; if (l==r) { t[p].lmax=t[p].rmax=t[p].max=t[p].sum=a[l]; return; } int mid=(t[p].l+t[p].r)/2; t[p].lch=++nn; build(nn,l,mid); t[p].rch=++nn; build(nn,mid+1,r); t[p].sum=t[t[p].lch].sum+t[t[p].rch].sum; t[p].lmax=mx(t[t[p].lch].lmax,t[t[p].lch].sum+t[t[p].rch].lmax); t[p].rmax=mx(t[t[p].rch].rmax,t[t[p].rch].sum+t[t[p].lch].rmax); t[p].max=mx(mx(t[t[p].lch].max,t[t[p].rch].max),t[t[p].lch].rmax+t[t[p].rch].lmax); } ans find(int p,int l,int r) { ans al,ar,a; if (t[p].l==l&&t[p].r==r) { a.l=t[p].lmax; a.r=t[p].rmax; a.max=t[p].max; a.s=t[p].sum; return a; } int mid=(t[p].l+t[p].r)/2; if (r<=mid) return find(t[p].lch,l,r); if (l>=mid+1) return find(t[p].rch,l,r); al=find(t[p].lch,l,mid); ar=find(t[p].rch,mid+1,r); a.l=mx(al.l,al.s+ar.l); a.r=mx(ar.r,ar.s+al.r); a.s=al.s+ar.s; a.max=mx(mx(al.max,ar.max),al.r+ar.l); return a; } void modi(int p,int q,int x) { if (t[p].l==t[p].r) { t[p].max=t[p].lmax=t[p].rmax=t[p].sum=x; return; } int mid=(t[p].l+t[p].r)/2; if (q<=mid) modi(t[p].lch,q,x); else modi(t[p].rch,q,x); t[p].sum=t[t[p].lch].sum+t[t[p].rch].sum; t[p].lmax=mx(t[t[p].lch].lmax,t[t[p].lch].sum+t[t[p].rch].lmax); t[p].rmax=mx(t[t[p].rch].rmax,t[t[p].rch].sum+t[t[p].lch].rmax); t[p].max=mx(mx(t[t[p].lch].max,t[t[p].rch].max),t[t[p].lch].rmax+t[t[p].rch].lmax); } int main() { int i,j,k,m,p,q,x,y,z; bool b; scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&a[i]); nn=1; build(1,1,n); scanf("%d",&m); for (i=1;i<=m;i++) { scanf("%d%d%d",&b,&x,&y); if (b) printf("%d\n",find(1,x,y).max); else modi(1,x,y); } }
相关文章推荐
- SPOJ GSS1 Can you answer these queries I
- Codeforces 350 Div2 E Correct Bracket Sequence Editor(list模拟)
- iOS获取设备唯一的UUID(解决删除应用UUID变更的问题)
- UIImageView和UIImage
- HDU3397 Sequence operation(线段树的区间合并)
- SDWebImage与UIScrollView或UICollectionView一起使用多次收到“Received memory warning.”,最终程序闪退的原因分析和解决办法。
- forward(request,reponse)与sendRedirect
- 可修改UID的白卡,请问为什么一般读写器不能修改这种卡的UID,必须要特定的读写器才能修改?
- iOS开发封装带有Button的UIView控件,使用target-action模式给button添加点击事件
- 129.Longest Consecutive Sequence
- iOS开发封装带有Button的UIView控件,使用代理给button添加点击事件
- deep learning---利用caffe在vgg-face上finetuing自己的人脸数据
- mui图片预览
- weblogic 下异常 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken
- 62. Unique Paths
- leetcode_347 Top K Frequent Elements
- Guid的使用
- iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
- String StringBuffer与StringBuilder的区别
- Java 设计模式-建造者模式(Builder)Android讲解