codeforce438D The Child and Sequence

codeforce438D 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 img.
  2. Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod 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 img.

  • 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

Note

Consider the first testcase:

  • At first, a = {1, 2, 3, 4, 5}.
  • After operation 1, a = {1, 2, 3, 0, 1}.
  • After operation 2, a = {1, 2, 5, 0, 1}.
  • At operation 3, 2 + 5 + 0 + 1 = 8.
  • After operation 4, a = {1, 2, 2, 0, 1}.
  • At operation 5, 1 + 2 + 2 = 5.

一.解题思路及要点

  1. 题目大意:长度为n的非负整数数列,3种操作

    1. 求[L,R]所有数的和。
    2. 将[L,R]中所有数都mod x。
    3. 将a[i]修改为v。
  2. 线段树的表示方法:

    (1) 利用数组来表示(如果需要维护多个值则需要开多个数组)

    (2)利用结构体来表示(包括l,r,val等)

  3. 区间取模的应用: 对于一个区间如果其最大值都小于mod(对mod取模),则无需操作,否则就需要暴力取模,复杂度为nlog(n)

  4. 线段树区间修改,区间查询,单点修改等操作的应用

  5. 每次修改后都需要维护区间的和与区间的最大值

t[ind]=t[ind<<1]+t[ind<<1|1];
m[ind]=max(m[ind<<1],m[ind<<1|1]);

二.完整代码

using namespace std;
#include<bits/stdc++.h>
int n,mm,type;
const int maxn=100005;
long long t[maxn<<2];//用于维护区间的和
long long m[maxn<<2];//用于维护区间最大值
long long int a[maxn];

void build(long long k,long long l,long long r)
{
    if(l==r) 
    {
        t[k]=a[l];
        m[k]=a[l];
        return;//注意需要返回并跳出函数
    }
    long long mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r); 
    t[k]=t[k<<1]+t[k<<1|1];
    m[k]=max(m[k<<1],m[k<<1|1]);
    return ;
}

void upd(long long k,long long l,long long r,long long ind,long long x)
{
    if(l==r)
    {
        t[ind]=x;
        m[ind]=x;
        return ;//注意需要返回并跳出函数
    }
    long long mid=(l+r)>>1;
    if(k<=mid)upd(k,l,mid,ind<<1,x);
    else upd(k,mid+1,r,ind<<1|1,x);
    t[ind]=t[ind<<1]+t[ind<<1|1];
    m[ind]=max(m[ind<<1],m[ind<<1|1]);
}
long long query(long long l,long long r,long long L,long long R,long long ind)
{
    //注意是L>=l而不是l>=L,注意此处判断条件的理解:当前区间是所求区间的子集时直接返回t[ind]
    if(L>=l&&R<=r) return t[ind];
    long long mid=(L+R)>>1;
    long long ans=0;
    //注意这种思路的学习,利用两个if语句就将所有的情况包含进去
    if(l<=mid)ans+=query(l,r,L,mid,ind<<1);
    if(r>mid) ans+=query(l,r,mid+1,R,ind<<1|1);
    return ans;
}

void modee(long long l,long long r ,long long L,long long R ,long long ind,long long x)
{
    if(l<=L&&r>=R&&m[ind]<x) return ;//注意是<x不是<=x
    if(L==R)
    {
        t[ind]%=x;
        m[ind]=t[ind];
        return;//记住要返回
    }
    long long mid=(L+R)>>1;
    //注意这种思路的学习,利用两个if语句就将所有的情况包含进去
    if(l<=mid) modee(l,r,L,mid,ind<<1,x);
    if(r>mid) modee(l,r,mid+1,R,ind<<1|1,x);
    t[ind]=t[ind<<1]+t[ind<<1|1];
    m[ind]=max(m[ind<<1],m[ind<<1|1]);
}

int main()
{
    scanf("%d%d",&n,&mm);
    for(long long i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    build(1,1,n);
    long long int l,r,x,k;
    while(mm--)
    {
        scanf("%d",&type);
        if(type==1)
        {
            scanf("%lld%lld",&l,&r);
            printf("%lld\n",query(l,r,1,n,1));
        }
        else if(type==2)
        {
            scanf("%lld%lld%lld",&l,&r,&x);
            modee(l,r,1,n,1,x);
        }
        else if(type==3)
        {
            scanf("%lld%lld",&k,&x);
            upd(k,1,n,1,x);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值