Codeforces Round #381 (Div. 2) E. Alyona and towers

来源:互联网 发布:ubuntu paichu fuzhi 编辑:程序博客网 时间:2024/06/06 13:58

E. Alyona and towers

Description

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(lkr) 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(1n3105) — the number of towers.

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

The third line contain single integer m(1m3105) — the number of additions.

The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1lrn,1di109), 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.

题意

对于一系列塔高ai, 求满足
al<al+1<al+2<<ak>ak+1>ak+2>>ar.的最长区间[l,r]。

塔高ai会根据m次操作变化,对每次变化给出此刻的解。

分析

针对区间修改及维护的问题,很容易想到线段树。

可以想到al<al+1<al+2<<ak>ak+1>ak+2>>ar,令sub[i]=a[i]a[i1](sub[i]>0+;sub[i]<0)

此时问题即可转换为求满足++++序列的sub的最长区间。

而区间更新即可转换为对sub[l1]+d,sub[r]d的单点更新。

#include<bits/stdc++.h>using namespace std;const int maxn = 3e5+10;long long sub[maxn],a[maxn];struct Node{    int left,right,lmx,rmx,mx;}seg[maxn<<2];void upmx(int l,int r,int p){    int mid = (l+r)>>1;    seg[p].mx = max(seg[p*2].mx,seg[p*2+1].mx);    seg[p].lmx = seg[p*2].lmx;    seg[p].rmx = seg[p*2+1].rmx;    if((sub[mid] > 0 && sub[mid+1] != 0) || (sub[mid] < 0 && sub[mid+1] < 0)){        seg[p].mx = max(seg[p].mx,seg[p*2].rmx + seg[p*2+1].lmx);        if(mid-l+1 == seg[p*2].lmx) seg[p].lmx = seg[p*2].lmx + seg[p*2+1].lmx;        if(r-mid == seg[p*2+1].rmx) seg[p].rmx = seg[p*2+1].rmx + seg[p*2].rmx;    }}void build(int l,int r,int p){    seg[p].left = l,    seg[p].right = r;    if(l == r){        if(sub[l])  seg[p].lmx = 1, seg[p].rmx = 1, seg[p].mx = 1;        return;    }    int mid = (l+r)>>1;    build(l,mid,p*2);    build(mid+1,r,p*2+1);    upmx(l,r,p);}void update(int l,int r,int idx,int p){    if(l == r && l == idx){        if(sub[l])    seg[p].lmx = 1, seg[p].rmx = 1, seg[p].mx = 1;        else    seg[p].lmx = 0, seg[p].rmx = 0, seg[p].mx = 0;        return;    }    int mid = (l+r)>>1;    if(l <= idx && idx <= mid)  update(l,mid,idx,p*2);    else    update(mid+1,r,idx,p*2+1);    upmx(l,r,p);}int main(){    int n,m;    scanf("%d",&n);    for(int i=1;i<=n;i++)        scanf("%I64d",&a[i]);    for(int i=1;i<n;i++)        sub[i] = a[i+1] - a[i];    if(n!=1)    build(1,n-1,1);    scanf("%d",&m);    for(int i=0,l,r,d;i<m;i++) {        scanf("%d %d %d",&l,&r,&d);        if(n==1)    {   printf("1\n");  continue;   };        if(l!=1)    sub[l-1] += d,    update(1,n-1,l-1,1);        if(r!=n)    sub[r] -= d,  update(1,n-1,r,1);        printf("%d\n",seg[1].mx+1);    }}
0 0
原创粉丝点击