QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#417666 | #8718. 保区间最小值一次回归问题 | grass8cow# | WA | 450ms | 52972kb | C++17 | 1.8kb | 2024-05-22 20:35:27 | 2024-05-22 20:35:28 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,m,a[501000],qc[501000],l[501000],r[500100],v[501000],cn,b[500100],fa[500100];
vector<int>g1[501000],g2[500100];
#define ll long long
#define pb push_back
int F(int x){if(x==fa[x])return x;return fa[x]=F(fa[x]);}
int cy[501000],e,L[501000];
ll ans,dp[501000];
int sta[500100],fl,fr;
void sol(){
ans=0,scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=-1;
for(int i=1;i<=n+1;i++)fa[i]=i;
cn=0;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&l[i],&r[i],&v[i]);
qc[++cn]=v[i];
}
sort(qc+1,qc+cn+1);cn=unique(qc+1,qc+cn+1)-qc-1;
for(int i=1;i<=cn;i++)g1[i].clear(),g2[i].clear();
for(int i=1;i<=m;i++)
v[i]=lower_bound(qc+1,qc+cn+1,v[i])-qc,g1[v[i]].pb(i);
for(int i=cn;i;i--)for(int o:g1[i]){
int u=F(l[o]),v=F(r[o]+1);
while(u!=v)fa[u]=F(u+1),b[u]=i,u=fa[u];
}
for(int i=1;i<=n;i++)g2[b[i]].pb(i);
for(int v=1;v<=cn;v++){
e=0;for(int j:g2[v])cy[++e]=j;
for(int i=1;i<=e;i++)L[i]=0;
for(int j:g1[v]){
int x=lower_bound(cy+1,cy+e+1,l[j])-cy,y=lower_bound(cy+1,cy+e+1,r[j]+1)-cy-1;
if(x<=y)L[y]=max(L[y],x);
else{puts("-1");return;}
}
fl=fr=1,sta[1]=0;
ll mi=1e18;
for(int i=1;i<=e;i++){
L[i]=max(L[i],L[i-1]);int a_;
if(a[cy[i]]<qc[v])a_=0,ans+=qc[v]-a[cy[i]];
else a_=a[cy[i]]-qc[v];
while(fl<=fr&&sta[fl]<L[i-1])fl++;dp[i]=dp[sta[fl]]+a_;
while(fl<=fr&&dp[sta[fr]]>=dp[i])fr--;sta[++fr]=i;
if(L[e]<=i)mi=min(mi,dp[i]);
}
ans+=mi;
}
printf("%lld\n",ans);
}
int main(){
int T;scanf("%d",&T);while(T--)sol();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 27180kb
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: 450ms
memory: 52972kb
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:
49 45 43 39 48 47 49 46 46 48 47 56 952 53 50 54 46 62 57 46 49 50 42 55 51 57 50 43 41 44 41 53 57 45 59 45 48 44 37 48 43 52 45 50 909 947 48 50 50 50 45 41 54 52 42 46 55 52 51 48 37 48 43 41 55 41 48 47 38 51 54 46 44 47 46 49 43 48 42 45 45 36 43 45 53 46 48 45 42 45 48 40 49 54 44 43 45 48 49 ...
result:
wrong answer 2nd numbers differ - expected: '46', found: '45'