QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431565#8718. 保区间最小值一次回归问题A_zjzjWA 499ms32976kbC++142.5kb2024-06-05 18:48:502024-06-05 18:48:51

Judging History

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

  • [2024-06-05 18:48:51]
  • 评测
  • 测评结果:WA
  • 用时:499ms
  • 内存:32976kb
  • [2024-06-05 18:48:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include"debug.h"
#else
#define debug(...) void()
#endif
#define all(x) (x).begin(),(x).end()
template<class T>
auto ary(T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
using ll=long long;
using ull=unsigned ll;
const int N=5e5+10;
const ll INF=1e18;
int T,n,m,a[N],lim[N];
struct opts{
	int l,r,v;
}o[N];
#ifdef DEBUG
ostream& operator << (ostream &out,opts a){
	return out<<make_tuple(a.l,a.r,a.v);
}
#endif
namespace SGT{
	int t[N<<2];
	void build(int l=1,int r=n,int rt=1){
		t[rt]=1;
		if(l==r)return;
		int mid=(l+r)>>1;
		build(l,mid,rt<<1);
		build(mid+1,r,rt<<1|1);
	}
	void update(int L,int R,int x,int l=1,int r=n,int rt=1){
		if(L<=l&&r<=R)return t[rt]=max(t[rt],x),void();
		int mid=(l+r)>>1;
		if(L<=mid)update(L,R,x,l,mid,rt<<1);
		if(mid<R)update(L,R,x,mid+1,r,rt<<1|1);
	}
	void push(int l=1,int r=n,int rt=1,int x=1){
		x=max(x,t[rt]);
		if(l==r)return lim[l]=x,void();
		int mid=(l+r)>>1;
		push(l,mid,rt<<1,x);
		push(mid+1,r,rt<<1|1,x);
	}
}
int cur[N],mx[N],q[N];
ll f[N],s[N];
void get(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	SGT::build();
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&o[i].l,&o[i].r,&o[i].v);
		SGT::update(o[i].l,o[i].r,o[i].v);
	}
	SGT::push(),lim[n+1]=0;
	iota(cur,cur+1+n,0);
	sort(cur+1,cur+1+n,[&](int x,int y){
		return lim[x]^lim[y]?lim[x]<lim[y]:x<y;
	});
	sort(o+1,o+1+m,[&](opts x,opts y){
		if(x.v^y.v)return x.v<y.v;
		return x.r<y.r;
	});
	ll ans=0;
	// debug(ary(lim,1,n));
	for(int l=1,r,x=1,y;l<=m;l=r,x=y){
		for(r=l+1;r<=m&&o[l].v==o[r].v;r++);
		for(y=x;y<=n&&lim[cur[y]]==o[l].v;y++);
		// debug(ary(o,l,r-1),ary(cur,x,y-1));
		if(y==x)return puts("-1"),void();
		cur[x-1]=0;
		int ne=n+1;
		swap(ne,cur[y]);
		mx[l-1]=s[x-1]=0;
		for(int i=l;i<r;i++)mx[i]=max(mx[i-1],o[i].l);
		for(int i=x;i<=y;i++)s[i]=s[i-1]+max(lim[cur[i]]-a[cur[i]],0);
		// debug(ary(s,x,y));
		int L=1,R=1;
		q[R]=x-1;
		fill(f+x,f+1+y,INF),f[x-1]=0;
		for(int i=x,p=l;i<=y;i++){
			for(;p<r&&o[p].r<cur[i];p++);
			for(;L<=R&&cur[q[L]]<mx[p-1];L++);
			if(L<=R){
				f[i]=f[q[L]]+s[i-1]-s[q[L]]+abs(lim[cur[i]]-a[cur[i]]);
				for(;L<=R&&f[q[R]]-s[q[R]]>=f[i]-s[i];R--);
				q[++R]=i;
			}
		}
		if(f[y]==INF)return puts("-1"),void();
		ans+=f[y];
		swap(ne,cur[y]);
	}
	printf("%lld\n",ans);
}
int main(){
	for(scanf("%d",&T);T--;)get();
	return 0;
}
#ifdef DEBUG
#include"debug.hpp"
#endif

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 20204kb

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: 499ms
memory: 32976kb

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:

-1
-1
43
-1
-1
-1
-1
-1
46
-1
47
56
-1
-1
-1
2742555400
-1
-1
-1
-1
-1
2688779800
-1
-1
-1
2742555402
-1
-1
-1
-1
-1
-1
1989697072
-1
-1
1882145870
-1
1505716704
-1
-1
1613267893
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
299030207
-1
338034128
468047242
-1
650065602
-1
-1
-1
650065598
312031507
-1
-1
-1
-1
-...

result:

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