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& ...
随机推荐
- ScrollView中嵌套ListView时,listview高度显示的问题
方法一:直接更改listview的控件高度,动态获取(根据条目和每个条目的高度获取) 前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个Lis ...
- 连接MongoDB数据库的配置说明
- 如何使用Git Bash Here,将本地项目传到github上
申请一个github账号 安装git bash git与git bash的区别: git:版本控制工具,支持该工具的网站有Github.BitBucket.Gitorious.国内的osChina仓库 ...
- Perl sendmail
introduction of sendmail example send mail to multi-receiver
- codevs 2728 整数帝国问题(水题日常)
时间限制: 1 s 空间限制: 16000 KB 题目等级 : 白银 Silver 题目描述 Description 在很久以前,在遥远的东方,有一个整数帝国,它里面里居住着大量的正整数,了缓解都 ...
- 如何在Kubernetes里创建一个Nginx service
Jerry之前的文章如何在Kubernetes里创建一个Nginx应用,已经使用kubectl命令行创建了Pod,但是在kubernetes中,Pod的IP地址会随着Pod的重启而变化,因此用Pod的 ...
- jQuery中ready方法的实现
https://blog.csdn.net/major_zhang/article/details/80146674 先普及一下jquery.ready()和window.onload,window. ...
- Python 类变量,成员变量,静态变量,局部变量
局部 class TestClass(object): val1 = 100 def __init__(self): self.val2 = 200 def fcn(self,val = 400): ...
- kmp 模板
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> ...
- docker:安装mysql
文章来源:https://www.cnblogs.com/hello-tl/p/9234429.html 1.添加镜像 docker pull mysql 2.在/data下新建文件夹mysql,进入 ...