QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#664562#6531. Base Station ConstructionAyayaTL 1ms9892kbC++142.2kb2024-10-21 21:12:242024-10-21 21:12:26

Judging History

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

  • [2024-10-21 21:12:26]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:9892kb
  • [2024-10-21 21:12:24]
  • 提交

answer

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<unordered_map>
#include<vector>
#include<bitset>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<random>
#include<numeric>
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define lc (x<<1)
#define rc (x<<1|1)
#define pii pair<int,int>
#define mkp make_pair
#define fi first
#define se second
#define cout (cerr<<">> ",cout)
const int Mx=500005,p=998244353;
int read(){
	char ch=getchar();
	int Alice=0,Aya=1;
	while(ch<'0'||ch>'9'){
		if(ch=='-') Aya=-Aya;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
		Alice=(Alice<<3)+(Alice<<1)+(ch^48),ch=getchar();
	return (Aya==1?Alice:-Alice);
}
int n,m;
int cost[Mx];
struct Seg{
	int l,r;
	bool operator <(const Seg&o)const{
		return l==o.l?r<o.r:l<o.l;
	}
}a[Mx],c[Mx];
int f[Mx];
int t[Mx];
void Ins(int i,int k){
	for(;i<=m+1;i+=(i&-i)) t[i]=min(t[i],k);
}
int Qry(int i){
	int res=1e18;
	for(;i;i&=(i-1)) res=min(res,t[i]);
	return res;
}
void Solve(){
	n=read();
	for(int i=1;i<=n;i++) cost[i]=read();
	m=read();
	for(int i=0;i<=m+1;i++) t[i]=1e18;
	for(int i=1;i<=m;i++){
		a[i].l=read(),a[i].r=read();
	}
	sort(a+1,a+m+1);
	int M=1;
	for(int i=2;i<=m;i++){
		if(a[i].l==a[M].l) continue;
		if(a[i].r<=a[M].r) a[M]=a[i];
		else a[++M]=a[i];
	}
	m=M;
	int ans=1e18;
	Ins(m+1,0);
	for(int i=1;i<=n;i++){
		int L=1,R=m,ok=-1;
		while(L<=R){
			int mid=((L+R)>>1);
			if(a[mid].r>=i) R=mid-1,ok=mid;
			else L=mid+1;
		}
		c[i].l=ok;
		L=1,R=m,ok=-1;
		while(L<=R){
			int mid=((L+R)>>1);
			if(a[mid].l<=i) L=mid+1,ok=mid;
			else R=mid-1;
		}
		c[i].r=ok;
		//cout<<c[i].l<<" "<<c[i].r<<endl;
		if(c[i].l==-1||c[i].r==-1){
			f[i]=1e18;
			continue;
		}
		int res=1e18;
		for(int j=0;j<i;j++){
			if(c[j].r+1>=c[i].l) res=min(res,f[j]);
		}
		f[i]=res+cost[i];
		Ins(m+2-(c[i].r+1),f[i]);
		if(c[i].r==m) ans=min(ans,f[i]);
	}
	printf("%lld\n",ans);
}
signed main(){
	int T=read();
	while(T--) Solve();
	return 0;
}
/*
Hello!!
Sample:

-------------------
*/


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
5
3 2 4 1 100
3
1 3
2 4
5 5
5
7 3 4 2 2
3
1 4
2 3
4 5

output:

102
5

result:

ok 2 number(s): "102 5"

Test #2:

score: -100
Time Limit Exceeded

input:

6201
12
88 78 46 95 84 98 55 3 68 42 6 18
19
6 9
10 11
12 12
8 11
8 12
2 3
2 3
1 5
9 9
7 8
6 11
2 4
12 12
2 4
2 9
7 10
8 8
1 7
6 11
5
76 27 48 66 61
2
1 4
3 5
8
64 81 20 6 86 9 4 55
1
7 7
9
1 43 18 81 11 22 9 61 83
14
5 6
2 6
5 8
1 4
9 9
9 9
7 7
2 5
8 9
5 6
4 8
5 8
9 9
6 6
10
66 41 84 7 80 31 22 96 ...

output:

141
48
4
126
303
141
23
170
159
139
153
194
136
89
230
93
287
89
124
130
148
27
1
193
194
93
239
303
236
150
177
57
46
18
24
66
83
160
108
62
35
122
156
77
115
138
54
242
126
28
171
86
311
244
262
87
302
81
86
173
30
129
75
91
90
179
81
196
142
154
77
111
194
247
211
53
66
17
213
101
308
137
177
24
...

result: