QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#432895#8718. 保区间最小值一次回归问题FesdrerTL 1ms3836kbC++172.2kb2024-06-07 20:06:352024-06-07 20:06:36

Judging History

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

  • [2024-06-07 20:06:36]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3836kb
  • [2024-06-07 20:06:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int T,n,m,a[N];
long long ans,f[N];
struct Node{int l,r,v;}que[N];
vector<pair<int,int>> query;
vector<int> id,liveid;
deque<int> q;
void solve(int v){
	while(q.size())	q.pop_back();
	f[0]=0;
	q.push_back(0);
	query.insert(query.begin(),{0,0});
	for(int i:id)	ans+=max(0,v-a[i]);
	id.insert(id.begin(),0);
	long long mn=0x3f3f3f3f3f3f3f3f;
	for(int i=1,j=0;i<=int(id.size())-1;i++){
		while(j<int(query.size())-1&&query[j+1].second<i)	j++;
		while(q.size()&&q.front()<query[j].first)	q.pop_front();
		f[i]=f[q.front()]+max(v,a[id[i]])-v;
		while(q.size()&&f[q.back()]<=f[i])	q.pop_back();
		q.push_back(i);
		if(i>=query.back().first&&i<=query.back().second)	mn=min(mn,f[i]);
	}
	ans+=mn;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);	
	cin>>T;
	while(T--){
		cin>>n>>m;
		for(int i=1;i<=n;i++)	cin>>a[i];
		for(int i=1;i<=m;i++)	cin>>que[i].l>>que[i].r>>que[i].v;
		liveid.clear();
		for(int i=1;i<=n+1;i++)	liveid.push_back(i);
		sort(que+1,que+m+1,[&](const Node &x,const Node &y){return x.v>y.v;});
		bool flagg=1;
		ans=0;
		for(int i=1;i<=m;i++){
			int l=i,r=i-1;
			query.clear(),id.clear();
			while(r<n&&que[l].v==que[r+1].v)	r++,query.push_back({que[r].l,que[r].r});
			i=r;
			sort(query.begin(),query.end(),[&](const pair<int,int> &x,const pair<int,int> &y){
				return (x.second==y.second?x.first>y.first:x.second<y.second);
			});
			for(int i=0;i<int(query.size());i++)
				if(i&&query[i].first<=query[i-1].first)	query.erase(query.begin()+i),i--;
			bool flag=1;
			for(pair<int,int> it:query)
				if(*lower_bound(liveid.begin(),liveid.end(),it.first)>it.second)	flag=0;
			if(!flag){flagg=0;break;}
			for(pair<int,int> it:query){
				vector<int>::iterator nowl=lower_bound(liveid.begin(),liveid.end(),it.first),nowr;
				for(nowr=nowl;(*nowr)<=it.second;nowr++)	id.push_back(*nowr);
				liveid.erase(nowl,nowr);
			}
			for(pair<int,int> &it:query){
				it.first=lower_bound(id.begin(),id.end(),it.first)-id.begin()+1;
				it.second=upper_bound(id.begin(),id.end(),it.second)-id.begin();
			}
			solve(que[i].v);
		}
		if(flagg)	cout<<ans<<endl;
		else	cout<<-1<<endl;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3836kb

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
Time Limit Exceeded

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:

574034911
467878458
53
1286167178
297741204
64
280347887
177712196
784026480
296728567
286923235
4988483639
14206888374
59
48
11175679
590350884
501237911
70
855019272
199949831
4700818306
49
180116732
1346919761
493114841
508696566
43
438210723
48
698387333
368822226
2114378718
601467502
1573521917...

result: