QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#432964#8718. 保区间最小值一次回归问题FesdrerTL 347ms19428kbC++172.3kb2024-06-07 21:23:122024-06-07 21:23:13

Judging History

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

  • [2024-06-07 21:23:13]
  • 评测
  • 测评结果:TL
  • 用时:347ms
  • 内存:19428kb
  • [2024-06-07 21:23:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int T,n,m,a[N],nxt[N];
long long ans,f[N];
struct Node{int l,r,v;}que[N];
vector<pair<int,int>> query,base;
vector<int> id;
deque<int> q;
int get(int x){
	if(nxt[x]==x)	return x;
	return nxt[x]=get(nxt[x]);
}
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(){
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	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;
		for(int i=1;i<=n+1;i++)	nxt[i]=i;
		sort(que+1,que+m+1,[&](const Node &x,const Node &y){
			if(x.v==y.v)	return (x.r==y.r?x.l>y.l:x.r<y.r);
			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(),base.clear(),id.clear();
			while(r<n&&que[l].v==que[r+1].v){
				r++;
				if(query.empty()||que[r].l>query.back().first)	query.push_back({que[r].l,que[r].r});
				base.push_back({que[r].l,que[r].r});
			}
			i=r;
			bool flag=1;
			for(pair<int,int> it:query)	if(get(it.first)>it.second)	{flag=0;break;}
			if(!flag){flagg=0;break;}
			for(pair<int,int> it:query)	for(int j=get(it.first);j<=it.second;j=get(j+1))
				id.push_back(j),nxt[j]=get(j+1);
			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);
			id.clear();
			for(pair<int,int> it:base)	for(int j=get(it.first);j<=it.second;j=get(j+1))
				id.push_back(j),nxt[j]=get(j+1);
			for(int j:id)	ans+=max(0,que[i].v-a[j]);
		}
		if(flagg)	cout<<ans<<endl;
		else	cout<<-1<<endl;
	}
	return 0;
}

詳細信息

Test #1:

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

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: 347ms
memory: 19428kb

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: 328ms
memory: 18136kb

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: 0
Accepted
time: 329ms
memory: 19324kb

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
12729662919
9598250477
14090962014
12379912845
12111344391
11664729042
11963959801
12049221193
11907426860
12443185625
241361559829
15859152345
12213559141
13098156239
16019877206
13030210346
11413013605
12943939894
15243504691
10167904190
13684987690
12625876652
13823026696
...

result:

ok 1000 numbers

Test #5:

score: -100
Time Limit Exceeded

input:

1000
5 4
2 4 4 3 6
1 5 5
3 4 4
3 3 3
1 2 2
3 1
5 1 5
2 3 6
1 6
1
1 1 3
1 1 1
1 1 6
1 1 6
1 1 5
1 1 6
2 3
5 1
1 2 2
1 2 5
1 2 5
2 2
5 2
1 2 2
1 2 5
4 1
2 6 1 5
1 4 6
1 4
3
1 1 2
1 1 1
1 1 5
1 1 1
1 4
6
1 1 2
1 1 3
1 1 2
1 1 3
3 6
5 5 6
2 3 6
1 2 3
1 3 2
2 3 1
2 3 1
1 1 1
4 3
6 5 6 2
1 2 3
1 2 4
1 3 2...

output:

-1
6

result: