ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

线段树之区间更新The Child and Sequence

2022-07-18 22:35:08  阅读:180  来源: 互联网

标签:10 Sequence int 线段 Picks Child operation should line


Description

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

  • 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.

Sample Input

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

Hint

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.
    #include<cstdio>
    #include<iostream>
    using namespace std;
    #define ll long long
    
    const int N=1e5+5;
    ll f[N<<2];//因为是存区间和,所以要开longlong
    ll lazy[N<<2];//存最值不用longlong
    int n,m;
    
    void pushup(int root){
        int rt=root<<1; 
        
        f[root]=f[rt]+f[rt+1];
        lazy[root]=max(lazy[rt],lazy[rt+1]);
    }
    
    
    void build(int left,int right,int root){
        if(left==right){
            
            scanf("%lld",&f[root]);
            lazy[root]=f[root];
            
            return ;
        }
        
        int mid=(left+right)>>1;
        int rt=root<<1;
        
        build(left,mid,rt);
        build(mid+1,right,rt+1);
        
        pushup(root);
    }
    
    void change(int x,int val,int left,int right,int root){//单点修改
        if(left==right){
            lazy[root]=f[root]=val;
            return ;
        }
        
        int mid=(left+right)>>1;
        int rt=root<<1;
        
        if(x<=mid){
            change(x,val,left,mid,rt);
        }else{
            change(x,val,mid+1,right,rt+1);
        }
        
        pushup(root);
    }
    
    void update(int x,int uleft,int uright,int left,int right,int root){//区间取模
        
        if(lazy[root]<x)
        {
            return ;
        }//区间最值如果小于x,则不用继续递归
        
        if(left==right){
            lazy[root]=(f[root]%=x);
            return ;
        }
        
        int rt=root<<1;
        int mid=(left+right)>>1;
        
        if(uleft<=mid){
            update(x,uleft,uright,left,mid,rt);
        }
        
        if(uright>mid){
            update(x,uleft,uright,mid+1,right,rt+1);
        }
        
        pushup(root);
    }
    
    ll query(int qleft,int qright,int left,int right,int root){//区间求和
        if(qleft<=left&&qright>=right){
            return f[root];
        }
        
        int mid=(left+right)/2;
        int rt=root<<1;
        
        ll ans=0;
        
        if(qleft<=mid){
            ans+=query(qleft,qright,left,mid,rt);
        }
        
        if(qright>mid){
            ans+=query(qleft,qright,mid+1,right,rt+1);
        }
        return ans;
    }
    
    int main(){
        
        scanf("%d%d",&n,&m);
        build(1,n,1);
        long long ans=0;
        int op,l,r,x,val;
        while(m--){
            scanf("%d",&op);
            
            if(op==1)
            {
                
                scanf("%d%d",&l,&r);
                ans=query(l,r,1,n,1);
                printf("%lld\n",ans);
                
            }else if(op==2)
            {
            
                scanf("%d%d%d",&l,&r,&x);
                update(x,l,r,1,n,1);
            }
            else
            {
                
                scanf("%d%d",&x,&val);
                change(x,val,1,n,1);
            }
        }
        return 0;
    }

     

标签:10,Sequence,int,线段,Picks,Child,operation,should,line
来源: https://www.cnblogs.com/killjoyskr/p/16492268.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有