80分!!!求助dalao帮助!!!

P2367 语文成绩

linzihang @ 2024-07-08 20:19:29

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

80分!!!最后一个TLE!本蒟蒻不知道什么是差分,希望有dalao指点一二!!!跪谢!!!


by cao691234 @ 2024-07-08 20:23:32

哪道题


by cao691234 @ 2024-07-08 20:30:50

差分就是用一个数组把另一个等差数列之间的差算出来,利用它进行单值查询


by linzihang @ 2024-07-08 20:33:28

@cao691234 能再讲清楚一些吗?谢谢!!!


by linzihang @ 2024-07-08 20:33:42

@cao691234 P2367


by _Panyc @ 2024-07-08 20:35:53

大概是这样吧

#include<bits/stdc++.h>
using namespace std;
int n,q,d[5000007],a,minn=0x3f3f3f3f,x,y,z;
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)scanf("%d",&d[i]);
    for(int i=n;i>1;i--)d[i]-=d[i-1];
    while(q--)
    {
        scanf("%d%d%d",&x,&y,&z);
        d[x]+=z;d[y+1]-=z;
    }
    minn=d[1];
    for(int i=2;i<=n;i++)
    {
        d[i]+=d[i-1];if(d[i]<minn)minn=d[i];
    }
    cout<<minn;
    return 0;
}

by _Panyc @ 2024-07-08 20:37:22

@linzihang


by linzihang @ 2024-07-08 20:37:28

@_Panyc 谢谢!!!!!!!


by Rainbow_rabbit @ 2024-07-16 16:24:49

https://www.luogu.com.cn/article/atvi5gen


|