QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#672838#8718. 保区间最小值一次回归问题ucup-team4479TL 0ms13844kbC++142.0kb2024-10-24 19:26:522024-10-24 19:26:53

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 19:26:53]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:13844kb
  • [2024-10-24 19:26:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int N=500005;
constexpr long long INF=1e18;
int n,m;
int a[N];
struct Seg
{
    int l,r,v;
}s[N];
int pos[N],tot;
int nxt[N];
int getf(int v)
{
    if(v==nxt[v]) return v;
    else return nxt[v]=getf(nxt[v]);
}
int pre[N];
long long f[N];
long long solve(vector<pair<int,int>>seg,int v)
{
    tot=0;
    for(auto [l,r]:seg)
    {
        for(int x=getf(l);x<=r;x=nxt[x])
        {
            pos[++tot]=x;
            nxt[x]=getf(x+1);
        }
    }
    sort(pos+1,pos+tot+1);
    for(int i=1;i<=tot;i++)
        pre[i]=0;
    for(auto [l,r]:seg)
    {
        int ll=lower_bound(pos+1,pos+tot+1,l)-pos;
        int rr=upper_bound(pos+1,pos+tot+1,r)-pos-1;
        if(ll>rr) return -1;
        pre[rr]=max(pre[rr],ll);
    }
    int lst=0;
    f[0]=0;
    for(int i=1;i<=tot;i++)
    {
        lst=max(lst,pre[i-1]);
        f[i]=INF;
        for(int j=lst;j<=i;j++)
            f[i]=min(f[i],f[j]+abs(a[pos[i]]-v));
        if(a[pos[i]]<v) lst=i;
    }
    lst=max(lst,pre[tot]);
    long long res=INF;
    for(int i=lst;i<=tot;i++)
        res=min(res,f[i]);
    return res;
}
void solve()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=m;i++)
        cin>>s[i].l>>s[i].r>>s[i].v;
    for(int i=1;i<=n+1;i++)
        nxt[i]=i;
    sort(s+1,s+m+1,[](const Seg &x,const Seg &y){return x.v<y.v;});
    long long ans=0;
    for(int i=1,j=1;i<=m;i=j)
    {
        while(j<=m&&s[i].v==s[j].v) j++;
        vector<pair<int,int>>seg;
        for(int k=i;k<j;k++)
            seg.emplace_back(s[k].l,s[k].r);
        long long sum=solve(seg,s[i].v);
        if(sum==-1)
        {
            cout<<-1<<"\n";
            return;
        }
        ans+=sum;
    }
    cout<<ans<<"\n";
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 13844kb

input:

1
3 2
2023 40 41
1 1 2022
2 3 39

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: -100
Time Limit Exceeded

input:

1000
100 100
1 35141686 84105222 84105220 7273527 178494861 178494861 112519027 77833654 77833656 261586535 278472336 278472336 261586536 416361017 416361017 426649080 323519513 278472337 420127821 420127823 420127823 482516531 434108818 420127821 631535744 615930922 546346921 546346920 546346920 70...

output:


result: