蒟蒻刚学OI,求助线段树
查看原帖
蒟蒻刚学OI,求助线段树
173840
小恐楼主2020/5/25 09:22

RT,除了第1个点其他全WA

#include<stdio.h>
#include<algorithm>
using namespace std;
const int NR=5e5+5;
const int MR=1e5+5;
const int INF=0x3f3f3f3f;
typedef long long LL;
int read()
{
	int x=0;
	int bei=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
			bei=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*=bei;
}
int a[NR];
struct Segtree
{
	int sum,qian,hou,zong;
}segtree[NR<<2];
void build(int x,int l,int r)
{
	if(l==r)
	{
		segtree[x]=(Segtree){a[l],a[l],a[l],a[l]};
		return;
	}
	int ls=x<<1,rs=x<<1|1;
	int mid=l+r>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	segtree[x].sum=segtree[ls].sum+segtree[rs].sum;
	segtree[x].zong=max(segtree[ls].hou+segtree[rs].qian,max(segtree[ls].zong,segtree[rs].zong));
	segtree[x].qian=max(segtree[ls].sum+segtree[rs].qian,segtree[ls].qian);
	segtree[x].hou=max(segtree[rs].sum+segtree[ls].hou,segtree[rs].hou);
}
int lst;
int ans;
void query(int x,int l,int r,int nl,int nr)
{
	if(nl<=l&&r<=nr)
	{
		ans=max(ans,max(lst+segtree[x].qian,segtree[x].zong));
		lst=segtree[x].hou;
		return;
	}
	int mid=l+r>>1;
	int ls=x<<1,rs=x<<1|1;
	if(nl<=mid)
		query(ls,l,mid,nl,nr);
	if(nr>mid)
		query(rs,mid+1,r,nl,nr);
}
void modify(int x,int l,int r,int nx,int k)
{
	if(l==r)
	{
		segtree[x]=(Segtree){k,k,k,k};
		return;
	}
	int ls=x<<1,rs=x<<1|1;
	int mid=l+r>>1;
	if(nx<=mid)
		modify(ls,l,mid,nx,k);
	else
		modify(rs,mid+1,r,nx,k);
	segtree[x].sum=segtree[ls].sum+segtree[rs].sum;
	segtree[x].zong=max(segtree[ls].hou+segtree[rs].qian,max(segtree[ls].zong,segtree[rs].zong));
	segtree[x].qian=max(segtree[ls].sum+segtree[rs].qian,segtree[ls].qian);
	segtree[x].hou=max(segtree[rs].sum+segtree[ls].hou,segtree[rs].hou);
}
int main()
{
	//freopen("P4513.in","r",stdin);
	//freopen("P4513.out","w",stdout);
	int n=read(),m=read();
	for(int i=1;i<=n;++i)
		a[i]=read();
	build(1,1,n);
	while(m--)
	{
		int p=read();
		if(p==1)
		{
			int l=read(),r=read();
			lst=ans=-INF;
			query(1,1,n,min(l,r),max(l,r));
			printf("%d\n",ans);
		}
		else
		{
			int x=read(),s=read();
			modify(1,1,n,x,s);
		}
	}
	return 0;
}
2020/5/25 09:22
加载中...