Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树暴力)

D. The Child and Sequence
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
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 1a = {1, 2, 3, 0, 1}.
  • After operation 2a = {1, 2, 5, 0, 1}.
  • At operation 32 + 5 + 0 + 1 = 8.
  • After operation 4a = {1, 2, 2, 0, 1}.
  • At operation 51 + 2 + 2 = 5.


    大致题意:

    对一数列三种操作:

    1.对L,R区间的所有数mod x

    2.把a[pos]改成x

    3.询问L,R区间的所有数之和


    思路:第二第三中操作都很好解决

    第一种操作要知道,一个数mod另一个数x,他小于x就不需要模,否则,这个数模后必减小一半以上

    所以暴力模操作就可以了,一个数最多模logn次。

    复杂度是:nlogn*logn

    
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string>
    #include <vector>
    #include <cstdio>
    #include <ctime>
    #include <bitset>
    #include <algorithm>
    #define SZ(x) ((int)(x).size())
    #define ALL(v) (v).begin(), (v).end()
    #define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
    #define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
    #define REP(i,n) for ( int i=1; i<=int(n); i++ )
    using namespace std;
    typedef long long ll;
    #define X first
    #define Y second
    typedef pair<int,int> pii;
    
    template <class T>
    inline bool RD(T &ret) {
    	char c; int sgn;
    	if (c = getchar(), c == EOF) return 0;
    	while (c != '-' && (c<'0' || c>'9')) c = getchar();
    	sgn = (c == '-') ? -1 : 1;
    	ret = (c == '-') ? 0 : (c - '0');
    	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
    	ret *= sgn;
    	return 1;
    }
    template <class T>
    inline void PT(T x) {
    	if (x < 0) {
    		putchar('-');
    		x = -x;
    	}
    	if (x > 9) pt(x / 10);
    	putchar(x % 10 + '0');
    }
    
    
    const int N = 1e5+100;
    int maxn[N<<2];
    ll sum[N<<2];
    int n,m;
    
    #define root 1,n,1
    #define ls rt<<1
    #define rs rt<<1|1
    
    inline void pushup(int rt){
            maxn[rt] = max(maxn[ls],maxn[rs]);
            sum[rt] = sum[ls]+sum[rs];
    }
    
    void build(int l,int r,int rt){
            if( l == r){
                    RD(maxn[rt]);
                    sum[rt] = maxn[rt];
                    return ;
            }
            int m = (l+r)>>1;
            build(l,m,ls);
            build(m+1,r,rs);
            pushup(rt);
    }
    
    ll query(int L,int R,int l,int r,int rt){
            if( L <= l && r <= R){
                    return sum[rt];
            }
            int m = (l+r)>>1;
            ll ans = 0;
            if( L <= m) ans += query(L,R,l,m,ls);
            if( R > m) ans += query(L,R,m+1,r,rs);
            return ans;
    }
    
    void getmod(int L,int R,int mod,int l,int r,int rt){
            if( maxn[rt] < mod) return;
            if( l == r ){
                    maxn[rt] %= mod;
                    sum[rt] %= mod;
                    return ;
            }
            int m = (l+r)>>1;
            if( L <= m) getmod(L,R,mod,l,m,ls);
            if( R > m) getmod(L,R,mod,m+1,r,rs);
            pushup(rt);
    }
    
    void update(int pos,int x,int l,int r,int rt){
            if( l == r) {
                    sum[rt] = x;
                    maxn[rt] = x;
                    return;
            }
            int m = (l+r)>>1;
            if( pos <= m ) update(pos,x,l,m,ls);
            else update(pos,x,m+1,r,rs);
            pushup(rt);
    }
    
    int main(){
            cin>>n>>m;
            build(root);
            while(m--){
                    int op; RD(op);
                    if( op == 1){
                            int l,r;
                            RD(l),RD(r);
                            printf("%I64d\n",query(l,r,root));
                    }
                    else if( op == 2 ){
                            int l,r,mod;
                            scanf("%d%d%d",&l,&r,&mod);
                            getmod(l,r,mod,root);
                    }
                    else{
                            int pos,x;
                            scanf("%d%d",&pos,&x);
                            update(pos,x,root);
                    }
            }
    }
    


    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值