ZOJ 3943 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


线段树暴力更新,维护小于等于0的最大值以及区间两端的值,然后就是暴力更新了。

现场赛的时候由于各种原因没能搞定,实在可惜。。。

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 4;
int T, n, m, x, a[maxn], b[maxn], l, r, A, B;

struct SegementTree
{
	const static int maxn = 8e5 + 10;
	LL f[maxn], L[maxn], R[maxn], t[maxn], sum[maxn];
	void update(int x)
	{
		f[x] = f[x << 1] + f[x << 1 | 1] + (R[x << 1] > 0 && L[x << 1 | 1] < 0);
		L[x] = L[x << 1];	R[x] = R[x << 1 | 1];
		if (max(t[x << 1], t[x << 1 | 1]) <= 0) t[x] = max(t[x << 1], t[x << 1 | 1]);
		else t[x] = min(t[x << 1], t[x << 1 | 1]);
	}
	void build(int x, int l, int r)
	{
		f[x] = sum[x] = 0;
		if (l == r) { L[x] = R[x] = t[x] = b[l]; return; }
		int m = l + r >> 1;
		build(x << 1, l, m);
		build(x << 1 | 1, m + 1, r);
		update(x);
	}
	void pushdown(int x)
	{
		sum[x << 1] += sum[x];	sum[x << 1 | 1] += sum[x];
		L[x << 1] += sum[x];	L[x << 1 | 1] += sum[x];
		R[x << 1] += sum[x];	R[x << 1 | 1] += sum[x];
		t[x << 1] += sum[x];	t[x << 1 | 1] += sum[x];
		sum[x] = 0;
	}
	void insert(int x, int l, int r, int ll, int rr, LL c)
	{
		if (sum[x]) pushdown(x);
		if (ll <= l&&r <= rr)
		{
			if (l == r)
			{
				L[x] += c;	R[x] += c;	t[x] += c;
			}
			else
			{
				if (t[x] <= 0 && t[x] + c >= 0)
				{
					int m = l + r >> 1;
					insert(x << 1, l, m, ll, rr, c);
					insert(x << 1 | 1, m + 1, r, ll, rr, c);
					update(x);
				}
				else
				{
					L[x] += c;	R[x] += c;	t[x] += c;	sum[x] += c;
				}
			}
		}
		else
		{
			int m = l + r >> 1;
			if (ll <= m) insert(x << 1, l, m, ll, rr, c);
			if (rr > m) insert(x << 1 | 1, m + 1, r, ll, rr, c);
			update(x);
		}
	}
}solve;

int main()
{
	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];
		if (n <= 2)
		{
			while (m--)
			{
				scanf("%d%d%d%d", &l, &r, &A, &B);
				printf("0\n");
			}
			continue;
		}
		solve.build(1, 1, n - 1);
		while (m--)
		{
			scanf("%d%d%d%d", &l, &r, &A, &B);
			if (l > 1) solve.insert(1, 1, n - 1, l - 1, l - 1, A);
			if (l < r) solve.insert(1, 1, n - 1, l, r - 1, B);
			if (r < n) solve.insert(1, 1, n - 1, r, r, -((LL)(r - l)*B + A));
			printf("%lld\n", solve.f[1]);
		}
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值