「codeforces 438D」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:

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.
Examples
Input
Copy

5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3

扫描二维码关注公众号,回复: 2939998 查看本文章

Output
Copy

8
5

Input
Copy

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
Copy

49
15
23
1
9

Note

Consider the first testcase:

线段树板子题,我们进行两种操作:单点更新和区间取余。
但是在取余的时候,我们无法进行懒标记(取余不能往下传递),因此我们只能查到区间的每个点,但是这样的话会超时 。我们考虑这样一种情况:如果这个区间内的最大值小于模,那么我们就没有必要对该区间进行更新取余,这样就能实现高效剪枝。注意这道题的数据范围。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100505;
typedef long long ll;
ll sum[maxn<<2],mx[maxn<<2];
struct node{
    ll l,r;
    ll mid(){
        return (l+r)>>1;
    }
}tree[maxn<<2];
void pushup(ll rt){
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void buildtree(ll l,ll r,ll rt){
    tree[rt].l=l;
    tree[rt].r=r;
    if(l==r){
        scanf("%lld",&sum[rt]);
        mx[rt]=sum[rt];
        return ;
    }
    ll mid=tree[rt].mid();
    buildtree(l,mid,rt<<1);
    buildtree(mid+1,r,rt<<1|1);
    pushup(rt);
}

void update1(ll c,ll l,ll r,ll rt,ll x){
    if(l==r){
        sum[rt]=c;
        mx[rt]=c;
        return ;
    }
    int mid=(l+r)>>1;
    if(x<=mid){
        update1(c,l,mid,rt<<1,x);
    }
    else update1(c,mid+1,r,rt<<1|1,x);
    pushup(rt);
}

void update2(ll c,ll l,ll r,ll rt){
    if(tree[rt].l>=l&&tree[rt].r<=r){
        if(mx[rt]<c) return ;
        if(tree[rt].l==tree[rt].r){
            sum[rt]%=c;
            mx[rt]%=c;
            return ;
        }
    }
    ll mid=tree[rt].mid();
    if(r<=mid){
        update2(c,l,r,rt<<1);
    }
    else if(l>mid){
        update2(c,l,r,rt<<1|1);
    }
    else{
        update2(c,l,mid,rt<<1);
        update2(c,mid+1,r,rt<<1|1);
    }
    pushup(rt);
}
ll query(ll l,ll r,ll rt){
    if(tree[rt].r==r&&tree[rt].l==l){
        return sum[rt];
    }
    ll res=0;
    ll mid=tree[rt].mid();
    if(r<=mid) res+=query(l,r,rt<<1);
    else if(l>mid) res+=query(l,r,rt<<1|1);
    else{
        res+=query(l,mid,rt<<1);
        res+=query(mid+1,r,rt<<1|1);
    }
    return res;
}
int main(){
    ll n,q;
    scanf("%lld %lld",&n,&q);
    buildtree(1,n,1);
    int op;
    int a,b,c;
    while(q--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d %d",&a,&b);
            printf("%lld\n",query(a,b,1));
        }
        else if(op==2){
            scanf("%d %d %d",&a,&b,&c);
            update2(c,a,b,1);
        }
        else {
            scanf("%d %d",&a,&b);
            update1(b,1,n,1,a);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/duanghaha/article/details/81945887