19暑假线段树A

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:

Print operation l, r. Picks should write down the value of .
Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod x for each i (l ≤ i ≤ r).
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.
板题,区间访问,区间取模和单点修改
可记录区间最大值,区间取模暴力取模,由上向下遍历,如果区间最大值小于模则跳过

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 100005
using namespace std;
int ma[maxn<<2];
long long sum[maxn<<2];
void pushup(int k)
{
	ma[k]=max(ma[k<<1],ma[k<<1|1]);
	sum[k]=sum[k<<1]+sum[k<<1|1];
}
void build(int l,int r,int k)
{
	if(l==r)
	{
		scanf("%d",ma+k);
		sum[k]=(long long)ma[k];
		return;
	}
	int mid=(l+r)/2;
	build(l,mid,k<<1);
	build(mid+1,r,k<<1|1);
	pushup(k);
}
void set(int l,int r,int k,int loc,int x)
{
	if(r==l)
	{
		ma[k]=x;
		sum[k]=(long long)x;
		return;
	}
	int mid=(l+r)/2;
	if(loc<=mid)
	set(l,mid,k<<1,loc,x);
	else
	set(mid+1,r,k<<1|1,loc,x);
	pushup(k);
}
void setmod(int l,int r,int k,int L,int R,int mod)
{
	if(ma[k]<mod)
	return;
	if(r==l)
	{
		ma[k]=ma[k]%mod;
		sum[k]=(long long)ma[k];
		return;
	}
	int mid=(l+r)/2;
	if(L<=mid)
	setmod(l,mid,k<<1,L,R,mod);
	if(R>mid)
	setmod(mid+1,r,k<<1|1,L,R,mod);
	pushup(k);
}
long long querysum(int l,int r,int k,int L,int R)
{
	if(L<=l&&r<=R)
	return sum[k];
	int mid=(l+r)/2;
	if(R<=mid)
	return querysum(l,mid,k<<1,L,R);
	if(mid<L)
	return querysum(mid+1,r,k<<1|1,L,R);
	return querysum(l,mid,k<<1,L,R)+querysum(mid+1,r,k<<1|1,L,R);	
} 
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	build(1,n,1);
	int op,loc,L,R,c,mod;
	while(m--)
	{
		scanf("%d",&op);
		if(op==1)
		{
			scanf("%d%d",&L,&R);
			printf("%lld\n",querysum(1,n,1,L,R));
		}
		if(op==2)		
		{
			scanf("%d%d%d",&L,&R,&mod);
			setmod(1,n,1,L,R,mod);
		}
		if(op==3)
		{
			scanf("%d%d",&loc,&c);
			set(1,n,1,loc,c);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值