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

来源:互联网 发布:淘宝精品推荐优惠券 编辑:程序博客网 时间:2024/06/05 21:49

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 liri and di (1 ≤ l ≤ r ≤ n1 ≤ 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
55 5 5 5 531 3 22 2 14 4 1
output
245
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<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<queue>#define INF 0x3f3f3f3f#define eps 1e-9#define MOD 1000000007#define N 300005  #define jud(a,b) (((a > 0) && (b > 0)) || ((a < 0) && (b < 0)))using namespace std;int n,m,l,r,val,a[N];long long b[N];struct Tree{int l,r,lmax,rmax,rud,lud,max;}tr[4*N];void update(int i){int mid = (tr[i].l + tr[i].r) >> 1,l = tr[i].l,r = tr[i].r;if(tr[2*i].lmax == mid - l + 1 && jud(b[l],b[mid+1]) > 0) tr[i].lmax = tr[2*i].lmax + tr[2*i+1].lmax;else tr[i].lmax = tr[2*i].lmax;if(tr[2*i+1].rmax == r - mid && jud(b[mid],b[r]) > 0) tr[i].rmax = tr[2*i+1].rmax + tr[2*i].rmax; else tr[i].rmax = tr[2*i+1].rmax;tr[i].max = max(tr[2*i].max,tr[2*i+1].max);if(b[mid] > 0) tr[i].max = max(tr[i].max,tr[2*i].rmax + max(tr[2*i+1].lud,tr[2*i+1].lmax));else if(b[mid] < 0)  tr[i].max = max(tr[i].max,tr[2*i].rud + (b[mid+1] < 0 ? tr[2*i+1].lmax : 0));if(b[l] > 0){tr[i].lud = tr[2*i].lud;if(tr[2*i].lud == mid - l + 1 && b[mid+1] < 0) tr[i].lud = tr[2*i].lud + tr[2*i+1].lmax;if(tr[2*i].lmax == mid - l + 1) tr[i].lud = max(tr[i].lud,tr[2*i].lmax + tr[2*i+1].lud);  }else tr[i].lud = 0;if(b[r] < 0){tr[i].rud = tr[2*i+1].rud;if(tr[2*i+1].rud == r - mid && b[mid] > 0) tr[i].rud = tr[2*i+1].rud + tr[2*i].rmax;if(tr[2*i+1].rmax == r - mid) tr[i].rud = max(tr[i].rud,tr[2*i+1].rmax + tr[2*i].rud);}else tr[i].rud = 0;}void Build(int i,int l,int r){tr[i].l = l;tr[i].r = r;if(l == r){if(b[l] > 0) {tr[i].lud = tr[i].lmax = tr[i].rmax = tr[i].max= 1;tr[i].rud = 0;}else  if(b[l] < 0)  { tr[i].rud = tr[i].lmax = tr[i].rmax = tr[i].max = 1; tr[i].lud = 0; } else tr[i].lmax = tr[i].rmax = tr[i].rud = tr[i].lud = tr[i].max = 0;return;} int mid = (tr[i].l + tr[i].r) >> 1;Build(2*i,l,mid);Build(2*i+1,mid+1,r); update(i);}void deal(int i,int x,int val){int l = tr[i].l,r = tr[i].r;if(l == r){b[l] += val;if(b[l] > 0) {tr[i].lud = tr[i].lmax = tr[i].rmax = tr[i].max= 1;tr[i].rud = 0;}else  if(b[l] < 0)  { tr[i].rud = tr[i].lmax = tr[i].rmax = tr[i].max = 1; tr[i].lud = 0; } else tr[i].lmax = tr[i].rmax = tr[i].rud = tr[i].lud = tr[i].max = 0;return; } int mid = (l + r) >> 1; if(x <= mid) deal(2*i,x,val); else deal(2*i+1,x,val); update(i);}int main(){scanf("%d",&n);for(int i = 1;i <= n;i++) scanf("%d",&a[i]);for(int i = 1;i <= n;i++) b[i-1] = a[i] - a[i-1];n--;if(n) Build(1,1,n);scanf("%d",&m);for(int i = 1;i <= m;i++){scanf("%d%d%d",&l,&r,&val);if(n == 1){printf("1\n");continue;}if(l-1) deal(1,l-1,val);if(r <= n) deal(1,r,-val);printf("%d\n",tr[1].max+1);}}



0 0
原创粉丝点击