看了这道题,咚咚锵,现在在TianTang看题解
查看原帖
看了这道题,咚咚锵,现在在TianTang看题解
265037
UntilR楼主2020/8/4 21:42

样例过不去,求助大佬。

#include <bits/stdc++.h>
using namespace std;
struct tree
{
	int left,right;
	long long answer,tag_sum,tag_plus;
}a[300001];
int n,m,mo,num[200001];
long long z;
void push_down(int l,int r,int dot)
{
	a[dot*2].answer=a[dot*2].answer*a[dot].tag_plus+
					(a[dot*2].right-a[dot*2].left)*a[dot].tag_sum;
	a[dot*2+1].answer=a[dot*2+1].answer*a[dot].tag_plus+
					(a[dot*2+1].right-a[dot*2+1].left)*a[dot].tag_sum;
	a[dot*2].tag_sum+=a[dot].tag_sum;
	a[dot*2].tag_plus*=a[dot].tag_plus;
	a[dot*2+1].tag_sum+=a[dot].tag_sum;
	a[dot*2+1].tag_plus*=a[dot].tag_plus;
	a[dot].tag_sum=0;
	a[dot].tag_plus=1;
	return;
}
void tree_search(int nl,int nr,int l,int r,int dot)
{
	if(nl<=l&&r<=nr)
	{
		z+=a[dot].answer;
		return;
	}
	push_down(l,r,dot);
	int mid=(l+r)/2;
	if(nl<=mid)
		tree_search(nl,nr,l,mid,dot*2);
	if(nr>mid)
		tree_search(nl,nr,mid+1,r,dot*2+1);
	a[dot].answer=a[dot*2].answer+a[dot*2+1].answer;
	return;
}
void tree_plus(int nl,int nr,int l,int r,int k,int dot)
{
	if(nl<=l&&r<=nr)
	{
		a[dot].tag_plus*=k;
		a[dot].tag_sum*=k;
		a[dot].answer*=k;
		return;
	}
	push_down(l,r,dot);
	int mid=(l+r)/2;
	if(nl<=mid)
		tree_plus(nl,nr,l,mid,k,dot*2);
	if(nr>mid)
		tree_plus(nl,nr,mid+1,r,k,dot*2+1);
	a[dot].answer=a[dot*2].answer+a[dot*2+1].answer;
	return;
}
void tree_sum(int nl,int nr,int l,int r,int k,int dot)
{
	if(nl<=l&&r<=nr)
	{
		a[dot].tag_sum+=k;
		a[dot].answer+=(r-l+1)*k;
		return;
	}
	push_down(l,r,dot);
	int mid=(l+r)/2;
	if(nl<=mid)
		tree_sum(nl,nr,l,mid,k,dot*2);
	if(nr>mid)
		tree_sum(nl,nr,mid+1,r,k,dot*2+1);
	a[dot].answer=a[dot*2].answer+a[dot*2+1].answer;
	return;
}
void build_tree(int l,int r,int dot)
{
	a[dot].left=l;
	a[dot].right=r;
	if(l==r)
	{
		a[dot].answer=num[l];
		return;
	}
	int mid=(l+r)/2;
	build_tree(l,mid,dot*2);
	build_tree(mid+1,r,dot*2+1);
	a[dot].answer=a[dot*2].answer+a[dot*2+1].answer;
	return;
}
int main()
{
	std::ios::sync_with_stdio(0);
	cin>>n>>m>>mo;
	for(int i=1;i<=n;i++)
	{
		cin>>num[i];
		a[i].tag_plus=1;
	}
	build_tree(1,n,1);
	for(int task=1;task<=m;task++)
	{
		int type;
		cin>>type;
		if(type==1)
		{
			int l,r,k;
			cin>>l>>r>>k;
			tree_sum(l,r,1,n,k,1);
		}
		if(type==2)
		{
			int l,r,k;
			cin>>l>>r>>k;
			tree_plus(l,r,1,n,k,1);
		}
		if(type==3)
		{
			int l,r;
			z=0;
			cin>>l>>r;
			tree_search(l,r,1,n,1);
			cout<<z%mo<<endl;
		}
	}
	return 0;
}
2020/8/4 21:42
加载中...