CodeForces - 438D - The Child and Sequence(线段树+取模操作)

题目:

At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.

Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he should perform a sequence of m operations. An operation can be one of the following:

  1. Print operation l, r. Picks should write down the value of .
  2. Modulo operation l, r, x. Picks should perform assignment a[i] = a[imod x for each i (l ≤ i ≤ r).
  3. Set operation k, x. Picks should set the value of a[k] to x (in other words perform an assignment a[k] = x).

Can you help Picks to perform the whole sequence of operations?

Input

The first line of input contains two integer: n, m (1 ≤ n, m ≤ 105). The second line contains n integers, separated by space: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — initial value of array elements.

Each of the next m lines begins with a number type .

  • If type = 1, there will be two integers more in the line: l, r (1 ≤ l ≤ r ≤ n), which correspond the operation 1.
  • If type = 2, there will be three integers more in the line: l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 109), which correspond the operation 2.
  • If type = 3, there will be two integers more in the line: k, x (1 ≤ k ≤ n; 1 ≤ x ≤ 109), which correspond the operation 3.

Output

For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.

Examples

input

5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3

output

8
5

input

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

output

49
15
23
1
9

题意:一段区间,1操作求 l 到 r的和,2操作把 l 到 r区间内的数进行取模操作,3操作把其中x位置的数变成z

题解:求和好求,线段树维护区间和,取模必须要递归到节点中,同时维护一个最大值,当子区间内的最大值都比当前取模的z值小的时候,那么整个子区间都不用进行取模操作了,因为不变。然后单点修改都是常规操作了,范围要开到long long才行,纪念一下第一次全自己写的线段树,且没出现各种奇葩Bug

代码:

#include<bits/stdc++.h>
#define N 100005
#define lson node<<1
#define rson node<<1|1
#define LL long long
using namespace std;
int n,m;
LL ans;
LL a[N];
struct ljh
{
	int l,r;
	LL lazy,sum,mx;
}e[N<<2];

inline void pushup(int node)
{
	e[node].mx=max(e[lson].mx,e[rson].mx);
	e[node].sum=e[lson].sum+e[rson].sum;
}
inline void pushdown(int node)
{
	if(e[node].lazy)
	{
		e[lson].mx+=e[node].lazy;
		e[rson].mx+=e[node].lazy;
		e[lson].sum+=(e[lson].r-e[lson].l+1)*e[node].lazy;
		e[rson].sum+=(e[rson].r-e[rson].l+1)*e[node].lazy;
		e[lson].lazy+=e[node].lazy;
		e[rson].lazy+=e[node].lazy;
		e[node].lazy=0;
	}
}

void Build(int node,int l,int r)
{
	e[node].l=l;
	e[node].r=r;
	e[node].lazy=0;
	if(l==r)
	{
		e[node].sum=a[l];
		e[node].mx=a[l];
		return;
	}
	int m=(l+r)>>1;
	Build(lson,l,m);
	Build(rson,m+1,r);
	pushup(node);
}

void makesum(int node,int x,int y)
{
	if(x<=e[node].l&&e[node].r<=y)
	{
		ans+=e[node].sum;
		return ;
	}
	pushdown(node);
	int m=(e[node].l+e[node].r)>>1;
	if(x<=m)makesum(lson,x,y);
	if(y>m)makesum(rson,x,y);
	pushup(node);
}

void UpdateMod(int node,int x,int y,LL z)
{
	if(e[node].mx<z)return;
	if(x<=e[node].l&&e[node].r<=y&&e[node].l==e[node].r)
	{
		e[node].mx=(e[node].mx%z);
		e[node].sum=(e[node].sum%z);
		return;
	}
	pushdown(node);
	int m=(e[node].l+e[node].r)>>1;
	if(x<=m)UpdateMod(lson,x,y,z);
	if(y>m)UpdateMod(rson,x,y,z);
	pushup(node);
}

void Update(int node,int x,LL z)
{
	if(x==e[node].l&&e[node].r==x)
	{
		e[node].mx=z;
		e[node].sum=z;
		return;
	}
	pushdown(node);
	int m=(e[node].l+e[node].r)>>1;
	if(x<=m)Update(lson,x,z);
	else Update(rson,x,z);
	pushup(node);
}

int main()
{
#ifdef LOCAL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	Build(1,1,n);
	while(m--)
	{
		int op,x,y;
		LL z;
		scanf("%d",&op);
		if(op==1)
		{
			scanf("%d%d",&x,&y);
			ans=0;
			makesum(1,x,y);
			printf("%lld\n",ans);
		}
		if(op==2)
		{
			scanf("%d%d%lld",&x,&y,&z);
			UpdateMod(1,x,y,z);
		}
		if(op==3)
		{
			scanf("%d%lld",&x,&z);
			Update(1,x,z);
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值