原题链接:http://codeforces.com/problemset/problem/438/D
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] 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
l
,
r
. Picks should write down the value of
∑ri=la[i]
∑
i
=
l
r
a
[
i
]
.
Modulo operation
l, r, x
l
,
r
,
x
. Picks should perform assignment
a[i] = a[i] mod x
a
[
i
]
=
a
[
i
]
m
o
d
x
for each
i (l ≤ i ≤ r)
i
(
l
≤
i
≤
r
)
.
Set operation
k, x
k
,
x
. Picks should set the value of
a[k]
a
[
k
]
to
x
x
(in other words perform an assignment ).
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) n , m ( 1 ≤ n , m ≤ 10 5 ) . The second line contains n integers, separated by space: a[1], a[2], ..., a[n](1 ≤ a[i] ≤ 109) a [ 1 ] , a [ 2 ] , . . . , a [ n ] ( 1 ≤ a [ i ] ≤ 10 9 ) — initial value of array elements.
Each of the next m lines begins with a number type (type∈{1,2,3}) ( t y p e ∈ { 1 , 2 , 3 } ) .
If type =
1
1
, there will be two integers more in the line: , which correspond the operation 1.
If type =
2
2
, there will be three integers more in the line: , which correspond the operation 2.
If type =
3
3
, there will be two integers more in the line: , 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
Note
Consider the first testcase:
At first,
a = {1, 2, 3, 4, 5}
a
=
{
1
,
2
,
3
,
4
,
5
}
.
After operation
1,a = {1, 2, 3, 0, 1}
1
,
a
=
{
1
,
2
,
3
,
0
,
1
}
.
After operation
2,a = {1, 2, 5, 0, 1}
2
,
a
=
{
1
,
2
,
5
,
0
,
1
}
.
At operation
3,2 + 5 + 0 + 1 = 8
3
,
2
+
5
+
0
+
1
=
8
.
After operation
4,a = {1, 2, 2, 0, 1}
4
,
a
=
{
1
,
2
,
2
,
0
,
1
}
.
At operation
5,1 + 2 + 2 = 5
5
,
1
+
2
+
2
=
5
.
题解
如果取模生效的话,被%的数至少会变成原来的一半,所以我们统计一下区间最大值,如果 max≥mod m a x ≥ m o d 就暴力递归下去取模,最多取 log2max l o g 2 m a x 次。因为修改是单点的,所以对复杂度的影响不大,最终复杂度为 O(n log2n log2max) O ( n l o g 2 n l o g 2 m a x ) 。
代码
#include<bits/stdc++.h>
#define ll long long
#define ls v<<1
#define rs ls|1
using namespace std;
const int M=4e5+5;
ll que[M],mx[M],sum[M],n,m;
void up(int v){mx[v]=max(mx[ls],mx[rs]),sum[v]=sum[ls]+sum[rs];}
void build(int v,int le,int ri)
{
if(le==ri){mx[v]=sum[v]=que[le];return;}
int mid=le+ri>>1;
build(ls,le,mid);build(rs,mid+1,ri);
up(v);
}
ll ask(int v,int le,int ri,int lb,int rb)
{
if(lb<=le&&ri<=rb)return sum[v];
int mid=le+ri>>1;ll ans=0;
if(lb<=mid)ans=ask(ls,le,mid,lb,rb);
if(mid<rb)ans+=ask(rs,mid+1,ri,lb,rb);
return ans;
}
void mod(int v,int le,int ri,int lb,int rb,int d)
{
if(le==ri){mx[v]=sum[v]=mx[v]%d;return;}
int mid=le+ri>>1;
if(lb<=mid&&mx[ls]>=d)mod(ls,le,mid,lb,rb,d);
if(mid<rb&&mx[rs]>=d)mod(rs,mid+1,ri,lb,rb,d);
up(v);
}
void alter(int v,int le,int ri,int pos,int d)
{
if(le==ri){sum[v]=mx[v]=d;return;}
int mid=le+ri>>1;
if(pos<=mid)alter(ls,le,mid,pos,d);
else alter(rs,mid+1,ri,pos,d);
up(v);
}
void in(){scanf("%d%d",&n,&m);for(int i=1;i<=n;++i)scanf("%d",&que[i]);}
void ac()
{
build(1,1,n);int a,b,c,op;
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&op,&a,&b);
if(op==1)printf("%I64d\n",ask(1,1,n,a,b));
else if(op==2)scanf("%d",&c),mod(1,1,n,a,b,c);
else alter(1,1,n,a,b);
}
}
int main(){in();ac();}