[AT2442]フェーン現象 (Foehn Phenomena)
题目大意:一个数组$A$和$P$,若$A_i>A_{i+1}$,$P_{i+1}=P_i+(A_i-A_{i+1})\times S$,否则$P_{i+1}=P_i-(A_{i+1}-A_i)\times T$,每次区间加$[l,r]$范围的$A$数组,问$P_n$的值。
题解:发现可以差分,$O(1)$求出解(我原来想用树状数组的,然后发现没什么用)
卡点:爆$int$
C++ Code:
#include <cstdio> #define maxn 200010 int n, m, S, T; long long a[maxn], ans; inline long long get(long long x) { if (x > 0) return static_cast<long long> (-x) * S; else return static_cast<long long> (-x) * T; } long long modify(int l, int r, int h) { ans -= get(a[l]); a[l] += h; ans += get(a[l]); if (r != n) ans -= get(a[r + 1]); a[r + 1] -= h; if (r != n) ans += get(a[r + 1]); return ans; } int main() { scanf("%d%d%d%d%*d", &n, &m, &S, &T); for (int i = 1, last = 0, x; i <= n; i++) { scanf("%d", &x); a[i] = x - last; last = x; ans += get(a[i]); } while (m --> 0) { int l, r, h; scanf("%d%d%d", &l, &r, &h); printf("%lld\n", modify(l, r, h)); } return 0; }