QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#943#570292#9311. Protection WarGalexLateRegistrationFailed.2024-10-08 16:25:042024-10-08 16:25:08

Details

Extra Test:

Invalid Input

input:

10 5
2 4 6 2 0 6 2 3 7 6
4
1 4 2
4
3
5 1 8

output:


result:

FAIL Integer parameter [name=a[i]] equals to 0, violates the range [1, 100000] (stdin, line 2)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570292#9311. Protection WarLateRegistration#AC ✓1046ms31928kbC++204.6kb2024-09-17 15:11:512024-09-17 15:11:53

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
const int B=1000;
int laz[300005],het[300005],zh[300005];
int bh[300005];
int bl[300005],br[300005];
void changed(int x,int k)
{
	zh[x]+=k;
	het[bh[x]]+=k;
}
int queryd(int x)
{
	return zh[x]+laz[bh[x]];
}
void change(int l,int r,int k)
{
	if(l>r)return;
	if(bh[l]==bh[r])
	{
		for(int i=l;i<=r;i++)zh[i]+=k,het[bh[i]]+=k;
		return;
	}
	for(int i=l;i<=br[bh[l]];i++)zh[i]+=k,het[bh[i]]+=k;
	for(int i=bh[l]+1;i<=bh[r]-1;i++)laz[i]+=k,het[i]+=k*(br[i]-bl[i]+1);
	for(int i=bl[bh[r]];i<=r;i++)zh[i]+=k,het[bh[i]]+=k;
}
int query(int l,int r)
{
	if(l>r)return 0;
	if(bh[l]==bh[r])
	{
		int ans=0;
		for(int i=l;i<=r;i++)ans+=zh[i]+laz[bh[i]];
		return ans;
	}
	int ans=0;
	for(int i=l;i<=br[bh[l]];i++)ans+=zh[i]+laz[bh[i]];
	for(int i=bh[l]+1;i<=bh[r]-1;i++)ans+=het[i];
	for(int i=bl[bh[r]];i<=r;i++)ans+=zh[i]+laz[bh[i]];
	return ans;
}
int dl[300005],dr[300005],dh[300005],dlaz[300005],cnt;
int nl[300005],nr[300005],nh[300005],nlaz[300005],ncnt;
int a[300005];
signed main()
{
	int n,q,opt,x,y,z;
	n=read();
	q=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
		bh[i]=(i-1)/B+1;
		if(bl[bh[i]]==0)bl[bh[i]]=i;
		br[bh[i]]=i;
	} 
	for(int i=1;i<=n;i++)changed(i,a[i]);
	cnt=1;
	dl[1]=1;
	dr[1]=n;
	dlaz[1]=0;
	for(int i=1;i<=n;i++)dh[1]+=a[i];
	int dflag=0;
	for(int cs=1;cs<=q;cs++)
	{
		dflag=0;
		opt=read();
		if(opt==1)
		{
			x=read();
			y=read();
			if(y!=0)
			{
				int sth=queryd(x);
				for(int i=1;i<=cnt;i++)if(dl[i]<=x&&dr[i]>=x)sth+=dlaz[i];
				opt=2;
				z=y-sth;
				y=x;
				dflag=1;
				//changed(x,y-sth);
				//for(int i=1;i<=cnt;i++)if(dl[i]<=x&&dr[i]>=x)dh[i]+=y-sth;
				//continue;
			}
			else
			{ 
			int sth=queryd(x);
			for(int i=1;i<=cnt;i++)if(dl[i]<=x&&dr[i]>=x)sth+=dlaz[i];
			changed(x,-sth);
			ncnt=0;
			for(int i=1;i<=cnt;i++)
			{
				if(dl[i]<=x&&dr[i]>=x)
				{
					change(dl[i],dr[i],dlaz[i]);
					if(dl[i]<x)
					{
						nl[++ncnt]=dl[i];
						nr[ncnt]=x-1;
						nlaz[ncnt]=0;
						nh[ncnt]=query(dl[i],x-1);
					}
					if(dr[i]>x)
					{
						nl[++ncnt]=x+1;
						nr[ncnt]=dr[i];
						nlaz[ncnt]=0;
						nh[ncnt]=query(x+1,dr[i]);
					}
				}
				else
				{
					nl[++ncnt]=dl[i];
					nr[ncnt]=dr[i];
					nh[ncnt]=dh[i];
					nlaz[ncnt]=dlaz[i];
				}
			}
			cnt=ncnt;
			for(int i=1;i<=cnt;i++)
			{
				dl[i]=nl[i];
				dr[i]=nr[i];
				dh[i]=nh[i];
				dlaz[i]=nlaz[i];
			}
			}
		}
		if(opt==2)
		{
			if(!dflag)
			{
			x=read();
			y=read();
			z=read();
		}
			int tl=x,tr=y;
			for(int i=1;i<=cnt;i++)
			{
				if(dl[i]<=x-1&&dr[i]>=x-1)tl=min(tl,dl[i]);
				if(dl[i]<=y+1&&dr[i]>=y+1)tr=max(tr,dr[i]);
			}
			change(x,y,z);
			ncnt=0;
			for(int i=1;i<=cnt;i++)
			{
				if(dr[i]<x-1||dl[i]>y+1)
				{
					nl[++ncnt]=dl[i];
					nr[ncnt]=dr[i];
					nh[ncnt]=dh[i];
					nlaz[ncnt]=dlaz[i];				
				}
				else
				{
					change(dl[i],dr[i],dlaz[i]);
				}
			}
			nl[++ncnt]=tl;
			nr[ncnt]=tr;
			nh[ncnt]=query(tl,tr);
			nlaz[ncnt]=0;
			cnt=ncnt;
			for(int i=1;i<=cnt;i++)
			{
				dl[i]=nl[i];
				dr[i]=nr[i];
				dh[i]=nh[i];
				dlaz[i]=nlaz[i];
			}
		}
		else if(opt==3)
		{
			for(int i=1;i<=cnt;i++)dlaz[i]++,dh[i]+=(dr[i]-dl[i]+1);
		}
		else if(opt==4)
		{
			for(int i=1;i<=cnt;i++)dlaz[i]+=dr[i]-dl[i]+1,dh[i]+=(dr[i]-dl[i]+1)*(dr[i]-dl[i]+1);
		} 
		else if(opt==5)
		{
			x=read();
			y=read();
			int ans=0;
			for(int i=1;i<=cnt;i++)
			{
				if(dl[i]>=x&&dr[i]<=y)ans+=dh[i]; 
				else if(dr[i]<x||dl[i]>y)
				{
					//do nothing
				}
				else
				{
					int tl=max(dl[i],x),tr=min(dr[i],y);
					//for(int j=tl;j<=tr;j++)printf("???%d %d\n",j,query(j,j)+dlaz[i]);
					ans+=query(tl,tr)+dlaz[i]*(tr-tl+1);
				}
			}
			printf("%lld\n",ans);
		}
		ncnt=0;
		for(int i=1;i<=cnt;i++)
		{
			if(dr[i]==n)
			{
				nl[++ncnt]=dl[i];
				nr[ncnt]=dr[i];
				nh[ncnt]=dh[i];
				nlaz[ncnt]=dlaz[i];
				continue;
			}
			int sth=queryd(dr[i]);
			changed(dr[i],-sth);
			if(dl[i]<dr[i])
			{
				nl[++ncnt]=dl[i];
				nr[ncnt]=dr[i]-1;
				nh[ncnt]=dh[i]-sth-dlaz[i];
				nlaz[ncnt]=dlaz[i];
			}
		}
		cnt=ncnt;
		//printf("orz%lld\n",cs);
		for(int i=1;i<=cnt;i++)
		{
			dl[i]=nl[i];
			dr[i]=nr[i];
			dh[i]=nh[i];
			dlaz[i]=nlaz[i];
			//printf("%lld %lld %lld %lld\n",dl[i],dr[i],dh[i],dlaz[i]);
		}
		//printf("!!!\n");
	}
}