Time limit 4000 ms
Memory limit 262144 kB
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.
题目分析
此题关键在于取膜的一个性质
一个数
x
x
x被取膜到0的最多只要
l
o
g
x
logx
logx步
显然为了让每次
x
x
x被取膜后的值都尽量大,每一次取膜的值都为
x
/
2
x/2
x/2
相当于每次除以2,所以最多取
l
o
g
x
logx
logx次膜即可到0
于是我们可以用线段树维护区间和的同时在维护一个区间最大值
每次区间修改进入左右区间前先检查其区间最大值是否小于取膜值,若是则不进入修改这个区间
对于其他的我们甚至可以不用打延迟标记,直接修改到最低端
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;
lt read()
{
lt f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const int maxn=100010;
int n,m;
lt a[maxn];
lt sum[maxn<<2],mx[maxn<<2];
void pushup(int p)
{
sum[p]=sum[p<<1]+sum[p<<1|1];
mx[p]=max(mx[p<<1],mx[p<<1|1]);
}
void build(int s,int t,int p)
{
if(s==t){ sum[p]=mx[p]=a[s]; return;}
int mid=s+t>>1;
build(s,mid,p<<1);build(mid+1,t,p<<1|1);
pushup(p);
}
void update(int ll,int rr,int s,int t,int p,lt x)
{
if(s==t){ sum[p]%=x; mx[p]%=x; return;}
int mid=s+t>>1;
if(ll<=mid&&mx[p<<1]>=x) update(ll,rr,s,mid,p<<1,x);
if(rr>mid&&mx[p<<1|1]>=x) update(ll,rr,mid+1,t,p<<1|1,x);
pushup(p);
}
lt qsum(int ll,int rr,int s,int t,int p)
{
if(ll<=s&&t<=rr) return sum[p];
int mid=s+t>>1;lt ans=0;
if(ll<=mid) ans+=qsum(ll,rr,s,mid,p<<1);
if(rr>mid) ans+=qsum(ll,rr,mid+1,t,p<<1|1);
return ans;
}
void change(int u,int s,int t,int p,lt x)
{
if(s==t){ sum[p]=mx[p]=x; return;}
int mid=s+t>>1;
if(u<=mid) change(u,s,mid,p<<1,x);
else change(u,mid+1,t,p<<1|1,x);
pushup(p);
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;++i) a[i]=read();
build(1,n,1);
while(m--)
{
lt opt=read(),ll=read(),rr=read();
if(opt==1) printf("%lld\n",qsum(ll,rr,1,n,1));
else if(opt==2) update(ll,rr,1,n,1,read());
else if(opt==3) change(ll,1,n,1,rr);
}
return 0;
}