QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#340649#4397. DOS Cardcrsfaa#AC ✓129ms33280kbC++141.7kb2024-02-29 10:59:122024-02-29 10:59:14

Judging History

This is the latest submission verdict.

  • [2024-02-29 10:59:14]
  • Judged
  • Verdict: AC
  • Time: 129ms
  • Memory: 33280kb
  • [2024-02-29 10:59:12]
  • Submitted

answer

#include<bits/stdc++.h>
#define Yukinoshita namespace
#define Yukino std
#define int long long
using Yukinoshita Yukino;
int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') w=ch=='-'?-1:1,ch=getchar();
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return s*w;
}
const int mxn=2e5+5;
/*
ABAB
AABB
*/
struct node
{
	int A,B,AA,BB,AAB,ABB,ABA,BAB,AB,BA,ans;
	friend node operator +(const node &x,const node &y)
	{
		node res;
		res.A=max(x.A,y.A);
		res.B=max(x.B,y.B);
		res.AA=max({x.AA,y.AA,x.A+y.A});
		res.BB=max({x.BB,y.BB,x.B+y.B});
		res.AAB=max({x.AAB,y.AAB,x.AA+y.B,x.A+y.AB});
		res.ABB=max({x.ABB,y.ABB,x.AB+y.B,x.A+y.BB});
		res.ABA=max({x.ABA,y.ABA,x.AB+y.A,x.A+y.BA});
		res.BAB=max({x.BAB,y.BAB,x.BA+y.B,x.B+y.AB});
		res.AB=max({x.AB,y.AB,x.A+y.B});
		res.BA=max({x.BA,y.BA,x.B+y.A});
		res.ans=max({x.ans,y.ans,x.A+y.BAB,x.AB+y.AB,x.ABA+y.B,x.A+y.ABB,x.AA+y.BB,x.AAB+y.B});
		return res;
	}
	void add(int a,int b)
	{
		A=a,B=b;
		AA=BB=AAB=ABB=ABA=BAB=AB=BA=ans=-1e17;
	}
};
struct seg
{
	int l,r;
	node s;
}t[mxn*4];
#define ls w<<1
#define rs ls|1
int a[mxn];
void build(int w,int l,int r)
{
	t[w].l=l,t[w].r=r;
	if(l<r)
	{
		int mid=l+r>>1;
		build(ls,l,mid);
		build(rs,mid+1,r);
		t[w].s=t[ls].s+t[rs].s;
	}
	else t[w].s.add(a[l]*a[l],-a[l]*a[l]);
}
node ask(int w,int l,int r)
{
	if(t[w].l>=l&&t[w].r<=r) return t[w].s;
	if(t[ls].r>=l&&t[rs].l<=r) return ask(ls,l,r)+ask(rs,l,r);
	return ask(t[ls].r>=l?ls:rs,l,r);
}
signed main()
{
	int T=read();
	while(T--)
	{
		int n=read(),m=read(),i,j;
		for(i=1;i<=n;i++)
			a[i]=read();
		build(1,1,n);
		while(m--)
		{
			int l=read(),r=read();
			printf("%lld\n",ask(1,l,r).ans);
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 129ms
memory: 33280kb

input:

100
96154 95730
90210724 22635940 55815661 83807625 19279659 73772905 76214297 19124836 44176768 61118775 90180769 78603454 23786707 63922615 30379117 541896 67837670 15861700 18129979 15378730 99790737 18747118 79018780 14023804 10636607 27422459 75194869 52362958 38176367 17048673 77142527 8688873...

output:

19996866975031454
19954573944633996
19999245288760024
19991774536026976
19998516034562673
19889495723295968
19987300645542796
19999515774953477
19999691378636568
19999691135662443
19966234306637958
19999691378636568
19994914188770357
19999244057031833
19999691393398008
19999691378636568
199996913933...

result:

ok 198115 lines