Codeforces Round #381 (Div. 1) C. Alyona and towers(线段树)

Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.

Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.

Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.

After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.

Input
The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.

The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.

The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.

The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≤ l ≤ r ≤ n, 1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.

Output
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.

Example
input
5
5 5 5 5 5
3
1 3 2
2 2 1
4 4 1
output
2
4
5
Note
The first sample is as follows:

After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to[7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.

After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.

After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.

题意:给你一个数列,m个操作每次修改一段区间,问每次修改后数列的最长山峰(al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.)、

分析:差分后线段树,讨论

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

struct node
{
    int l,r;
    int ln,lpn;
    int rp,rpn;
    int tans;
}tr[300005*4];

int n,m;
long long a[300005];

node up(node x,node y)
{
    node ntr;
    ntr.l=x.l;
    ntr.r=y.r;

    //ln
    ntr.ln=x.ln;
    if(x.ln==(x.r-x.l+1))
    {
        ntr.ln=x.ln+y.ln;
    }
    //rp
    ntr.rp=y.rp;
    if(y.rp==(y.r-y.l+1))
    {
        ntr.rp=y.rp+x.rp;
    }
    //lpn
    ntr.lpn=x.lpn;
    if(x.lpn==(x.r-x.l+1))
    {
        ntr.lpn=x.lpn+y.ln;
    }
    if(x.rp==(x.r-x.l+1))
    {
        ntr.lpn=max(ntr.lpn,x.rp+y.lpn);
        ntr.lpn=max(ntr.lpn,x.rp+y.ln);
    }
    //rpn
    ntr.rpn=y.rpn;
    if(y.rpn==(y.r-y.l+1))
    {
        ntr.rpn=y.rpn+x.rp;
    }
    if(y.ln==(y.r-y.l+1))
    {
        ntr.rpn=max(ntr.rpn,y.ln+x.rpn);
        ntr.rpn=max(ntr.rpn,y.ln+x.rp);
    }

    ntr.tans=max(x.tans,y.tans);
    ntr.tans=max(ntr.tans,max(ntr.ln,ntr.lpn));
    ntr.tans=max(ntr.tans,max(ntr.rp,ntr.rpn));

    ntr.tans=max(ntr.tans,max(x.rp+y.lpn,x.rpn+y.ln));
    ntr.tans=max(ntr.tans,x.rp+y.ln);

    return ntr;
}

void build(int i,int l,int r)
{
    tr[i].l=l;
    tr[i].r=r;
    if(l==r)
    {
        if(a[l]==a[l-1])
        {
            tr[i].ln=0;
            tr[i].rp=0;
            tr[i].lpn=0;
            tr[i].rpn=0;
            tr[i].tans=0;
        }
        else if(a[l]>a[l-1])
        {
            tr[i].ln=0;
            tr[i].rp=1;
            tr[i].lpn=0;
            tr[i].rpn=0;
            tr[i].tans=1;
        }
        else
        {
            tr[i].ln=1;
            tr[i].rp=0;
            tr[i].lpn=0;
            tr[i].rpn=0;
            tr[i].tans=1;
        }
    }
    else
    {
        int mid=(l+r)/2;
        build(2*i,l,mid);
        build(2*i+1,mid+1,r);

        tr[i]=up(tr[2*i],tr[2*i+1]);
    }

}
void update(int i,int l,int r,long long v)
{
    if(tr[i].l==l && tr[i].r==r)
    {
        if(v==0)
        {
            tr[i].ln=0;
            tr[i].rp=0;
            tr[i].lpn=0;
            tr[i].rpn=0;
            tr[i].tans=0;
        }
        else if(v>0)
        {
            tr[i].ln=0;
            tr[i].rp=1;
            tr[i].lpn=0;
            tr[i].rpn=0;
            tr[i].tans=1;
        }
        else
        {
            tr[i].ln=1;
            tr[i].rp=0;
            tr[i].lpn=0;
            tr[i].rpn=0;
            tr[i].tans=1;
        }
    }
    else
    {
        int mid=(tr[i].l+tr[i].r)/2;
        if(r<=mid)
        {
            update(2*i,l,r,v);
        }
        else if(l>mid)
        {
            update(2*i+1,l,r,v);
        }
        else
        {
            update(2*i,l,mid,v);
            update(2*i+1,mid+1,r,v);
        }
        tr[i]=up(tr[2*i],tr[2*i+1]);
    }

}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }

        if(n==1)
        {
            scanf("%d",&m);
            while(m--)
            {
                int l,r,d;
                scanf("%d%d%d",&l,&r,&d);
                printf("1\n");
            }
            return 0;
        }
        build(1,2,n);
        scanf("%d",&m);
        for(int i=n;i>=2;i--)
        {
            a[i]=a[i]-a[i-1];
        }

        for(int i=0;i<m;i++)
        {
            int l,r,d;
            scanf("%d%d%d",&l,&r,&d);

            if(l>=2)
            {
                a[l]+=d;
                update(1,l,l,a[l]);
            }
            if(r<n)
            {
                a[r+1]-=d;
                update(1,r+1,r+1,a[r+1]);
            }
            printf("%d\n",tr[1].tans+1);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值