QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602855 | #8718. 保区间最小值一次回归问题 | ucup-team902# | WA | 479ms | 25704kb | C++17 | 2.4kb | 2024-10-01 13:10:48 | 2024-10-01 13:10:49 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=5e5;
const ll inf=1ll<<60;
int n,m;
int a[N+5];
struct Que{
int l,r,v;
Que(int l=0,int r=0,int v=0):l(l),r(r),v(v){}
}b[N+5];
int f[N+5];
ll ans;
int find(int x){ return x==f[x]?x:f[x]=find(f[x]); }
bool cmp(Que x,Que y){ return x.v>y.v; }
struct Segtr{
ll tr[N*4+5];
#define lc id<<1
#define rc id<<1|1
void build(int l,int r,int id){
tr[id]=inf;
if(l==r) return;
int mid=l+r>>1;
build(l,mid,lc);
build(mid+1,r,rc);
}
ll query(int l,int r,int st,int en,int id){
if(st<=l&&en>=r) return tr[id];
int mid=l+r>>1; ll ans=inf;
if(st<=mid) ans=min(ans,query(l,mid,st,en,lc));
if(en>mid) ans=min(ans,query(mid+1,r,st,en,rc));
return ans;
}
void change(int l,int r,int p,ll x,int id){
if(l==r){
tr[id]=x;
return;
}
int mid=l+r>>1;
if(p<=mid) change(l,mid,p,x,lc);
else change(mid+1,r,p,x,rc);
}
}T;
bool work(int l,int r,int v){
static int mxl[N+5];
static int pos[N+5];
int ps=0;
for(int i=l;i<=r;i++){
int j=find(b[i].l);
while(j<=b[i].r){
pos[++ps]=j;
f[j]=j+1; j=find(j);
}
}
sort(pos+1,pos+1+ps);
fill(mxl+1,mxl+1+ps,0);
for(int i=l;i<=r;i++){
int ll=lower_bound(pos+1,pos+1+ps,b[i].l)-pos;
int rr=upper_bound(pos+1,pos+1+ps,b[i].r)-pos-1;
if(ll>rr) return 0;
mxl[rr]=max(mxl[rr],ll);
}
T.build(0,ps,1);
T.change(0,ps,0,0,1);
int ql=0;
for(int i=1;i<=ps;i++){
int p=pos[i];
ql=max(ql,mxl[i-1]);
ll val=T.query(0,ps,ql,i-1,1)+abs(v-a[p]);
T.change(0,ps,i,val,1);
if(a[i]<=v) ql=i;
}
ql=max(ql,mxl[ps]);
ans+=T.query(0,ps,ql,ps,1);
return 1;
}
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].v);
for(int i=1;i<=n+1;i++) f[i]=i;
ans=0;
sort(b+1,b+1+m,cmp);
for(int i=1,j;i<=m;i=j){
for(j=i;j<=m&&b[j].v==b[i].v;j++);
if(!work(i,j-1,b[i].v)){
puts("-1");
return;
}
}
printf("%lld\n",ans);
}
int main(){
int t; scanf("%d",&t);
while(t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 18220kb
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: 479ms
memory: 25704kb
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:
62 1152921504606847025 1152921504606847010 6917529027641081881 2305843009213693983 2305843009213693982 1152921504606847011 57 -5764607523034234859 63 3458764513820540961 2305843009213694011 8070450532247929456 1152921504606847039 1152921504606847020 1152921504606847039 2305843009213694004 2305843009...
result:
wrong answer 1st numbers differ - expected: '49', found: '62'