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;
		}
	}
}

  

时间: 2024-12-06 17:21:25

SPOJ GSS3 Can you answer these queries III ——线段树的相关文章

SPOJ GSS3 Can you answer these queries III (线段树)

题目大意: 求区间最大子区间的和. 思路分析: 记录左最大,右最大,区间最大. 注意Q_L  和 Q_R  就好. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e #define maxn 55555 using na

SP1716 GSS3 - Can you answer these queries III 线段树

题目传送门:SP1043 GSS1 - Can you answer these queries I 更好的阅读体验 动态维护子段和最大值 前置知识 静态维护子段和最大值:SP1043 GSS1 - Can you answer these queries I 题解传送 题解: 提供结构体指针线段树写法: 设\(l\)为区间左端点, \(r\)为区间右端点: \(ls\)为以\(l\)为左端点的最大子段和, \(rs\)为以\(r\)为右端点的最大子段和; \(sum\)为区间和, \(val\

Spoj 1716 Can you answer these queries III 线段树 单点修改 区间求最大子段和

题目链接:点击打开链接 == 原来写1的时候已经把更新函数写好了.. #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <vector> #include <map> using namespace std; #define N 50050 #define Lso

SPOJ GSS4 Can you answer these queries IV (线段树)

题目大意: 给出N个数 0     操作   把 l -----  r之间的数全部开平方 1     操作  输出 l -----r  之间的和 思路分析: 判断区间里的数字是否全相同.如果相同, 将cov 置为该数 查询的时候和更新的时候,如果碰到cov != -1 的  就直接返回就可以了 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #incl

数据结构(线段树):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{

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  Solved: 76[Submit][Status][Discuss] Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和(可选空子段). 这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次. 比如:1,2,3,2,2,2出现了3次,但只算一次,

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/problems/GSS2/ Description Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse

SPOJ GSS3 Can you answer these queries III

Time Limit: 330MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description 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

Spoj 2916 Can you answer these queries V 线段树 求任意重叠区间的最大子段和

题目链接:点击打开链接 题意: T个测试数据 n个数字 q个询问 每个询问 : [x1, y1] [x2, y2] 问: int ans = -inf; for(int i = x1; i <= y1; i++) for(int j = max(x2, i); j <= y2; j++) ans = max(ans, query(i, j)); 思路: query_L(int l, int r) 求的是在区间[l,r]内 ,左端点为l的最大子段和. 其他和GSS3差不多. 分类讨论一下给定的区