QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#437845#8718. 保区间最小值一次回归问题233LWA 359ms30604kbC++142.4kb2024-06-09 18:40:332024-06-09 18:40:33

Judging History

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

  • [2024-06-09 18:40:33]
  • 评测
  • 测评结果:WA
  • 用时:359ms
  • 内存:30604kb
  • [2024-06-09 18:40:33]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define ldb long double
#define pii pair<int,int>
#define mkp make_pair
#define mkt make_tuple
#define fr first
#define se second
#define uset unordered_set
#define umap unordered_map
#define pqueue priority_queue
#define all(_box) _box.begin(),_box.end()
#define ppc __builtin_popcount
#define ctz __builtin_ctz
#define clz __builtin_clz
#define lbd lower_bound
#define ubd upper_bound
#define deb(x) cerr<<#x<<'='<<(x)<<' '
using namespace std;
const int N=5e5+4;
int n,m;
int a[N],b[N];
struct node{
    int l,r,x;
}c[N];
int mx[N];
ll f[N];

namespace MFS{
    int fa[N];

    void init(){
        iota(fa+1,fa+n+2,1);
    }
    int find(int u){
        return u==fa[u]?u:fa[u]=find(fa[u]);
    }
    void merge(int u,int v){
        fa[find(v)]=find(u);
    }
}
using MFS::find;
using MFS::merge;

bool solve(){
    cin>>n>>m;
    MFS::init();
    for(int i=1;i<=n;i++)cin>>a[i];
    map<int,vector<pii>>qy;
    for(int i=1;i<=m;i++){
        cin>>c[i].l>>c[i].r>>c[i].x;
        qy[c[i].x].push_back({c[i].l,c[i].r});
    }
    sort(c+1,c+m+1,[](const node &u,const node &v){
        return u.x<v.x;
    });

    ll ans=0;
    map<int,vector<int>>pos;
    for(int i=m;i>=1;i--)
        for(int j=c[i].l;(j=find(j))<=c[i].r;merge(j+1,j)){
            pos[b[j]=c[i].x].push_back(j);
            if(a[j]<b[j])ans+=b[j]-a[j],a[j]=b[j];
        }
    
    for(const auto &i:qy)
        if(pos[i.fr].empty())return 0;
    
    for(const auto &i:pos){
        auto &arr=i.se;
        int len=arr.size();
        fill(mx+1,mx+len+1,0);
        for(const auto &j:qy[i.fr]){
            int l=lbd(all(arr),j.fr)-arr.begin()+1;
            int r=ubd(all(arr),j.se)-arr.begin();//
            if(l>r)return 0;
            mx[r]=max(mx[r],l);
        }

        deque<int>q({0});
        for(int j=1;j<=len;j++){
            mx[j]=max(mx[j],mx[j-1]);
            while(q.front()<mx[j-1])q.pop_front();
            f[j]=f[q.front()]+a[arr[j-1]]-i.fr;
            while(!q.empty()&&f[j]<f[q.back()])q.pop_back();
            q.push_back(j);
        }
        ans+=*min_element(f+mx[len],f+len+1);
    }
    cout<<ans<<'\n';
    return 1;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    int T;
    cin>>T;
    while(T--){
        if(solve());
        else cout<<-1<<'\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11812kb

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
Wrong Answer
time: 359ms
memory: 30604kb

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:

-1
-1
-1
-1
-1
-1
-1
46
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer 1st numbers differ - expected: '49', found: '-1'