[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;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/9883306.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值