QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416683 | #8718. 保区间最小值一次回归问题 | Afterlife | WA | 855ms | 75024kb | C++17 | 2.5kb | 2024-05-22 02:42:44 | 2024-05-22 02:42:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+1e3+7;
int T;
int n,m,c[N],a[N];
int l[N],r[N],v[N];
vector<int> p[N];
map<int,vector<pair<int,int> >>seg;
map<int,vector<int> >seq;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i],p[i].clear();
seg.clear();
for(int i=1;i<=m;i++)
{
cin>>l[i]>>r[i]>>v[i];
seg[v[i]].push_back({l[i],r[i]});
p[l[i]].push_back(v[i]);
p[r[i]+1].push_back(-v[i]);
}
multiset<int> s;
for(int i=1;i<=n;i++)
{
for(auto x:p[i])
if(x<0)
s.erase(s.find(-x));
else
s.insert(x);
if(s.size())
c[i]=*prev(s.end());
else
c[i]=1;
}
seq.clear();
for(int i=1;i<=n;i++)
seq[-c[i]].push_back(i);
int ans=0;
for(auto [tx,v]:seq)
{
int x=-tx;
int m=v.size();
vector<int> s(m+1);
vector<int> f(m+1);
vector<int> mxl(m+1);
for(auto &[l,r]:seg[x])
{
l=lower_bound(v.begin(),v.end(),l)-v.begin()+1;
r=upper_bound(v.begin(),v.end(),r)-v.begin();
mxl[r]=max(mxl[r],l);
}
for(int i=1;i<=m;i++)
mxl[i]=max(mxl[i],mxl[i-1]);
for(int i=1;i<=m;i++)
{
s[i]=s[i-1];
if(x>a[v[i-1]])
s[i]+=x-a[v[i-1]];
}
deque<int> q;
q.push_back(0);
for(int i=1;i<=m;i++)
{
f[i]=1e18;
while(q.size()&&q.front()<mxl[i-1])
q.pop_front();
if(q.size())
f[i]=f[q.front()]-s[q.front()]+s[i-1]+abs(x-a[v[i-1]]);
while(q.size()&&f[i]-s[i]<f[q.back()]-s[q.back()])
q.pop_back();
q.push_back(i);
}
int mn=1e18;
for(int i=mxl[m];i<=m;i++)
mn=min(mn,f[i]);
ans+=mn;
ans=min(ans,(int)1e18);
}
if(ans>1e17)
cout<<"-1\n";
else
cout<<ans<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 25240kb
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: 855ms
memory: 75024kb
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:
39 36 42 34 45 46 49 43 42 47 45 50 893 47 45 48 43 51 51 42 46 37 37 48 49 48 42 38 37 40 38 49 53 42 51 39 45 44 34 47 42 50 38 46 908 872 48 44 48 41 37 39 48 49 37 45 46 46 47 45 33 43 39 38 46 38 44 45 38 46 49 41 36 43 45 49 43 45 41 37 40 34 38 41 43 44 47 43 36 43 43 39 46 51 39 37 40 45 48 ...
result:
wrong answer 1st numbers differ - expected: '49', found: '39'