QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#437797#8718. 保区间最小值一次回归问题233LWA 429ms32612kbC++142.5kb2024-06-09 17:48:182024-06-09 17:48:18

Judging History

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

  • [2024-06-09 17:48:18]
  • 评测
  • 测评结果:WA
  • 用时:429ms
  • 内存:32612kb
  • [2024-06-09 17:48:18]
  • 提交

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();
    fill(b+1,b+n+1,0);
    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;j++){
            pos[b[j]=c[i].x].push_back(j),merge(j+1,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=lbd(all(arr),j.se)-arr.begin()+1;
            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.empty()&&q.front()<mx[j-1])q.pop_front();
            if(q.empty())return 0;
            int pos=arr[q.front()];
            f[j]=f[q.front()]+a[pos]-b[pos];
            while(!q.empty()&&f[j]<f[q.back()])q.pop_back();
            q.push_back(j);
        }
        ans+=f[len];
    }
    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: 0ms
memory: 11748kb

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: 429ms
memory: 32612kb

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:

55
48
38
44
50
46
46
50
52
48
47
60
858
56
52
57
50
64
54
48
48
55
45
54
51
61
56
46
44
46
43
53
60
50
62
52
45
43
41
47
42
51
46
49
715
850
49
51
49
52
50
45
54
51
46
46
59
56
56
49
39
54
47
42
56
45
49
47
36
56
50
51
45
49
43
49
42
48
41
46
51
36
49
44
56
49
49
45
42
45
47
41
50
53
47
45
47
45
52
...

result:

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