QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#438303 | #8718. 保区间最小值一次回归问题 | as_lyr | WA | 456ms | 26240kb | C++14 | 2.1kb | 2024-06-10 15:19:53 | 2024-06-10 15:19:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510000;
const ll INF=1e18;
int n,m;
int a[N];
ll sum[N];
struct node{
int l,r,x;
}b[N];
int f[N];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
int g[N];
inline int as(int x){
return x<0?-x:x;
}
ll dp[N];
void solve(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].x);
sort(b+1,b+m+1,[](const node x,const node y){
return x.x>y.x;
});
for(int i=1;i<=n+1;i++)
f[i]=i;
ll ans=0;
for(int i=1,j;i<=m;i=j+1){
j=i;
while(j<m&&b[i].x==b[j+1].x)
j++;
vector <int> v;
for(int k=i;k<=j;k++)
for(int x=find(b[k].l);x<=b[k].r;x=find(x+1)){
v.push_back(x);
f[x]=x+1;
}
sort(v.begin(),v.end());
sort(b+i,b+j+1,[](const node x,const node y){
return x.r<y.r;
});
if(v.empty()){
puts("-1");
return ;
}
for(int k=i;k<=j;k++)
if(lower_bound(v.begin(),v.end(),b[k].l)==upper_bound(v.begin(),v.end(),b[k].r)){
puts("-1");
return ;
}
for(int k=0;k<=(int)v.size();k++)
g[k]=-1;
for(int k=i;k<=j;k++){
int x=lower_bound(v.begin(),v.end(),b[k].l)-v.begin();
int y=upper_bound(v.begin(),v.end(),b[k].r)-v.begin();
g[y]=max(g[y],x);
}
for(int k=1;k<=(int)v.size();k++)
g[k]=max(g[k],g[k-1]);
deque <pair<int,int>> q;
q.push_back(make_pair(-1,0));
sum[0]=(a[v[0]]<b[i].x?b[i].x-a[v[0]]:0);
for(int k=1;k<(int)v.size();k++)
sum[k]=sum[k-1]+(a[v[k]]<b[i].x?b[i].x-a[v[k]]:0);
for(int k=0;k<(int)v.size();k++){
while(q.empty()==0&&q.front().first<g[k])
q.pop_front();
dp[k]=q.front().second+sum[k-1]+as(a[v[k]]-b[i].x);
while(q.empty()==0&&q.back().second>=dp[k]-sum[k])
q.pop_back();
q.push_back(make_pair(k,dp[k]-sum[k]));
}
ll res=INF;
for(int k=g[(int)v.size()];k<(int)v.size();k++)
res=min(res,dp[k]+sum[(int)v.size()-1]-sum[k]);
ans+=res;
}
printf("%lld\n",ans);
}
int main(){
int T;
scanf("%d",&T);
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 14320kb
input:
1 3 2 2023 40 41 1 1 2022 2 3 39
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 456ms
memory: 26056kb
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 46 43 39 48 47 49 46 46 48 47 56 952 53 50 55 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 46 50 909 948 48 50 50 50 45 42 54 53 42 46 55 52 51 48 38 48 43 41 55 41 48 47 38 51 54 46 44 47 46 49 43 48 42 45 47 36 43 45 53 46 48 45 42 45 48 40 49 54 44 43 45 48 49 ...
result:
ok 1000 numbers
Test #3:
score: 0
Accepted
time: 455ms
memory: 26240kb
input:
1000 100 100 40 35141748 84105257 84105343 7273633 178494885 178495007 112519027 77833696 77833671 261586538 278472335 278472427 261586652 416361094 416361130 426649132 323519561 278472520 420127898 420127936 420127900 482516532 434108895 420127875 631535939 615931040 546346933 546346951 546347103 7...
output:
5391 5728 5395 4133 4671 3496 3629 5091 5285 5530 4748 5441 84743 6061 4524 6175 5755 5357 3835 5486 4698 5872 4955 5320 4374 5628 4426 4747 5019 4439 4361 5774 6141 6897 5578 5067 5036 5719 4380 4763 4098 4693 4514 5690 92018 84532 4776 5056 4351 5501 4885 4828 5223 6395 3673 6007 6028 5993 5289 45...
result:
ok 1000 numbers
Test #4:
score: -100
Wrong Answer
time: 453ms
memory: 23712kb
input:
1000 100 100 148088468 98149781 583014390 471155624 805747464 361353765 809227446 405213819 979246312 746234854 328924822 293996764 439427756 927284157 709886804 719570910 987283502 607077506 460561747 601690465 661900658 689494603 664094557 738719400 968257412 849232761 704892887 981300891 63494525...
output:
13272909075 10500705683 8935129067 9598250477 10625796188 8084945549 8577223091 11664729042 11963959801 12049221193 7612459564 12443185625 87450861197 15859152345 12213559141 13098156239 16019877206 13030210346 7118046309 12943939894 10948537395 10167904190 13684987690 12625876652 10529367910 115414...
result:
wrong answer 3rd numbers differ - expected: '12729662919', found: '8935129067'