80分一个超时,求助!(开02了)

P2367 语文成绩

Lin080903 @ 2023-09-18 20:38:06

#include<bits/stdc++.h>
using namespace std;
int n,p,a[5000010],b1,b2,fen;
int main()
{
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    for(int i=1;i<=p;i++)
    {
        scanf("%d%d%d",&b1,&b2,&fen);
        for(int j=b1;j<=b2;j++)
        a[j]+=fen;
    }
    sort(a+1,a+n+1);
    printf("%d",a[1]);
    return 0;
}

by xQWQx @ 2023-09-18 20:40:26

@Lin080903

这个暴力过不了


by _Haoomff_ @ 2023-09-18 20:41:14

@Lin080903 这题要用差分的思想去做


by xQWQx @ 2023-09-18 20:41:37

@Lin080903

你可以参考一下题解,题解讲的很详细


by Lin080903 @ 2023-09-18 20:43:39

谢谢,我看看去


by kathy_accton @ 2023-10-04 21:48:34

请用差分解答


by Tan100520 @ 2023-10-11 22:35:03

@C112345565 我用暴力也过了啊

#include<bits/stdc++.h>
using namespace std;
int n,p,a[114514],minn = INT_MAX; 
int main(){
    cin >> n >> p;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= p; i++) {
        int x,y,z;cin >> x >> y >> z;
        for (int j = x; j <= y; j++) a[j] += z;
    }
    for (int i = 1; i <= n; i++) minn = min(minn,a[i]);
    cout << minn;
    return 0;
}

by Tan100520 @ 2023-10-11 22:37:18

@Tan100520 开O2,看这里


by xQWQx @ 2023-10-12 21:20:09

@Tan100520

好吧QWQ

你怎么这么久的贴都在回复


|