The shell of 16 H Xiaoyang in Niuke beginner's monthly competition (difference + line tree)

Keywords: PHP shell

Links: https://ac.nowcoder.com/acm/contest/949/H
Source: niuke.com

Title Description

There are n shells in Xiaoyang's hands, each of which has a color, and the color of the first shell is colicoli coli. Now Xiaoyang has three operations:

1 l r x: add xx to the color value of all shells in the [l,r][l,r] range.

2 l r: ask the maximum value of the difference (absolute value) of the color values of all adjacent shells in the [l,r][l,r] interval (if l=rl=r, output 0).

3 l r: ask the maximum common divisor of all shell color values in the [l,r][l,r] interval.

Enter a description:

In the first line, enter two positive integers, n,mn,m, representing the number of shells and operands respectively.
In the second line, enter the number of nn collicollicolli to represent the initial color of each shell.
From the third line to the m+2m+2 line, the first number of each line is optopt, indicating the operation number. The next input variable corresponds to the operation number.

Output Description:

There are m lines in total, and corresponding results are output for each query (operation 2 and operation 3).
Example 1

input

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

output

3
3
1
3

Remarks:

1≤n,m≤105,1≤coli,x≤103,1≤opt≤3,1≤l≤r≤n

Train of thought:
Because gcd(a,b) == gcd(a,b-a), then we will differentiate the original array, so that only two points can be modified for interval modification, and it is the same to find the difference between two adjacent points, just maintain multiple mx [] on the line tree.

Implementation code:
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid ll m = (l + r) >> 1
const ll M = 2e5+10;
ll a[M],b[M],c[M],sum[M<<2],mx[M<<2],n;
ll lowbit(ll x)
{
    return x & -x;
}
void add(ll x,ll val){
    for(ll i = x;i <= M;i += lowbit(i)){
        c[i] += val;
    }
}

ll getsum(ll x){
    ll ans = 0;
    for(ll i = x;i > 0;i -= lowbit(i)){
        ans += c[i];
    }
    return ans;
}

void up(ll rt){
    sum[rt] = __gcd(sum[rt<<1] ,sum[rt<<1|1]);
    mx[rt] = max(abs(mx[rt<<1]),abs(mx[rt<<1|1]));
}

void build(ll l,ll r,ll rt){
    if(l == r){
        sum[rt] = b[l];
        mx[rt] = b[l];
        return ;
    }
    mid;
    build(lson); build(rson);
    up(rt);
}

void update(ll p,ll c,ll l,ll r,ll rt){
    if(l == r){
        sum[rt] += c;
        mx[rt] += c;
        return ;
    }
    mid;
    if(p <= m) update(p,c,lson);
    else update(p,c,rson);
    up(rt);
}

ll query_gcd(ll L,ll R,ll l,ll r,ll rt){
    if(L <= l&&R >= r){
        return sum[rt];
    }
    mid;
    ll ret = 0;
    if(L <= m) ret = __gcd(ret,query_gcd(L,R,lson));
    if(R > m) ret = __gcd(ret,query_gcd(L,R,rson));
    return abs(ret);
}

ll query_max(ll L,ll R,ll l,ll r,ll rt){
    if(L <= l&&R >= r){
        return mx[rt];
    }
    mid;
    ll ret = 0;
    if(L <= m) ret = max(ret,abs(query_max(L,R,lson)));
    if(R > m) ret = max(ret,abs(query_max(L,R,rson)));
    return ret;
}



int main()
{
    ll m,l,r,x;
    scanf("%lld%lld",&n,&m);
    for(ll i = 1;i <= n;i ++) scanf("%lld",&a[i]),b[i]=a[i]-a[i-1];
    build(1,n,1);
    ll op;
    for(ll i = 1;i <= m;i ++){
        scanf("%lld",&op);
        if(op == 1){
            scanf("%lld%lld%lld",&l,&r,&x);
            update(l,x,1,n,1);
            if(r < n) update(r+1,-x,1,n,1);
            add(l,x); add(r+1,-x);
        }
        else if(op == 2){
            scanf("%lld%lld",&l,&r);
            ll ans;
            if(l!=r)
            ans = query_max(l+1,r,1,n,1);
            else
            ans = 0;
            printf("%lld\n",ans);
        }
        else {
            scanf("%lld%lld",&l,&r);
            ll cnt = a[l] + getsum(l);
            if(l != r){
            ll ans = query_gcd(l+1,r,1,n,1);
            printf("%lld\n",__gcd(cnt,ans));
            }
            else {
                printf("%lld\n",cnt);
            }
        }
    }
    return 0;
}

 

 

Posted by s0me0ne on Mon, 28 Oct 2019 11:14:25 -0700