ZOJ3943-Himalayas

Himalayas

Time Limit: 5 Seconds       Memory Limit: 65536 KB

The Himalayas or Himalaya is a mountain range in the Indian subcontinent, which separates the Indo-Gangetic Plain from the Tibetan Plateau. This range is home to nine of the ten highest peaks on Earth, including the highest above sea level, Mount Everest. The Himalayas have profoundly shaped the cultures of South Asia. Many Himalayan peaks are sacred in both Buddhism and Hinduism.


By Sudan Shrestha. License: CC-by-sa 3.0

Edward, the headmaster of Marjar University, is doing a research about the Himalayas. To simplify the model, Edward thinks that there are N mountains in the Himalayas and they are lined up. Edward numbered them from 1 to N from left to right. What's more, Edward defines peaks of the Himalayas which satisfy:

  1. 1 < i < N
  2. Hi - 1 < Hi > Hi + 1, where Hi means the height of the i-th mountain

Then the i-th mountain is called a peak.

Furthermore, Edward found an interesting fact about the Himalayas from its history: earthquakes will change the height of some continuous mountains. To be more specific, for an earthquake, we record it as (LRAB), which means the height of the i-th (L ≤ i ≤ R) mountain will change by A + (i - L) * B after the earthquake.

Edward wants to know the amount of peaks after each earthquake. Please tell him.

Input

There are multiple test cases. The first line of input contains an integer T (≤ 10) indicating the number of test case. For each test case:

The first line contains two integers NM (1 ≤ NM ≤ 105) indicating the number of the mountains and the number of the earthquakes.

The next line contains N (1 ≤ N ≤ 105) integers, the i-th integer is the initial height Hi of the i-th mountain (0 ≤ Hi ≤ 105).

Then followed by M lines, each line contains four integers LRAB (1 ≤ L ≤ R ≤ N, 1 ≤ B ≤ 105, -105 ≤ A ≤ 105) of an earthquake, in chronological order.

Output

For each earthquake, output the amount of peak after it.

Sample Input
2
4 2
1 5 3 0
3 4 1 1
3 4 2 1
4 2
1 5 3 0
3 4 1 1
3 4 1 1
Sample Output
1
1
1
0
Hint

1 5 3 0 -> 1 5 4 2 -> 1 5 6 5


Author:  CHEN, Weijie
Source:  The 13th Zhejiang Provincial Collegiate Programming Contest


题意:有n座山,每座山有个高度,若满足1<i<n&&Hi-1>Hi<Hi+1,那么它就是山谷,每次地震,第L到第R座山高度会发生变化,每座山变化为A+(i-L)*B(1<=i<=n),问每次地震后有几座山谷

解题思路:线段树,因为每座山高度变化是不同的,所以可以对山的相对高度进行建线段树,区间更新即可


#include <iostream>    
#include <cstdio>    
#include <cstring>    
#include <string>    
#include <algorithm>    
#include <map>    
#include <cmath>  
#include <set>    
#include <stack>    
#include <queue>    
#include <vector>    
#include <bitset>    
#include <functional>  

using namespace std;  

#define LL long long    
const int INF = 0x3f3f3f3f;

int a[100005],b[100005],n,m;
LL sum[100005*4],lh[100005*4],rh[100005*4],lazy[100005*4],h[100005*4];
int aa,bb,ll,rr;

void Merge(int k,int l,int r)
{
	lh[k]=lh[l],rh[k]=rh[r];
	sum[k]=sum[l]+sum[r]+(rh[l]>0&&lh[r]<0);
	if(max(h[l],h[r])<=0) h[k]=max(h[l],h[r]);
	else h[k]=min(h[l],h[r]);
}

void Push(int k,int l,int r)
{
	lazy[l]+=lazy[k],lazy[r]+=lazy[k];
	lh[l]+=lazy[k],rh[l]+=lazy[k];
	lh[r]+=lazy[k],rh[r]+=lazy[k];
	h[l]+=lazy[k],h[r]+=lazy[k];
	lazy[k]=0;
}

void build(int k,int l,int r)
{
	lazy[k]=sum[k]=0;
	if(l==r) {lh[k]=rh[k]=h[k]=b[l];return ;}
	int mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	Merge(k,k<<1,k<<1|1);
}

void update(int k,int l,int r,int ll,int rr,LL val)
{
	if(l>=ll&&r<=rr)
	{
		if(l==r)
		{
			lh[k]+=val,rh[k]+=val,h[k]+=val;
		}
		else if(h[k]<=0&&h[k]+val>=0)
		{
			int mid=(l+r)>>1;
			if(lazy[k]) Push(k,k<<1,k<<1|1);
			update(k<<1,l,mid,ll,rr,val);
			update(k<<1|1,mid+1,r,ll,rr,val);
			Merge(k,k<<1,k<<1|1);
		}
		else if(h[k]>=0&&&h[k]+val<=0)
		{
			int mid=(l+r)>>1;
			if(lazy[k]) Push(k,k<<1,k<<1|1);
			update(k<<1,l,mid,ll,rr,val);
			update(k<<1|1,mid+1,r,ll,rr,val);
			Merge(k,k<<1,k<<1|1);
		}
		else
		{
			lh[k]+=val,rh[k]+=val,lazy[k]+=val,h[k]+=val;
		}
		return ;
	}
	int mid=(l+r)>>1;
	if(lazy[k]) Push(k,k<<1,k<<1|1);
	if(ll<=mid) update(k<<1,l,mid,ll,rr,val);
	if(rr>mid) update(k<<1|1,mid+1,r,ll,rr,val);
	Merge(k,k<<1,k<<1|1);
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<n;i++) b[i]=a[i+1]-a[i]; 
		build(1,1,n-1);
		while(m--)
		{
			scanf("%d%d%d%d",&ll,&rr,&aa,&bb);
			if(ll>1) update(1,1,n-1,ll-1,ll-1,aa);
			if(ll<rr) update(1,1,n-1,ll,rr-1,bb);
			if(rr<n) update(1,1,n-1,rr,rr,-(1LL*bb*(rr-ll)+aa));
			printf("%lld\n",sum[1]);
		}
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值