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

185 篇文章 0 订阅
18 篇文章 0 订阅

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
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<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值