Libre OJ P2332「JOI 2017 Final」焚风现象【差分思想】By cellur925

题目传送门

这道题开始看起来会很晕...\(qwq\)。首先我们要明确题目中的海拔&&温度。温度是受海拔影响的,每次改变的是海拔,我们求的是温度。

我们开始读入的时候便可以处理出开始\(N\)位置的温度以及各个位置的海拔差。每次读入影响的是一段区间,区间内的相对海拔是不变的因此温度也不会变。只有区间的边界可能受到影响。因此我们只要处理边界就行了:这便是差分的思想

比如有\([l,r]\)区间需要处理,那么我们把\(l\)位置的原答案减去,把\(l\)位置的海拔改变,并加上新答案。再对\(r+1\)位置做类似的处理,其他部分不会受到影响。

\(Code\)

#include<cstdio>
#include<algorithm>

using namespace std;
typedef long long ll;

int n,Q,las;
ll s,t,ans,a[200090];

ll cal(ll x)
{
	return x>0 ? -x*s : -x*t;
}

int main()
{
	scanf("%d%d%lld%lld",&n,&Q,&s,&t);
	scanf("%d",&las);
	for(int i=1;i<=n;i++)
	{
		int x=0;
		scanf("%d",&x);
		a[i]=x-las;
		las=x;
		ans+=cal(a[i]);
	}
	while(Q--)
	{
		int x=0,y=0,z=0;
		scanf("%d%d%d",&x,&y,&z);
		ans-=cal(a[x]);
		a[x]+=z;
		ans+=cal(a[x]);
		if(y==n){printf("%lld\n",ans);continue;}
		ans-=cal(a[y+1]);
		a[y+1]-=z;
		ans+=cal(a[y+1]);
		printf("%lld\n",ans);
	}
	return 0;
}
posted @ 2018-11-02 17:20  cellur925&Chemist  阅读(489)  评论(0编辑  收藏  举报