蒟蒻求助!!!1

P2367 语文成绩

lij123 @ 2023-08-28 06:52:21

#include<bits/stdc++.h>
using namespace std;
int n,a[1000001],s[1000001],b[1000001],q;
int main(){
    int n,m,minn=1000;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        b[i]=x;
    }
    for(int i=1;i<=m;i++){
        int l,r,w;
        cin>>l>>r>>w;
        b[l]+=w;
        b[r+1]-=w;
    }
    for(int i=1;i<=n;i++){
        s[i]=s[i-1]+b[i];
        minn=min(s[i],minn);
    }
    cout<<minn;
}

by free_fall @ 2023-08-28 07:08:34

@lij123 minn改为极大值,以及b数组原本就有值,不能直接差分,记到另一个数组里。


by free_fall @ 2023-08-28 07:10:20

改成这样

#include<bits/stdc++.h>
using namespace std;
int n,a[5000001],s[5000001],b[5000001],c[5000001],q;
int main(){
    int n,m,minn=10000000;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        b[i]=x;
    }
    for(int i=1;i<=m;i++){
        int l,r,w;
        cin>>l>>r>>w;
        c[l]+=w;
        c[r+1]-=w;
    }
    for(int i=1;i<=n;i++){
        s[i]=s[i-1]+c[i];
        b[i]+=s[i];
        minn=min(b[i],minn);
    }
    cout<<minn;
}

by lij123 @ 2023-08-28 07:10:21

明白了


|