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.
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:
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 (L, R, A, B), 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.
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 N, M (1 ≤ N, M ≤ 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 L, R, A, B (1 ≤ L ≤ R ≤ N, 1 ≤ B ≤ 105, -105 ≤ A ≤ 105) of an earthquake, in chronological order.
For each earthquake, output the amount of peak after it.
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
1 1 1 0
1 5 3 0 -> 1 5 4 2 -> 1 5 6 5
Author: CHEN, WeijieSource: 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; }