SPOJ GSS3 Can you answer these queries III ——线段树
【题目分析】
GSS1的基础上增加修改操作。
同理线段树即可,多写一个函数就好了。
【代码】
#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 2000005
#define eps 1e-8
#define db double
#define ll long long
#define inf 0x3f3f3f3f
#define F(i,j,k) for (ll i=j;i<=k;++i)
#define D(i,j,k) for (ll i=j;i>=k;--i) void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("wa.txt","w",stdout);
#endif
} ll Getll()
{
ll 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;
} ll buf=100005,next[maxn],pos[maxn]; struct Node{
ll lx,rx,mx,sum;
Node operator + (Node x)
{
Node ret;
ret.lx=max(lx,sum+x.lx);
ret.rx=max(x.sum+rx,x.rx);
ret.sum=sum+x.sum;
ret.mx=max(max(mx,x.mx),max(rx+x.lx,max(ret.lx,ret.rx)));
return ret;
}
}t[maxn]; ll n,a[maxn],q,L,R,x,c; struct Problem{ll l,r,id,ans;}p[maxn]; bool cmp1(Problem x,Problem y)
{return x.l==y.l?x.r<y.r:x.l<y.l;}
bool cmp2(Problem x,Problem y)
{return x.id<y.id;} void Build(ll o,ll l,ll r)
{
if (l==r)
{
t[o].lx=t[o].rx=t[o].mx=t[o].sum=a[l];
return ;
}
ll mid=l+r>>1;
Build(o<<1,l,mid);
Build(o<<1|1,mid+1,r);
t[o]=t[o<<1]+t[o<<1|1];
} Node Query(ll o,ll l,ll r)
{
if (L<=l&&r<=R) return t[o];
ll mid=l+r>>1;
if (L>mid) return Query(o<<1|1,mid+1,r);
else if (R<=mid) return Query(o<<1,l,mid);
else return Query(o<<1,l,mid)+Query(o<<1|1,mid+1,r);
} void Modify(ll o,ll l,ll r)
{
if (l==r)
{
t[o].lx=t[o].rx=t[o].mx=t[o].sum=c;
return ;
}
ll mid=l+r>>1;
if (x<=mid) Modify(o<<1,l,mid);
else Modify(o<<1|1,mid+1,r);
t[o]=t[o<<1]+t[o<<1|1]; } int main()
{
Finout(); n=Getll();
// printf("%lld\n",n);
F(i,1,n) a[i]=Getll();
// F(i,1,n) printf("%lld ",a[i]);
// printf("\n");
Build(1,1,n); q=Getll();
F(i,1,q)
{
ll opt=Getll();
switch (opt)
{
case 1:
L=Getll();
R=Getll();
printf("%lld\n",Query(1,1,n).mx);
break;
case 0:
x=Getll();
c=Getll();
Modify(1,1,n);
break;
}
}
}
SPOJ GSS3 Can you answer these queries III ——线段树的更多相关文章
- 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 1557 GSS3 - Can you answer these queries III 线段树
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...
- SP1716 GSS3 - Can you answer these queries III 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- 数据结构(线段树):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 bet ...
- 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 ...
- bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 145 ...
- SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)
Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...
- GSS5 spoj 2916. Can you answer these queries V 线段树
gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
随机推荐
- 覆盖alert对话框-自制Jquery.alert插件
Javascript 代码: (function ($) { 'use strict'; window.alert = $.alert = function (msg) { var defaultOp ...
- C语言二维数组作为函数参数
设有整型二维数组a[3][4]如下:0 1 2 34 5 6 78 9 10 11 它的定义为: int a[3][4]={{0,1,2,3},{4,5,6,7} ...
- afnetworking NSCocoaErrorDomain Code=3840 解决
afnetworking json解析出错 解决方法1 AFURLResponseSerialization.m 258行修改 responseString = [responseString str ...
- pathForResource获取资源为nil的原因
利用NSbundle获取 资源文件的时候,如果是自己添加的文件,获取的时候纵使返回nil的解决办法.原因是因为该文件没有添加到资源文件中,只要在添加文件的时候选择添加到 Create Folder R ...
- 背包问题2 (lintcode)
这里: for(int j = 1;j <= m;j++) result[0][j] = 0x80000000; 不能从0开始,result[0][0]是可以取到的,是0.其他情况取不到才用最小 ...
- Java获取2个日期里面的所有月份
public static void main(String[] args) { String t1="2018-08-01"; t1 = t1.replaceAll(" ...
- Bootstrap历练实例:基本按钮组
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- shell脚本,实现奇数行等于偶数行。
请把如下字符串stu494e222fstu495bedf3stu49692236stu49749b91转为如下形式:stu494=e222fstu495=bedf3stu496=92236stu497 ...
- x220 OS X 10.10.4安装
变色龙安装过程: 1.使用磁盘助手将按照盘写入独立的磁盘分区(AF格式,就是Apple的HPS格式): 2.安装启动时,用-v -f -x参数,分别为显示信息.重新build系统驱动.安全模式: 3. ...
- 组合的输出(DFS)
题目描述: 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且r<=n),我们可以简单地将n个元素理解为自然数1,2,…,n,从中任取r个数. 现要求你用递归的方法输出 ...