跳到正文

剪贴板

剪贴板 owri8kpo

原文
剪贴板信息

剪贴板信息

收录
更新

P5608

不考虑修改。

变量名含义左段右段总段
(仅为示例,不在代码中维护。)算式1×21\times23+43+41×2×3+41\times2\times3+4
len区间长度。可以通过 r−l+1r-l+1 计算出来,辅助判断极长连乘段。222244
rop这一段右侧的运算符的对应代号。00 为 ++,11 为 ×\times。11不必考虑,无论存储为多少都没问题。同左。
llen左端极长连乘段的长度221133
rlen右端极长连乘段的长度221111
sum最终答案22771010
lsum左端极长连乘段结果223366
rsum右端极长连乘段结果224444

Record

C++
// Problem: P5608 [Ynoi2013] 文化课
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5608
// Memory Limit: 64 MB
// Time Limit: 1500 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<cstdio>
#include<algorithm>
using namespace std;
//#define DEBUG
const int MAXN=100000;
const long long MOD=1e9+7;
struct Tree
{
	int l,r;//左端点与右端点
	int len;//长度
	long long sum,addsum,mulsum;//总和、和、积
	long long lsum,rsum;//左边和右边的极长连乘段
	int llen,rlen;//左边和右边的极长连乘段的长度
	int rop;
}tree[4*MAXN+5];
long long a[MAXN+5];
int b[MAXN+5];
int n,m;
Tree update(Tree a,Tree b)
{
	Tree res;
	res.l=a.l;
	res.r=b.r;
	res.len=a.len+b.len;
	res.addsum=(a.addsum+b.addsum)%MOD;
	res.mulsum=(a.mulsum*b.mulsum)%MOD;
	res.rop=b.rop;
	if(a.rop)
	{
		res.sum=(((((((a.sum+b.sum)%MOD-a.rsum))%MOD)-b.lsum)%MOD)+a.rsum*b.lsum)%MOD;
	}
	else
	{
		res.sum=(a.sum+b.sum)%MOD;
	}
	if(a.rop&&(a.llen==a.len))//中间为乘
	{
		res.lsum=(a.sum*b.lsum)%MOD;
		res.llen=a.len+b.llen;
	}
	else
	{
		res.lsum=a.lsum;
		res.llen=a.llen;
	}
	if(a.rop&&(b.rlen==b.len))
	{
		res.rsum=(b.sum*a.rsum)%MOD;
		res.rlen=b.len+a.rlen;
	}
	else
	{
		res.rsum=b.rsum;
		res.rlen=b.rlen;
	}
	#ifdef DEBUG
	if(res.r<=10)
	{
		printf("Update %d %d sum %lld lsum %lld rsum %lld middle rop %d len %d llen %d rlen %d\n",res.l,res.r,res.sum,res.lsum,res.rsum,b.rop,res.len,res.llen,res.rlen);
	}
	#endif
	return res;
}
void build(int o,int l,int r)
{
	#ifdef DEBUG
	printf("Build %d %d %d\n",o,l,r);
	#endif
	tree[o].l=l;
	tree[o].r=r;
	tree[o].len=r-l+1;
	if(l==r)
	{
		tree[o].sum=tree[o].addsum=tree[o].mulsum=tree[o].lsum=tree[o].rsum=(a[l]%MOD);
		tree[o].llen=tree[o].rlen=1;
		tree[o].rop=b[l];
		return;
	}
	int mid=(l+r)>>1;
	build(o<<1,l,mid);
	build(o<<1|1,mid+1,r);
	tree[o]=update(tree[o<<1],tree[o<<1|1]);
}
Tree query(int o,int l,int r)
{
	#ifdef DEBUG
	printf("Query %d %d %d\n",o,l,r);
	#endif
	if(tree[o].l==l&&tree[o].r==r)
	{
		#ifdef DEBUG
		printf("Query %d %d %d res %lld\n",o,l,r,tree[o].sum);
		#endif
		return tree[o];
	}
	int mid=(tree[o].l+tree[o].r)>>1;
	Tree res;
	if(mid>=r)
	{
		res=query(o<<1,l,r);
	}
	else if(mid<l)
	{
		res=query(o<<1|1,l,r);
	}
	else
	{
		res=update(query(o<<1,l,mid),query(o<<1|1,mid+1,r));
	}
	#ifdef DEBUG
	printf("Query %d %d %d res %lld\n",o,l,r,res.sum);
	#endif
	return res;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(int i=1;i<n;i++)
	{
		scanf("%d",&b[i]);
	}
	build(1,1,n);
	while(m--)
	{
		int op;
		int l,r;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1)
		{
			long long x;
			scanf("%lld",&x);
		}
		else if(op==2)
		{
			int x;
			scanf("%d",&x);
		}
		else
		{
			printf("%lld\n",query(1,l,r).sum);
		}
	}
	return 0;
}

只修改运算符。

变量名含义左段右段总段
(仅为示例,不在代码中维护。)算式1×21\times23+43+41×2×3+41\times2\times3+4
addsum如果这一段都是加法,结果为多少?33771010
mulsum如果这一段都是乘法,结果为多少?2212122424
lval最左边的值113311
rval最右边的值224444

Record

C++
// Problem: P5608 [Ynoi2013] 文化课
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5608
// Memory Limit: 64 MB
// Time Limit: 1500 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<cstdio>
#include<algorithm>
using namespace std;
//#define DEBUG
//#define DEBUG_BUILD
#define DEBUG_QUERY
//#define DEBUG_PUSHDOWN
//#define DEBUG_CHANGEOP
//#define DEBUG_UPDATE
#define DEBUG_RESULT
const int MAXN=100000;
const long long MOD=1e9+7;
struct Tree
{
	int l,r;//左端点与右端点
	int len;//长度
	long long sum,addsum,mulsum;//总和、和、积
	long long lval,rval;//左边和右边的值
	long long lsum,rsum;//左边和右边的极长连乘段
	int llen,rlen;//左边和右边的极长连乘段的长度
	int rop;
	int lazyop;
}tree[4*MAXN+5];
long long a[MAXN+5];
int b[MAXN+5];
int n,m;
void pushdown(int o);
Tree update(Tree a,Tree b,int lazyop=-1)
{
	Tree res;
	res.l=a.l;
	res.r=b.r;
	res.len=a.len+b.len;
	res.addsum=(a.addsum+b.addsum)%MOD;
	res.mulsum=(a.mulsum*b.mulsum)%MOD;
	res.lval=a.lval;
	res.rval=b.rval;
	res.rop=b.rop;
	res.lazyop=lazyop;
	if(a.rop)
	{
		//res.sum=(((((((a.sum+b.sum)%MOD-a.rsum)))-b.lsum))+a.rsum*b.lsum)%MOD;
		res.sum=(((((((a.sum+b.sum)%MOD)+((a.rsum*b.lsum)%MOD))%MOD)-a.rsum)-b.lsum)%MOD);
		if(res.sum<0)
		{
			res.sum=(res.sum+MOD)%MOD;
		}
	}
	else
	{
		res.sum=(a.sum+b.sum)%MOD;
	}
	if(a.rop&&(a.llen==a.len))//中间为乘
	{
		res.lsum=(a.sum*b.lsum)%MOD;
		res.llen=a.len+b.llen;
	}
	else
	{
		res.lsum=a.lsum;
		res.llen=a.llen;
	}
	if(a.rop&&(b.rlen==b.len))
	{
		res.rsum=(b.sum*a.rsum)%MOD;
		res.rlen=b.len+a.rlen;
	}
	else
	{
		res.rsum=b.rsum;
		res.rlen=b.rlen;
	}
	#ifdef DEBUG
	#ifdef DEBUG_UPDATE
	if(res.l>=438&&res.r<=500)
	{
		printf("Update                 a               b             res\n");
		printf("l       %16d%16d%16d\n",a.l,b.l,res.l);
		printf("r       %16d%16d%16d\n",a.r,b.r,res.r);
		printf("len     %16d%16d%16d\n",a.len,b.len,res.len);
		printf("addsum  %16lld%16lld%16lld\n",a.addsum,b.addsum,res.addsum);
		printf("mulsum  %16lld%16lld%16lld\n",a.mulsum,b.mulsum,res.mulsum);
		printf("sum     %16lld%16lld%16lld\n",a.sum,b.sum,res.sum);
		printf("lsum    %16lld%16lld%16lld\n",a.lsum,b.lsum,res.lsum);
		printf("rsum    %16lld%16lld%16lld\n",a.rsum,b.rsum,res.rsum);
	}
	/*if(res.r<=30)
	{
		printf("Update %d %d sum %lld lsum %lld rsum %lld middle rop %d len %d llen %d rlen %d\n",res.l,res.r,res.sum,res.lsum,res.rsum,a.rop,res.len,res.llen,res.rlen);
	}*/
	#endif
	#endif
	return res;
}
void build(int o,int l,int r)
{
	#ifdef DEBUG
	#ifdef DEBUG_BUILD
	printf("Build %d %d %d\n",o,l,r);
	#endif
	#endif
	tree[o].l=l;
	tree[o].r=r;
	tree[o].len=r-l+1;
	tree[o].lazyop=-1;
	if(l==r)
	{
		tree[o].sum=tree[o].addsum=tree[o].mulsum=tree[o].lsum=tree[o].rsum=tree[o].lval=tree[o].rval=(a[l]%MOD);
		tree[o].llen=tree[o].rlen=1;
		tree[o].rop=b[l];
		return;
	}
	int mid=(l+r)>>1;
	build(o<<1,l,mid);
	build(o<<1|1,mid+1,r);
	tree[o]=update(tree[o<<1],tree[o<<1|1],tree[o].lazyop);
}
Tree query(int o,int l,int r)
{
	#ifdef DEBUG
	#ifdef DEBUG_QUERY
	if(l>=438&&r<=500)printf("Query %d %d %d\n",o,l,r);
	#endif
	#endif
	if(tree[o].l==l&&tree[o].r==r)
	{
		#ifdef DEBUG
		#ifdef DEBUG_QUERY
		if(l>=438&&r<=500)printf("Query %d %d %d res %lld\n",o,l,r,tree[o].sum);
		#endif
		#endif
		return tree[o];
	}
	pushdown(o);
	int mid=(tree[o].l+tree[o].r)>>1;
	Tree res;
	if(mid>=r)
	{
		res=query(o<<1,l,r);
	}
	else if(mid<l)
	{
		res=query(o<<1|1,l,r);
	}
	else
	{
		res=update(query(o<<1,l,mid),query(o<<1|1,mid+1,r));
	}
	#ifdef DEBUG
	#ifdef DEBUG_QUERY
	if(l>=438&&r<=500)printf("Query %d %d %d res %lld\n",o,l,r,res.sum);
	#endif
	#endif
	return res;
}
void execopdown(int o,int x)
{
	tree[o].lazyop=x;
	tree[o].rop=x;
	if(x==0)
	{
		tree[o].sum=tree[o].addsum;
		tree[o].lsum=tree[o].lval;
		tree[o].rsum=tree[o].rval;
		tree[o].llen=tree[o].rlen=1;
	}
	else
	{
		tree[o].sum=tree[o].mulsum;
		tree[o].lsum=tree[o].mulsum;
		tree[o].rsum=tree[o].mulsum;
		tree[o].llen=tree[o].rlen=tree[o].len;
	}
}
void pushdown(int o)
{
	if(tree[o].lazyop!=-1)
	{
		#ifdef DEBUG
		#ifdef DEBUG_PUSHDOWN
		printf("Pushdown %d\n",o);
		#endif
		#endif
		execopdown(o<<1,tree[o].lazyop);
		execopdown(o<<1|1,tree[o].lazyop);
		tree[o].lazyop=-1;
	}
}
void changeop(int o,int l,int r,int x)
{
	#ifdef DEBUG
	#ifdef CHANGEOP
	printf("ChangeOP %d %d %d %d\n",o,l,r,x);
	#endif
	#endif
	if(tree[o].l==l&&tree[o].r==r)
	{
		execopdown(o,x);
		return;
	}
	pushdown(o);
	int mid=(tree[o].l+tree[o].r)>>1;
	if(mid>=r)
	{
		changeop(o<<1,l,r,x);
	}
	else if(mid<l)
	{
		changeop(o<<1|1,l,r,x);
	}
	else
	{
		changeop(o<<1,l,mid,x);
		changeop(o<<1|1,mid+1,r,x);
	}
	tree[o]=update(tree[o<<1],tree[o<<1|1],tree[o].lazyop);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(int i=1;i<n;i++)
	{
		scanf("%d",&b[i]);
	}
	build(1,1,n);
	while(m--)
	{
		int op;
		int l,r;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1)
		{
			long long x;
			scanf("%lld",&x);
		}
		else if(op==2)
		{
			int x;
			scanf("%d",&x);
			changeop(1,l,r,x);
			#ifdef DEBUG
			#ifdef DEBUG_RESULT
			for(int i=l;i<=r;i++)
			{
				b[i]=x;
			}
			#endif
			#endif
		}
		else
		{
			long long res=query(1,l,r).sum;
			printf("%lld\n",res);
			#ifdef DEBUG
			#ifdef DEBUG_RESULT
			if(res==151680449)
			{
				printf("%d %d\n",l,r);
				for(int i=l;i<=r;i++)
				{
					printf("%lld%c",a[i],b[i]?'*':'+');
				}
				printf("\n");
			}
			#endif
			#endif
		}
	}
	return 0;
}

修改数值。

运算符vector 的长度vector 中的内容
++11(1,len)(1,len)
×\times11(len,1)(len,1)

Record

C++
// Problem: P5608 [Ynoi2013] 文化课
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5608
// Memory Limit: 64 MB
// Time Limit: 1500 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
//#define DEBUG
#define DEBUG_BUILD
#define DEBUG_QUERY
#define DEBUG_PUSHDOWN
#define DEBUG_CHANGEOP
#define DEBUG_CHANGEVAL
#define DEBUG_UPDATE
//#define DEBUG_RESULT
#define pr pair<int,int>
#define Itor vector<pr >::iterator
const int MAXN=100000;
const long long MOD=1e9+7;
struct Tree
{
	int l,r;//左端点与右端点
	int len;//长度
	long long sum,addsum,mulsum;//总和、和、积
	long long lval,rval;//左边和右边的值
	long long lsum,rsum;//左边和右边的极长连乘段
	int llen,rlen;//左边和右边的极长连乘段的长度
	int rop;
	int lazyop;
	long long lazyval;
	vector<pr >all;
	void test_all()
	{
		printf("Test L %d R %d\n",l,r);
		for(int i=0;i<all.size();i++)
		{
			printf("Len %d cnt %d\n",all[i].first,all[i].second);
		}
	}
}tree[4*MAXN+5];
long long a[MAXN+5];
int b[MAXN+5];
int n,m;
void pushdown(int o);
long long qpow(long long a,int b)
{
	long long res=1;
	while(b)
	{
		if(b&1)
		{
			res=res*a%MOD;
		}
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}
Tree update(Tree a,Tree b)
{
	Tree res;
	res.l=a.l;
	res.r=b.r;
	res.len=a.len+b.len;
	res.addsum=(a.addsum+b.addsum)%MOD;
	res.mulsum=(a.mulsum*b.mulsum)%MOD;
	res.lval=a.lval;
	res.rval=b.rval;
	res.rop=b.rop;
	res.lazyop=res.lazyval=-1;
	if(a.rop)
	{
		//res.sum=(((((((a.sum+b.sum)%MOD-a.rsum)))-b.lsum))+a.rsum*b.lsum)%MOD;
		res.sum=(((((((a.sum+b.sum)%MOD)+((a.rsum*b.lsum)%MOD))%MOD)-a.rsum)-b.lsum)%MOD);
		if(res.sum<0)
		{
			res.sum=(res.sum+MOD)%MOD;
		}
	}
	else
	{
		res.sum=(a.sum+b.sum)%MOD;
	}
	if(a.rop&&(a.llen==a.len))//中间为乘
	{
		res.lsum=(a.sum*b.lsum)%MOD;
		res.llen=a.len+b.llen;
	}
	else
	{
		res.lsum=a.lsum;
		res.llen=a.llen;
	}
	if(a.rop&&(b.rlen==b.len))
	{
		res.rsum=(b.sum*a.rsum)%MOD;
		res.rlen=b.len+a.rlen;
	}
	else
	{
		res.rsum=b.rsum;
		res.rlen=b.rlen;
	}
	res.all.clear();
	Itor it1=a.all.begin(),it2=b.all.begin();
	Itor ed1=a.all.end(),ed2=b.all.end();
	while(it1!=ed1&&it2!=ed2)
	{
		if((*it1).first<(*it2).first)
		{
			res.all.push_back((*it1));
			it1++;
		}
		else if((*it1).first>(*it2).first)
		{
			res.all.push_back((*it2));
			it2++;
		}
		else
		{
			res.all.push_back(make_pair((*it1).first,(*it1).second+(*it2).second));
			it1++;
			it2++;
		}
	}
	while(it1!=ed1)
	{
		res.all.push_back(make_pair((*it1).first,(*it1).second));
		it1++;
	}
	while(it2!=ed2)
	{
		res.all.push_back(make_pair((*it2).first,(*it2).second));
		it2++;
	}
	if(a.rop)
	{
		Itor it=lower_bound(res.all.begin(),res.all.end(),make_pair(a.rlen,0));
		it->second--;
		if(!(it->second))
		{
			res.all.erase(it);
		}
		it=lower_bound(res.all.begin(),res.all.end(),make_pair(b.llen,0));
		it->second--;
		if(!(it->second))
		{
			res.all.erase(it);
		}
		int nlen=a.rlen+b.llen;
		it=lower_bound(res.all.begin(),res.all.end(),make_pair(nlen,0));
		if(it!=res.all.end()&&(*it).first==nlen)
		{
			(*it).second++;
		}
		else
		{
			res.all.insert(it,make_pair(nlen,1));
		}
	}
	#ifdef DEBUG
	#ifdef DEBUG_UPDATE
	if(res.l>=438&&res.r<=500)
	{
		printf("Update                 a               b             res\n");
		printf("l       %16d%16d%16d\n",a.l,b.l,res.l);
		printf("r       %16d%16d%16d\n",a.r,b.r,res.r);
		printf("len     %16d%16d%16d\n",a.len,b.len,res.len);
		printf("addsum  %16lld%16lld%16lld\n",a.addsum,b.addsum,res.addsum);
		printf("mulsum  %16lld%16lld%16lld\n",a.mulsum,b.mulsum,res.mulsum);
		printf("sum     %16lld%16lld%16lld\n",a.sum,b.sum,res.sum);
		printf("lsum    %16lld%16lld%16lld\n",a.lsum,b.lsum,res.lsum);
		printf("rsum    %16lld%16lld%16lld\n",a.rsum,b.rsum,res.rsum);
	}
	/*if(res.r<=30)
	{
		printf("Update %d %d sum %lld lsum %lld rsum %lld middle rop %d len %d llen %d rlen %d\n",res.l,res.r,res.sum,res.lsum,res.rsum,a.rop,res.len,res.llen,res.rlen);
	}*/
	#endif
	#endif
	return res;
}
void build(int o,int l,int r)
{
	#ifdef DEBUG
	#ifdef DEBUG_BUILD
	printf("Build %d %d %d\n",o,l,r);
	#endif
	#endif
	tree[o].l=l;
	tree[o].r=r;
	tree[o].len=r-l+1;
	tree[o].lazyop=tree[o].lazyval=-1;
	if(l==r)
	{
		tree[o].sum=tree[o].addsum=tree[o].mulsum=tree[o].lsum=tree[o].rsum=tree[o].lval=tree[o].rval=(a[l]%MOD);
		tree[o].llen=tree[o].rlen=1;
		tree[o].rop=b[l];
		tree[o].all.push_back(make_pair(1,1));
		return;
	}
	int mid=(l+r)>>1;
	build(o<<1,l,mid);
	build(o<<1|1,mid+1,r);
	tree[o]=update(tree[o<<1],tree[o<<1|1]);
}
Tree query(int o,int l,int r)
{
	#ifdef DEBUG
	#ifdef DEBUG_QUERY
	printf("Query %d %d %d\n",o,l,r);
	#endif
	#endif
	if(tree[o].l==l&&tree[o].r==r)
	{
		#ifdef DEBUG
		#ifdef DEBUG_QUERY
		printf("Query %d %d %d res %lld\n",o,l,r,tree[o].sum);
		#endif
		#endif
		return tree[o];
	}
	pushdown(o);
	int mid=(tree[o].l+tree[o].r)>>1;
	Tree res;
	if(mid>=r)
	{
		res=query(o<<1,l,r);
	}
	else if(mid<l)
	{
		res=query(o<<1|1,l,r);
	}
	else
	{
		res=update(query(o<<1,l,mid),query(o<<1|1,mid+1,r));
	}
	#ifdef DEBUG
	#ifdef DEBUG_QUERY
	printf("Query %d %d %d res %lld\n",o,l,r,res.sum);
	#endif
	#endif
	return res;
}
void execopdown(int o,int x)
{
	tree[o].lazyop=x;
	tree[o].rop=x;
	if(x==0)
	{
		tree[o].sum=tree[o].addsum;
		tree[o].lsum=tree[o].lval;
		tree[o].rsum=tree[o].rval;
		tree[o].llen=tree[o].rlen=1;
		tree[o].all.clear();
		tree[o].all.push_back(make_pair(1,tree[o].len));
	}
	else
	{
		tree[o].sum=tree[o].mulsum;
		tree[o].lsum=tree[o].mulsum;
		tree[o].rsum=tree[o].mulsum;
		tree[o].llen=tree[o].rlen=tree[o].len;
		tree[o].all.clear();
		tree[o].all.push_back(make_pair(tree[o].len,1));
	}
}
void execvaldown(int o,long long x)
{
	x%=MOD;
	tree[o].lazyval=x;
	tree[o].addsum=x*tree[o].len%MOD;
	tree[o].mulsum=qpow(x,tree[o].len);
	tree[o].lval=tree[o].rval=x;
	tree[o].lsum=qpow(x,tree[o].llen);
	tree[o].rsum=qpow(x,tree[o].rlen);
	tree[o].sum=0;
	Itor it=tree[o].all.begin();
	Itor ed=tree[o].all.end();
	while(it!=ed)
	{
		tree[o].sum+=qpow(x,(*it).first)*(*it).second%MOD;
		tree[o].sum%=MOD;
		it++;
	}
}
void pushdown(int o)
{
	if(tree[o].lazyop!=-1)
	{
		#ifdef DEBUG
		#ifdef DEBUG_PUSHDOWN
		printf("Pushdown OP %d\n",o);
		#endif
		#endif
		execopdown(o<<1,tree[o].lazyop);
		execopdown(o<<1|1,tree[o].lazyop);
		tree[o].lazyop=-1;
	}
	if(tree[o].lazyval!=-1)
	{
		#ifdef DEBUG
		#ifdef DEBUG_PUSHDOWN
		printf("Pushdown VAL %d\n",o);
		#endif
		#endif
		execvaldown(o<<1,tree[o].lazyval);
		execvaldown(o<<1|1,tree[o].lazyval);
		tree[o].lazyval=-1;
	}
}
void changeop(int o,int l,int r,int x)
{
	#ifdef DEBUG
	#ifdef DEBUG_CHANGEOP
	printf("ChangeOP %d %d %d %d\n",o,l,r,x);
	#endif
	#endif
	if(tree[o].l==l&&tree[o].r==r)
	{
		execopdown(o,x);
		return;
	}
	pushdown(o);
	int mid=(tree[o].l+tree[o].r)>>1;
	if(mid>=r)
	{
		changeop(o<<1,l,r,x);
	}
	else if(mid<l)
	{
		changeop(o<<1|1,l,r,x);
	}
	else
	{
		changeop(o<<1,l,mid,x);
		changeop(o<<1|1,mid+1,r,x);
	}
	tree[o]=update(tree[o<<1],tree[o<<1|1]);
}
void changeval(int o,int l,int r,long long x)
{
	#ifdef DEBUG
	#ifdef DEBUG_CHANGEVAL
	printf("ChangeVAL %d %d %d %d\n",o,l,r,x);
	#endif
	#endif
	if(tree[o].l==l&&tree[o].r==r)
	{
		execvaldown(o,x);
		return;
	}
	pushdown(o);
	int mid=(tree[o].l+tree[o].r)>>1;
	if(mid>=r)
	{
		changeval(o<<1,l,r,x);
	}
	else if(mid<l)
	{
		changeval(o<<1|1,l,r,x);
	}
	else
	{
		changeval(o<<1,l,mid,x);
		changeval(o<<1|1,mid+1,r,x);
	}
	tree[o]=update(tree[o<<1],tree[o<<1|1]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(int i=1;i<n;i++)
	{
		scanf("%d",&b[i]);
	}
	build(1,1,n);
	while(m--)
	{
		int op;
		int l,r;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1)
		{
			long long x;
			scanf("%lld",&x);
			changeval(1,l,r,x);
		}
		else if(op==2)
		{
			int x;
			scanf("%d",&x);
			changeop(1,l,r,x);
		}
		else
		{
			long long res=query(1,l,r).sum;
			printf("%lld\n",res);
		}
	}
	return 0;
}

优化。注意,代码有较大修改。

Record

C++
// Problem: P5608 [Ynoi2013] 文化课
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5608
// Memory Limit: 64 MB
// Time Limit: 1500 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
#define MOD 1000000007
#define MAXN 100000
long long from=-1,cache=-1;
int now;
void pushdown(int o);
long long qpow(long long a,int b);
#define Itor vector<int >::iterator
struct Tree
{
	int l,r;//左端点与右端点
	int len;//长度
	//short cntlen;//cnt数组的长度
	long long sum,addsum,mulsum;//总和、和、积
	long long lval,rval;//左边和右边的值
	long long lsum,rsum;//左边和右边的极长连乘段
	int cntlen;
	int llen,rlen;//左边和右边的极长连乘段的长度
	int rop;
	int lazyop;
	long long lazyval;
	int *cnt;
	vector<int >cntv;
	void clear_cnt()
	{
		for(int i=1;i<=cntlen;i++)
		{
			cnt[i]=0;
		}
	}
	void clear_cntv()
	{
		cntv.clear();
	}
	void update(Tree &a,Tree &b)
	{
		addsum=(a.addsum+b.addsum)%MOD;
		mulsum=(a.mulsum*b.mulsum)%MOD;
		lval=a.lval;
		rval=b.rval;
		rop=b.rop;
		if(a.rop)
		{
			//sum=(((((((a.sum+b.sum)%MOD-a.rsum)))-b.lsum))+a.rsum*b.lsum)%MOD;
			sum=(((((((a.sum+b.sum)%MOD)+((a.rsum*b.lsum)%MOD))%MOD)-a.rsum)-b.lsum)%MOD);
			if(sum<0)
			{
				sum=(sum+MOD)%MOD;
			}
		}
		else
		{
			sum=(a.sum+b.sum)%MOD;
		}
		if(a.rop&&(a.llen==a.len))//中间为乘
		{
			lsum=(a.sum*b.lsum)%MOD;
			llen=a.len+b.llen;
		}
		else
		{
			lsum=a.lsum;
			llen=a.llen;
		}
		if(a.rop&&(b.rlen==b.len))
		{
			rsum=(b.sum*a.rsum)%MOD;
			rlen=b.len+a.rlen;
		}
		else
		{
			rsum=b.rsum;
			rlen=b.rlen;
		}
		clear_cnt();
		clear_cntv();
		for(register int i=1;i<=sqrt(a.len);i++)
		{
			cnt[i]+=a.cnt[i];
		}
		for(register int i=1;i<=sqrt(b.len);i++)
		{
			cnt[i]+=b.cnt[i];
		}
		Itor it1=a.cntv.begin(),it2=b.cntv.begin();
		Itor ed1=a.cntv.end(),ed2=b.cntv.end();
		while(it1!=ed1&&it2!=ed2)
		{
			if((*it1)<(*it2))
			{
				if((*it1)<=cntlen)
				{
					cnt[(*it1)]+=1;
				}
				else
				{
					cntv.push_back((*it1));
				}
				it1++;
			}
			else if((*it1)>(*it2))
			{
				if((*it2)<=cntlen)
				{
					cnt[(*it2)]+=1;
				}
				else
				{
					cntv.push_back((*it2));
				}
				it2++;
			}
			else
			{
				if((*it1)<=cntlen)
				{
					cnt[(*it1)]+=2;
				}
				else
				{
					cntv.push_back((*it1));
					cntv.push_back((*it2));
				}
				it1++;
				it2++;
			}
		}
		while(it1!=ed1)
		{
			if((*it1)<=cntlen)
			{
				cnt[(*it1)]+=1;
			}
			else
			{
				cntv.push_back((*it1));
			}
			it1++;
		}
		while(it2!=ed2)
		{
			if((*it2)<=cntlen)
			{
				cnt[(*it2)]+=1;
			}
			else
			{
				cntv.push_back((*it2));
			}
			it2++;
		}
		if(a.rop)
		{
			if(a.rlen<=cntlen)
			{
				cnt[a.rlen]--;
			}
			else
			{
				Itor it=lower_bound(cntv.begin(),cntv.end(),a.rlen);
				cntv.erase(it);
			}
			if(b.llen<=cntlen)
			{
				cnt[b.llen]--;
			}
			else
			{
				Itor it=lower_bound(cntv.begin(),cntv.end(),b.llen);
				cntv.erase(it);
			}
			int nlen=a.rlen+b.llen;
			if(nlen<=cntlen)
			{
				cnt[nlen]++;
			}
			else
			{
				Itor it=lower_bound(cntv.begin(),cntv.end(),nlen);
				cntv.insert(it,nlen);
			}
		}
	}
	void execopdown(int x)
	{
		lazyop=x;
		rop=x;
		if(x==0)
		{
			sum=addsum;
			lsum=lval;
			rsum=rval;
			llen=rlen=1;
			clear_cnt();
			clear_cntv();
			cnt[1]=len;
		}
		else
		{
			sum=mulsum;
			lsum=mulsum;
			rsum=mulsum;
			llen=rlen=len;
			clear_cnt();
			clear_cntv();
			if(len<=cntlen)
			{
				cnt[len]=1;
			}
			else
			{
				cntv.push_back(len);
			}
		}
	}
	void execvaldown(long long x)
	{
		x%=MOD;
		lazyval=x;
		addsum=x*len%MOD;
		mulsum=qpow(x,len);
		lval=rval=x;
		lsum=qpow(x,llen);
		rsum=qpow(x,rlen);
		sum=0;
		for(int i=1;i<=cntlen;i++)
		{
			//sum+=qpow(x,i)*cnt[i];
			if(cnt[i])
			{
				if(from!=x||now>i)
				{
					cache=qpow(x,i)%MOD;
					sum+=cache*cnt[i];
					from=x;
					now=i;
				}
				else
				{
					cache=cache*qpow(x,i-now)%MOD;
					sum+=cache*cnt[i];
					now=i;
				}
				sum%=MOD;
			}
		}
		Itor it=cntv.begin();
		Itor ed=cntv.end();
		while(it!=ed)
		{
			/*sum+=qpow(x,(*it));
			sum%=MOD;*/
			if(from!=x||now>(*it))
			{
				cache=qpow(x,(*it))%MOD;
				sum+=cache;
				from=x;
				now=(*it);
			}
			else
			{
				cache=cache*qpow(x,(*it)-now)%MOD;
				sum+=cache;
				now=(*it);
			}
			sum%=MOD;
			it++;
		}
	}
	void pushdown(Tree &a,Tree &b)
	{
		if(lazyop!=-1)
		{
			a.execopdown(lazyop);
			b.execopdown(lazyop);
			lazyop=-1;
		}
		if(lazyval!=-1)
		{
			a.execvaldown(lazyval);
			b.execvaldown(lazyval);
			lazyval=-1;
		}
	}
}tree[4*MAXN+5];
struct Answer
{
	long long sum,lsum,rsum;
	int len;
	int llen,rlen;
	int rop;
};
long long a[MAXN+5];
int b[MAXN+5];
int n,m;
inline int read(){
    register int x = 0;
    register char ch=getchar(); // 读入单个字符到寄存器
    while(ch<'0'||ch>'9'){
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);  // 移位与异或
      	// 第十行可以换成 x = x * 10 + ch - '0'
        ch=getchar();
    }
    return x;
}
inline long long readll(){
    register long long x = 0;
    register char ch=getchar(); // 读入单个字符到寄存器
    while(ch<'0'||ch>'9'){
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);  // 移位与异或
      	// 第十行可以换成 x = x * 10 + ch - '0'
        ch=getchar();
    }
    return x;
}
inline void write(int x)
{
    if(x>9)
		write(x/10);
    putchar(x%10+'0');
}
inline void write(long long x)
{
    if(x>9)
		write(x/10);
    putchar(x%10+'0');
}
long long qpow(long long a,int b)
{
	long long res=1;
	while(b)
	{
		if(b&1)
		{
			res=res*a%MOD;
		}
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}
Answer update_ans(Answer a,Answer b)
{
	Answer res;
	res.len=a.len+b.len;
	res.rop=b.rop;
	if(a.rop)
	{
		//res.sum=(((((((a.sum+b.sum)%MOD-a.rsum)))-b.lsum))+a.rsum*b.lsum)%MOD;
		res.sum=(((((((a.sum+b.sum)%MOD)+((a.rsum*b.lsum)%MOD))%MOD)-a.rsum)-b.lsum)%MOD);
		if(res.sum<0)
		{
			res.sum=(res.sum+MOD)%MOD;
		}
	}
	else
	{
		res.sum=(a.sum+b.sum)%MOD;
	}
	if(a.rop&&(a.llen==a.len))//中间为乘
	{
		res.lsum=(a.sum*b.lsum)%MOD;
		res.llen=a.len+b.llen;
	}
	else
	{
		res.lsum=a.lsum;
		res.llen=a.llen;
	}
	if(a.rop&&(b.rlen==b.len))
	{
		res.rsum=(b.sum*a.rsum)%MOD;
		res.rlen=b.len+a.rlen;
	}
	else
	{
		res.rsum=b.rsum;
		res.rlen=b.rlen;
	}
	return res;
}
void build(int o,int l,int r)
{
	tree[o].l=l;
	tree[o].r=r;
	tree[o].len=r-l+1;
	tree[o].cntlen=sqrt(tree[o].len);
	tree[o].cnt=new int[tree[o].cntlen+2]();
	//tree[o].clear_cnt();
	tree[o].lazyop=tree[o].lazyval=-1;
	if(l==r)
	{
		tree[o].sum=tree[o].addsum=tree[o].mulsum=tree[o].lsum=tree[o].rsum=tree[o].lval=tree[o].rval=(a[l]%MOD);
		tree[o].llen=tree[o].rlen=1;
		tree[o].rop=b[l];
		tree[o].cnt[1]=1;
		//tree[o].cntv.push_back(make_pair(1,1));
		return;
	}
	int mid=(l+r)>>1;
	build(o<<1,l,mid);
	build(o<<1|1,mid+1,r);
	tree[o].update(tree[o<<1],tree[o<<1|1]);
}
Answer query(int o,int l,int r)
{
	if(tree[o].l==l&&tree[o].r==r)
	{
		return (Answer){tree[o].sum%MOD,tree[o].lsum%MOD,tree[o].rsum%MOD,tree[o].len,tree[o].llen,tree[o].rlen,tree[o].rop};
		//return tree[o];
	}
	tree[o].pushdown(tree[o<<1],tree[o<<1|1]);
	int mid=(tree[o].l+tree[o].r)>>1;
	Answer res;
	if(mid>=r)
	{
		res=query(o<<1,l,r);
	}
	else if(mid<l)
	{
		res=query(o<<1|1,l,r);
	}
	else
	{
		res=update_ans(query(o<<1,l,mid),query(o<<1|1,mid+1,r));
	}
	return res;
}
void changeop(int o,int l,int r,int x)
{
	if(tree[o].l==l&&tree[o].r==r)
	{
		tree[o].execopdown(x);
		return;
	}
	tree[o].pushdown(tree[o<<1],tree[o<<1|1]);
	int mid=(tree[o].l+tree[o].r)>>1;
	if(mid>=r)
	{
		changeop(o<<1,l,r,x);
	}
	else if(mid<l)
	{
		changeop(o<<1|1,l,r,x);
	}
	else
	{
		changeop(o<<1,l,mid,x);
		changeop(o<<1|1,mid+1,r,x);
	}
	tree[o].update(tree[o<<1],tree[o<<1|1]);
}
void changeval(int o,int l,int r,long long x)
{
	if(tree[o].l==l&&tree[o].r==r)
	{
		tree[o].execvaldown(x);
		return;
	}
	tree[o].pushdown(tree[o<<1],tree[o<<1|1]);
	int mid=(tree[o].l+tree[o].r)>>1;
	if(mid>=r)
	{
		changeval(o<<1,l,r,x);
	}
	else if(mid<l)
	{
		changeval(o<<1|1,l,r,x);
	}
	else
	{
		changeval(o<<1,l,mid,x);
		changeval(o<<1|1,mid+1,r,x);
	}
	tree[o].update(tree[o<<1],tree[o<<1|1]);
}
int main()
{
	//scanf("%d%d",&n,&m);
	n=read(),m=read();
	for(register int i=1;i<=n;i++)
	{
		//scanf("%lld",&a[i]);
		a[i]=readll();
	}
	for(register int i=1;i<n;i++)
	{
		//scanf("%d",&b[i]);
		b[i]=read();
	}
	build(1,1,n);
	while(m--)
	{
		register int op;
		register int l,r;
		//scanf("%d%d%d",&op,&l,&r);
		op=read();
		l=read();
		r=read();
		if(op==1)
		{
			long long x;
			//scanf("%lld",&x);
			x=readll();
			changeval(1,l,r,x);
		}
		else if(op==2)
		{
			int x;
			//scanf("%d",&x);
			x=read();
			changeop(1,l,r,x);
		}
		else
		{
			//long long res=query(1,l,r).sum;
			//printf("%lld\n",res);
			write(query(1,l,r).sum);
			putchar('\n');
		}
	}
	return 0;
}