线段树板子

这篇博客讲述了如何优化线段树算法以应对取模操作的特殊性,通过维护区间最大值来判断是否需要取模,确保对所有数最多进行O(n log n)次操作。作者详细介绍了问题背景、解决方案和代码实现,适用于CodeForces-438D题目的解决思路。
摘要由CSDN通过智能技术生成

线段树板子题

A. 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:
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.

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

题目来源
CodeForces - 438D

就是给线段树的板子题本来区间应该是用lazytag打标的,但是这个题取模运算比较特别然后以下是学长PPT的分析

对于一段区间,如果取模的数比这段区间所有的数都大,那取模就是没有意义的,就是说,如果取模的数比区间最大的数还大,那么就不用取模了,所以我们在线段树里再记录一个区间最大值,考虑每次取模,对于每一个数x,取模y,x mod y的值必然比y小,如果y小于x/2,那x就变得小于x/2,如果y大于x/2,x剩下的部分也比x/2少,x也会变得比x/2小 那么就是说x每次取模都会变得比x/2小,就是说,对于一个数x,有效的取mod最多进行logx次,一共只会进行nlogn次取模,那么就算对所有的数取模,这个时间复杂度都是可以接受的 那么我们对于每个区间记录一个最大值,如果取模的数大于最大值,就不管,如果小于最大值,就暴力取模

然后就是代码实现

#include <bits/stdc++.h>
typedef long long ll;
ll a[100010];
struct node{
	int l,r;
	ll sum,max;
}tree[100010<<2];//线段树至少开四倍空间 
int m,n;
ll maxn(ll a,ll b){
	return a>b?a:b;
}
void build(int k,int l,int r){//建树
	tree[k].l=l;
	tree[k].r=r;
	int mid=(l+r)/2;
	if(l==r){
	    tree[k].sum=a[l];
	    tree[k].max=a[l];
	    return;
	}
	build(k*2,l,mid);
	build(k*2+1,mid+1,r);
	tree[k].sum=tree[2*k].sum+tree[2*k+1].sum;//区间维护 
	tree[k].max=maxn(tree[2*k].max,tree[2*k+1].max);//区间维护 
	return;
}
void renew(int k,int num,ll newn){//区间更新,k当前节点,num被更新节点,newn更新值 
	if(tree[k].l==tree[k].r&&tree[k].l==num){
		tree[k].sum=newn;
		tree[k].max=newn;
		return;
	}
	int mid=(tree[k].l+tree[k].r)/2;
	if(num<=mid)//在左子树 
		renew(2*k,num,newn);
	else if(num>=mid+1)//在右子树 
		renew(2*k+1,num,newn);
    tree[k].sum=tree[2*k].sum+tree[2*k+1].sum;
    tree[k].max=maxn(tree[2*k].max,tree[2*k+1].max);
	return;  
}
void takemod(int k,int l,int r,ll m){//因为这题驱魔的特殊性这个函数是暴力取模,其实区间修改应该lazytag打标的k是当前节点,l,r区间端点,m模值 
	if(tree[k].max<m)
		return;
	int mid=(tree[k].l+tree[k].r)/2;
	if(tree[k].l==tree[k].r){
		tree[k].max=tree[k].max%m;
		tree[k].sum=tree[k].sum%m; 
		return;
	}
	if(r<=mid)
		takemod(2*k,l,r,m);
	else if(l>=mid+1) 
		takemod(2*k+1,l,r,m);
	else{
		takemod(2*k,l,mid,m);
		takemod(2*k+1,mid+1,r,m);
	}
	tree[k].sum=tree[2*k].sum+tree[2*k+1].sum;
    tree[k].max=maxn(tree[2*k].max,tree[2*k+1].max);
}
ll putout(int k,int l,int r)//查询函数相当于query 
{
	if(tree[k].l==l&&tree[k].r==r)
		return tree[k].sum;
	int mid=(tree[k].l+tree[k].r)/2;
	if(r<=mid)
		return putout(2*k,l,r);
	else if(l>=mid+1)
		return putout(2*k+1,l,r);
	else
		return (putout(2*k,l,mid)+putout(2*k+1,mid+1,r));
}
void shuchu(){//输出 
	int l,r;
	scanf("%d %d",&l,&r);
	printf("%lld\n",putout(1,l,r));
}
void qumo(){//取模 
	int l,r;ll mo;
	scanf("%d %d %lld",&l,&r,&mo);
	takemod(1,l,r,mo);
}
void huanshu(){//换数 
	int num;ll nu;
	scanf("%d %lld",&num,&nu);
	renew(1,num,nu);
}
int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	memset(tree,0,sizeof(tree));
	build(1,1,n);
	int p;
	for(int i=1;i<=m;i++){
		scanf("%d",&p);
		switch(p){
			case 1 :shuchu();   break;
			case 2 :qumo();     break;
			case 3 :huanshu();  break;	
		}
	}
    return 0;
}

好了这就是第一篇博客
2021.7.16

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值