hainoir @ 2024-10-17 16:12:04
实在找不到原因了,和题解思路也是一样的
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5*1e6+9;
ll a[N],d[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll n,p,res;cin >> n >> p;
for(int i = 1;i <= n;i++)cin >> a[i];
for(int i = 1;i <= n;i++)d[i] = a[i] - a[i-1];
while(p--){
ll x,y,z;cin >> x >> y >> z;
d[x] += z;
d[y + 1] -= z;
}
for(int i = 1;i <= n;i++)a[i] = d[i] + a[i - 1];
for(int i = 1;i <= n;i++)res = min(res,a[i]);
cout << res <<'\n';
return 0;
}
by Ff472130 @ 2024-10-17 16:22:16
res没设置初值,取min的时候可能出错
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5*1e6+9;
ll a[N],d[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll n,p,res=1e18;cin >> n >> p;
for(int i = 1;i <= n;i++)cin >> a[i];
for(int i = 1;i <= n;i++)d[i] = a[i] - a[i-1];
while(p--){
ll x,y,z;cin >> x >> y >> z;
d[x] += z;
d[y + 1] -= z;
}
for(int i = 1;i <= n;i++)a[i] = d[i] + a[i - 1];
for(int i = 1;i <= n;i++)res = min(res,a[i]);
cout << res <<'\n';
return 0;
}
求关