Cambridge @ 2020-12-11 14:02:30
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long n,k,shu,ans=2100000000,s[5000010],x,y,z,ss[5000010];
int main()
{
cin>>n>>k;
for(int i=1; i<=n; i++)scanf("%lld",&s[i]);
for(int i=1; i<=k; i++)
{
scanf("%lld%lld%lld",&x,&y,&z);
if(x>y)swap(x,y);
s[x]+=z,s[y+1]-=z;
}
for(int i=1; i<=n; i++)shu+=s[i],ss[i]+=shu,ans=min(ans,ss[i]);
cout<<ans;
return 0;
}
by 天外来客 @ 2021-05-21 20:21:06
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long n, k, ans=1e9, s[5000010], x, y, z, ss[5000010];
int main()
{
cin>>n>>k;
for(int i=1; i<=n; i++)
{
scanf("%lld",&ss[i]);
s[i] = ss[i] - ss[i-1];
}
for(int i=1; i<=k; i++)
{
scanf("%lld%lld%lld",&x,&y,&z);
s[x] += z;
s[y+1] -= z;
}
for(int i=1; i<=n; i++)
{
ss[i] = ss[i-1] + s[i];
ans = min(ans,ss[i]);
}
cout << ans;
return 0;
}
by 天外来客 @ 2021-05-21 20:21:36
改成差分
by l_yu @ 2021-07-07 16:38:22
@Cambridge
s数组与ss数组混淆了,注意一下输入。